Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission Week Eight #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions week08/basic_calculator_ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time complexity: O(N), N = Number of characters in the string.
// Space complexity: O(total count of possible numbers in the string)

int calculate(string s){
long long currNum = 0;
long result = 0;
stack<int> stk;
char sign = '+';
int s_length = s.size();
for(int i = 0; i < s_length; i++){
if(s[i] >= '0' && s[i] <= '9'){
currNum = 10 * currNum + s[i] - '0';
}
if(!(s[i] >= '0' && s[i] <= '9') && (s[i] != ' ') || i == s_length - 1){
if(sign == '+') stk.push(currNum);
else if(sign == '-') stk.push(-1 * currNum);
else{
if(sign == '*') currNum = stk.top() * currNum;
else currNum = stk.top() / currNum;
stk.pop();
stk.push(currNum);
}
currNum = 0;
sign = s[i];
}
}
while(!stk.empty()){
result += stk.top();
stk.pop();
}
return result;
}
24 changes: 24 additions & 0 deletions week08/count_primes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Time complexity: O(square_root(N) * log(log(N)) , N = Given integer number.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's O(N * log(log(N))). Not O(sqrt(N) * log(log(N))).

// Space complexity: O(N)

int sieve(int n){
vector<bool> comp(n + 5, false);
comp[1] = true;
for(int i = 2; i * i <= n; i++){
if(comp[i] == false){
for(int j = i * i; j <= n; j += i){
comp[j] = true;
}
}
}
int count = 1;
for(int i = 3; i < n; i += 2){
if(comp[i] == false) count++;
}
return count;
}

int countPrimes(int n){
if(n == 0 || n == 1 || n == 2) return 0;
return sieve(n);
}
11 changes: 11 additions & 0 deletions week08/factorial_trailing_zeroes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//Time complexity: O(Count of 5 in the n's factorial's divisors).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which is, in worst case, O(log5(N)).

//Space complexity: O(1)
int trailingZeroes(int n){
int count_5 = 0;
int div = 5;
while(n >= div){
count_5 += n / div;
div *= 5;
}
return count_5;
}
27 changes: 27 additions & 0 deletions week08/guess_the_word.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time complexity: O(N), N = Number of strings.
// Space complexity: O(N)

int calculateScore(string &ref, string &candidate){
int res = 0;
int ref_length = ref.size();
for(int i = 0; i < ref_length; i++){
res += ref[i] == candidate[i];
}
return res;
}

void findSecretWord(vector<string> &wordlist, Master &master){
while(wordlist.size() > 1){
int rnd_index = rand() % wordlist.size();
string query_string = wordlist[rnd_index];
int score = master.guess(query_string);
vector<string> new_word_list;
for(auto &word: wordlist){
if(calculateScore(word, query_string) == score){
new_word_list.push_back();
}
}
wordlist = new_word_list;
}
master.quess(wordlist[0]);
}
20 changes: 20 additions & 0 deletions week08/powx_n.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Time complexity: O(log N), N is the power of the base.
// Space complexity: O(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is O(log(N)) too. Think why.


double modularExpo(double x, long long n){
if(n == 0) return 1.0;
if(n % 2 == 0){
double res = modularExpo(x, n / 2);
return res * res;
}
return x * modularExpo(x, n - 1);
}

double myPow(double x, int n){
if(n == 0) return 1.0;
bool inverse_flag = n < 0;
long long N = n < 0? (-1) * 1LL * n : n;
double result = modularExpo(x, N);
result = inverse_flag ? 1.0 / result : result;
return result;
}
12 changes: 12 additions & 0 deletions week08/sqrtx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Time complexity: O(log N) , N = given Number;
// Space complexity: O(1)

int mySqrt(int x){
long long left = 0, right = x;
while(left < right){
long long mid = left + (right - left + 1) / 2;
if(mid * mid > x) right = mid - 1;
else left = mid;
}
return right;
}