-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recursion exercises and maze resolution with backtracking
- Loading branch information
1 parent
2104c92
commit 27ae187
Showing
5 changed files
with
642 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
|
||
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; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <cstring> | ||
#include <algorithm> | ||
|
||
/* | ||
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; | ||
// ---------------------------------------------------- | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.