diff --git a/theory/recursion/binary_search.cpp b/theory/recursion/binary_search.cpp new file mode 100644 index 0000000..c79a4b1 --- /dev/null +++ b/theory/recursion/binary_search.cpp @@ -0,0 +1,37 @@ +#include +#include + + +int binary_search(int *v, int l, int r, int x) { + + int m = (l+r)/2; + + // base cases + if(v[m] == x) + return m; + + if(l > r) + return -1; + + // recursion call + if(v[m] < x ) + return binary_search(v,m+1,r,x); + else + return binary_search(v,l,m-1,x); + +} + + +int main(){ + + int vet[] = {0,1,2,3,4,5,6,7,8,9}; + int n = 10; + int v = binary_search(vet,0,n-1,8); + + if(v != -1) + std::cout << "found at index: " << v << std::endl; + else + std::cout << "not found" << std::endl; + +} + diff --git a/theory/recursion/level1_questions.cpp b/theory/recursion/level1_questions.cpp new file mode 100644 index 0000000..7725986 --- /dev/null +++ b/theory/recursion/level1_questions.cpp @@ -0,0 +1,222 @@ +#include +#include +#include +#include + +/* + Question 1: + + Give an integer N, print all the number before N + until 1; + + input: 5 + + output: 5 4 3 2 1 +*/ +void print_until_n(int n) { + + // base case + if(n == 1) { + std::cout << 1 << std::endl; + return; + } + + // recursion call + std::cout << n << " "; + print_until_n(n-1); + +} + + +/* + Question 2: + + Print all the number between [1,N]; + + input: 5 + + output: 1 2 3 4 5 +*/ +void print_at_n(int n) { + + // base case + if(n == 1) { + std::cout << 1 << " "; + return; + } + + // recursion call + print_at_n(n-1); + std::cout << n << " "; + +} + +/* + Question 3: + + Factorial of a number N; +*/ +int factorial(int n) { + + // base case + if(n==1) + return 1; + + // recursion case + return n*factorial(n-1); + +} + +/* + Question 4: + + Sum of the digit between [N,1]; + + +*/ +int sum_between_n(int n) { + + // base case + if(n == 1) + return 1; + + // recursion call + return n+sum_between_n(n-1); + +} + +/* + Question 5: + + sum of digits of N + +*/ +int sum_of_digits(int n) { + + // base case + if (n/10 == 0) + return n; + + // recursion call + return n%10+sum_of_digits(n/10); + +} + +/* + Question 6: + + Products of digits of N; +*/ +int products_of_n(int n) { + + // base case + if(n/10 == 0) + return n; + + // recursion call + return n%10*products_of_n(n/10); + +} + +/* + Question 7: + + reverse a number N; +*/ +void reverse(int n) { + + // base case + if(n/10 == 0) { + std::cout << n; + return; + } + + std::cout << n%10; + reverse(n/10); + +} + +/* + Question 8: + + Check if N is a palindrome +*/ +bool palindrome(std::string n) { + + std::string n2; + for(int i = n.length(), b = 0; i >= 0; --i, ++b) + n2[b] = n[i]; + + if(n2 == n) + return true; + else + return false; + +} + + + + + + +int main(){ + + std::cout << "Question 01" << std::endl; + print_until_n(5); + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 02" << std::endl; + print_at_n(5); + std::cout << std::endl; + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 03" << std::endl; + std::cout << factorial(5) << std::endl; + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 04" << std::endl; + std::cout << sum_between_n(10) << std::endl; + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 05" << std::endl; + std::cout << sum_of_digits(123) << std::endl; + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 06" << std::endl; + std::cout << products_of_n(222) << std::endl; + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 07" << std::endl; + reverse(345); + std::cout << std::endl; + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 08" << std::endl; + if(palindrome("123321")) + std::cout << "Is a palindrome" << std::endl; + else + std::cout << "Isn't a palindrome" << std::endl; + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 09" << std::endl; + + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 10" << std::endl; + + std::cout << std::endl; +// ---------------------------------------------------- + std::cout << "Question 11" << std::endl; + + std::cout << std::endl; +// ---------------------------------------------------- + + +} + + + + + + + diff --git a/theory/recursion/maze.cpp b/theory/recursion/maze.cpp new file mode 100644 index 0000000..7efaeea --- /dev/null +++ b/theory/recursion/maze.cpp @@ -0,0 +1,283 @@ +#include +#include + +/* + Question 1: + + Getting the number of paths that solves a maze(MxN) + + note: This first maze doesn't have obstacles and just move right + and down + +*/ +int countPaths(int r, int c) { + + // If our maze just can move to right and down + // base case + if(r == 1 || c == 1) + return 1; + + // segment tree for left and right + // recursion call + int left = countPaths(r-1,c); + int right = countPaths(r,c-1); + + return left+right; + +} + +/* + Question 2: + + Printing the paths to solve the maze(MxN) + + note: The maze doesn't have obstacles, you only can move to + right and down + +*/ +void printingPaths(std::string path, int r, int c) { + + // base case + if(r == 1 && c == 1) { + std::cout << path << std::endl; + return; + } + + // recursion call + // left recursion call + if(r > 1) + printingPaths(path + "V", r-1,c); + + // right recursion call + if(c > 1) + printingPaths(path + "H",r,c-1); + +} + +/* + Question 3: + + Print the paths including diagonal right(NxM) + + note: the maze doesn't have obstacles + you can justo move to right, diagonal right and down; + +*/ +void printingPathsDiagonal(std::string path, int r, int c) { + + // base case + if(r == 1 && c == 1) { + std::cout << path << std::endl; + return; + } + + // recursion call + // left recursion (when goes down) + if (r > 1) + printingPathsDiagonal(path + "V", r-1, c); + + // mid recursion (when goes diagonally) + if(r > 1 && c > 1) + printingPathsDiagonal(path + "D", r-1, c-1); + + // right recursion (when goes right) + if(c > 1) + printingPathsDiagonal(path + "H", r, c-1); + +} + +/* + Question 4: + + How to solve a maze(MxN) with obstacles + + obs: you can only move to right and down +*/ +void pathWithRestrictions(std::string path, std::vector> maze, int r, int c) { + + // Now we've a boolean matrix that dicts where is the + // rowns and colums blocked (and we start at 0,0) + if(r == maze.size()-1 && c == maze[0].size()-1) { + std::cout << path << std::endl; + return; + } + + // Check if is possible to continue the path + if(maze[r][c]) + return; + + if(r < maze.size()-1) { + pathWithRestrictions(path + "V", maze, r+1, c); + } + + if (c < maze[0].size()-1) { + pathWithRestrictions(path+"H", maze, r, c+1); + } + +} + +/* + Question 5 + + including all paths and using the same logic to the maze + as question 4 + +*/ +void pathsMaze(std::string path, std::vector> maze, int r, int c) { + + // base case + if(r == maze.size()-1 && c == maze[0].size()-1) { + std::cout << path << std::endl; + return; + } + + // checking if is possible to cross + if(maze[r][c]) + return; + + // Considering this block in the path + maze[r][c] = 1; + + + // recursion call + // check if is possible to go down + if(r < maze.size()-1) + pathsMaze(path + "D", maze, r+1,c); + + // check if is possible to go left + // the row don't change, just the column + if(c > 0) + pathsMaze(path + "L", maze, r, c-1); + + //check if is possible to go up + // the colum don't change, just the row + if(r > 0) + pathsMaze(path + "U", maze, r-1,c); + + // check if is possible to go right + if(c < maze[0].size()-1) + pathsMaze(path + "R", maze, r, c+1); + + // restoring the original maze (when the function is over) + // before the function gets removed, also remove the changes + // that were made by that function + maze[r][c] = 0; // backtracking + +} + +/* + Question 6: The final!! + + print the paths and the matrix of a maze(MxN) that can + have obstacles. + + obs: you can go to 4 directions (up,left,down,right) +*/ +void solveMaze(std::string output, std::vector> maze,std::vector> &path, int r, int c, int step) { + + // base call + if(r == maze.size()-1 && c == maze[0].size()-1) { + // printing the values of path + for(int i = 0; i < path.size(); ++i){ + for(int j = 0; j < path[0].size(); ++j) + std::cout << path[i][j] << " "; + std::cout << std::endl; + } + std::cout << output << std::endl; + std::cout << std::endl; + return; + } + + // checking if we can pass through + if(maze[r][c]) + return; + + // mark if we pass + maze[r][c] = 1; + path[r][c] = step; // step starts at 1 + + // recursion call to the directins + // down + if(r < maze.size()-1) + solveMaze(output + "D", maze, path, r+1, c, step+1); + + + // left + if(c > 0) + solveMaze(output + "L", maze, path, r, c-1, step+1); + + + // up + if(r > 0) + solveMaze(output + "U", maze, path, r-1, c, step+1); + + + //right + if (c < maze[0].size()-1) + solveMaze(output + "R", maze, path, r, c+1, step+1); + + + // backtracking + maze[r][c] = 0; + path[r][c] = 0; + +} + + + +int main(){ + + std::cout << "Question 1" << std::endl; + std::cout << countPaths(3,3) << std::endl; + std::cout << std::endl; +// --------------------------------------------------- + std::cout << "Question 2" << std::endl; + std::string output = ""; + printingPaths(output,3,3); + std::cout << std::endl; +// --------------------------------------------------- + std::cout << "Question 3" << std::endl; + output = ""; + printingPathsDiagonal(output,3,3); + std::cout << std::endl; +// --------------------------------------------------- + std::cout << "Question 4" << std::endl; + std::vector> maze; + output = ""; + maze = { + {0,0,0,0}, + {0,1,0,0}, + {0,0,0,1}, + {1,0,0,0} + }; + pathWithRestrictions(output, maze, 0, 0); + std::cout << std::endl; +// ----------------------------------------------------- + std::cout << "Question 5" << std::endl; + output = ""; + maze = { + {0,0,0}, + {0,0,0}, + {0,0,0}, + }; + pathsMaze(output,maze,0,0); + std::cout << std::endl; +// ----------------------------------------------------- + std::cout << "Question 6" << std::endl; + output = ""; + maze = { + {0,0,0}, + {0,0,0}, + {0,0,0}, + }; + std::vector> arr; + arr = { + {0,0,0}, + {0,0,0}, + {0,0,0}, + }; + solveMaze(output,maze,arr,0,0,1); + std::cout << std::endl; +} + + diff --git a/theory/recursion/pattern.cpp b/theory/recursion/pattern.cpp new file mode 100644 index 0000000..3523228 --- /dev/null +++ b/theory/recursion/pattern.cpp @@ -0,0 +1,56 @@ +#include + +/* + Question 1: + + Print a triangle that follows this pattern (recursively) + + * * * * * + * * * * + * * * + * * + * + +*/ + +void triangle1(int n) { + + if (n == 0) + return; + + for(int i = 0; i < n; ++i) + std::cout << "*" << " "; + std::cout << std::endl; + + triangle1(n-1); + +} + +// printing a normal triangle without "loop" +void triangle1_opt(int r, int c) { + + if(r == 0) + return; + + if(c +#include + + + +/* + Question 1: + + skip a caracter in string + + intput: baccad, a + output: bccd + +*/ +void jump_char(char *input, char *output, char a, int i, int j) { + + if(input[i] == '\0') { + output[j] = '\0'; + return; + } + + if(input[i] == a) + jump_char(input,output,a,i+1,j); + else { + output[j] = input[i]; + jump_char(input,output,a,i+1,j+1); + } + +} + + +int main(){ + + char input[100], output[100]; + char a; + std::cin >> input >> a; + jump_char(input,output,a,0,0); + + std::cout << output << std::endl; + + +} + +