Initial commit of code for dopelib, and ch 1, 2, and 12
This commit is contained in:
6
libs/chapter1/chapter1.hpp
Normal file
6
libs/chapter1/chapter1.hpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
bool IsUnique(const char* s, int len);
|
||||
bool CheckPermutation(const char* s1, const char* s2, size_t s1_len, size_t s2_len);
|
||||
35
libs/chapter1/check_permutation.cpp
Normal file
35
libs/chapter1/check_permutation.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "chapter1.hpp"
|
||||
|
||||
/* Prompt
|
||||
Check Permutation: Given two strings, write a method to decide if
|
||||
one is a permutation of the other.
|
||||
*/
|
||||
|
||||
bool CheckPermutation(const char* s1, const char* s2,
|
||||
size_t s1_len, size_t s2_len){
|
||||
|
||||
int letters1[255] = {};
|
||||
int letters2[255] = {};
|
||||
long unsigned int indx = 0;
|
||||
|
||||
if(s1_len != s2_len){
|
||||
return false;
|
||||
}
|
||||
|
||||
for(indx = 0; indx < s1_len; ++indx){
|
||||
int li1 = static_cast<int>(s1[indx]);
|
||||
int li2 = static_cast<int>(s2[indx]);
|
||||
letters1[li1] += 1;
|
||||
letters2[li2] += 1;
|
||||
}
|
||||
|
||||
for(indx = 0; indx < 255; ++indx){
|
||||
if (letters1[indx] != letters2[indx]){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
21
libs/chapter1/is_unique.cpp
Normal file
21
libs/chapter1/is_unique.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include "chapter1.hpp"
|
||||
|
||||
/* Prompt
|
||||
Is Unique: Implement an algorithm to determine if a string
|
||||
has all unique characters. What if you cannot use additional data
|
||||
structures?
|
||||
*/
|
||||
|
||||
bool IsUnique(const char* s, int len){
|
||||
int tracker[255]= {};
|
||||
|
||||
for(int i=0; i<len; ++i){
|
||||
int tracker_index = static_cast<int>(s[i]);
|
||||
tracker[tracker_index] += 1;
|
||||
if (tracker[tracker_index] > 1){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
5
libs/chapter1/meson.build
Normal file
5
libs/chapter1/meson.build
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
chapter1_sources = ['is_unique.cpp', 'check_permutation.cpp']
|
||||
|
||||
chapter1_lib = static_library('chapter1',
|
||||
chapter1_sources)
|
||||
Reference in New Issue
Block a user