Skip to content

Commit f26d593

Browse files
authored
Add files via upload
1 parent 038007d commit f26d593

13 files changed

+233
-0
lines changed

Diff for: Lecture 3 - Recursion 1/All Indices of Number.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
int allIndexes(int input[], int size, int x, int output[]) {
2+
/* Don't write main().
3+
Don't read input, it is passed as function argument.
4+
Save all the indexes in the output array passed and return the size of
5+
output array. Taking input and printing output is handled automatically.
6+
*/
7+
if (size == 0) {
8+
return 0;
9+
}
10+
11+
int ans = allIndexes(input, size - 1, x, output);
12+
13+
if (input[size - 1] == x) {
14+
output[ans] = size - 1;
15+
return ans + 1;
16+
}
17+
else {
18+
return ans;
19+
}
20+
}

Diff for: Lecture 3 - Recursion 1/Check Number.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
bool checkNumber(int input[], int size, int x) {
2+
/* Don't write main().
3+
Don't read input, it is passed as function argument.
4+
Return output and don't print it.
5+
Taking input and printing output is handled automatically.
6+
*/
7+
8+
if(size == 0)
9+
{
10+
return false;
11+
}
12+
13+
if (input[0] == x) {
14+
return true;
15+
}
16+
17+
return checkNumber(input + 1, size - 1, x);
18+
19+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
bool checkPalindrome(char input[], int size)
2+
{
3+
if(size == 0 || size == 1)
4+
{
5+
return true;
6+
}
7+
8+
if(input[0] == input[size-1])
9+
{
10+
return checkPalindrome(input+1, size-2);
11+
}
12+
13+
else
14+
{
15+
return false;
16+
}
17+
18+
}
19+
20+
bool checkPalindrome(char input[]) {
21+
// Write your code here
22+
int length = 0;
23+
for(int i=0; input[i] != '\0'; i++) length++;
24+
return checkPalindrome(input, length);
25+
}

Diff for: Lecture 3 - Recursion 1/Count Zeros.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
int countZeros(int n) {
3+
// Write your code here
4+
if(n == 0)
5+
{
6+
return 1;
7+
}
8+
9+
if(n < 10)
10+
{
11+
return 0;
12+
}
13+
14+
int ans = countZeros(n/10);
15+
16+
if(n % 10 == 0)
17+
{
18+
return ans+1;
19+
}
20+
21+
return ans;
22+
}
23+
24+

Diff for: Lecture 3 - Recursion 1/First Index of Number.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
int firstIndex(int input[], int size, int x) {
2+
/* Don't write main().
3+
Don't read input, it is passed as function argument.
4+
Return output and don't print it.
5+
Taking input and printing output is handled automatically.
6+
*/
7+
8+
if(size == 0)
9+
{
10+
return -1;
11+
}
12+
13+
if(input[0] == x)
14+
{
15+
return 0;
16+
}
17+
18+
int ans = firstIndex(input+1, size-1, x);
19+
20+
if(ans != -1)
21+
{
22+
return ans+1;
23+
}
24+
return -1;
25+
}

Diff for: Lecture 3 - Recursion 1/Geometric Sum.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
double geometricSum(int k) {
2+
// Write your code here
3+
if(k == 0)
4+
{
5+
return 1;
6+
}
7+
8+
float ans = geometricSum(k-1);
9+
10+
return ans + (1/pow(2,k));
11+
}

Diff for: Lecture 3 - Recursion 1/Last Index of Number.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
int lastIndex(int input[], int size, int x) {
2+
/* Don't write main().
3+
Don't read input, it is passed as function argument.
4+
Return output and don't print it.
5+
Taking input and printing output is handled automatically.
6+
*/
7+
if(size == 0)
8+
{
9+
return -1;
10+
}
11+
12+
int ans = lastIndex(input+1, size-1, x);
13+
14+
if(ans == -1)
15+
{
16+
if (input[0] == x)
17+
{
18+
return 0;
19+
}
20+
else
21+
{
22+
return -1;
23+
}
24+
}
25+
else
26+
{
27+
return ans+1;
28+
}
29+
30+
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int multiplyNumbers(int m, int n) {
2+
// Write your code here
3+
if(n==0)
4+
{
5+
return 0;
6+
}
7+
int ans = multiplyNumbers(m,n-1);
8+
9+
return ans + m;
10+
}

Diff for: Lecture 3 - Recursion 1/Number of Digits.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
int count(int n){
2+
//write your code here
3+
4+
if(n == 0)
5+
{
6+
return 0;
7+
}
8+
9+
int ans = count(n/10);
10+
11+
return ans + 1;
12+
}

Diff for: Lecture 3 - Recursion 1/Power.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
int power(int x, int n) {
2+
/* Don't write main().
3+
Don't read input, it is passed as function argument.
4+
Return output and don't print it.
5+
Taking input and printing output is handled automatically.
6+
*/
7+
if(n == 0)
8+
{
9+
return 1;
10+
}
11+
12+
int ans = power(x, n-1);
13+
14+
return ans * x;
15+
}

Diff for: Lecture 3 - Recursion 1/Print Numbers.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
void print(int n){
2+
//write your code here
3+
4+
if(n == 1)
5+
{
6+
cout << n << " ";
7+
return;
8+
}
9+
10+
print(n-1);
11+
12+
cout << n << " ";
13+
}

Diff for: Lecture 3 - Recursion 1/Sum of Array.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
int sum(int input[], int n) {
2+
/* Don't write main().
3+
Don't read input, it is passed as function argument.
4+
Return output and don't print it.
5+
Taking input and printing output is handled automatically.
6+
*/
7+
8+
if(n == 1)
9+
{
10+
return input[n-1];
11+
}
12+
13+
int ans = sum(input+1, n-1);
14+
15+
return input[0] + ans;
16+
17+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
int sumOfDigits(int n) {
2+
// Write your code here
3+
if(n ==0)
4+
{
5+
return 0;
6+
}
7+
8+
int ans = sumOfDigits(n/10);
9+
10+
return n%10 + ans;
11+
}
12+

0 commit comments

Comments
 (0)