Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b4d028b

Browse files
committed
chapter 9: recursion
1 parent 582b68d commit b4d028b

File tree

6 files changed

+183
-0
lines changed

6 files changed

+183
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// src/09-recursion/01-intro-recursion.js
2+
3+
// To understand recursion, one must first understand recursion
4+
const readline = require('readline').createInterface({
5+
input: process.stdin,
6+
output: process.stdout
7+
});
8+
9+
function understandRecursion() {
10+
readline.question('Do you understand recursion? (y/n) ', (answer) => {
11+
if (answer.toLowerCase() === 'y') { // Base case
12+
console.log("Excellent! You've grasped recursion.");
13+
readline.close(); // Exit the program
14+
} else {
15+
console.log("Let's try again...");
16+
understandRecursion(); // Recursive call
17+
}
18+
});
19+
}
20+
21+
understandRecursion();
22+
23+
// to see the output of this file use the command: node src/09-recursion/01-intro-recursion.js

‎src/09-recursion/02-factorial.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// src/09-recursion/02-factorial.js
2+
3+
// iterative approach
4+
function factorialIterative(number) {
5+
if (number < 0) {
6+
return undefined;
7+
}
8+
let total = 1;
9+
for (let n = number; n > 1; n--) {
10+
total *= n;
11+
}
12+
return total;
13+
}
14+
15+
console.log('5! =',factorialIterative(5)); // 5! = 120
16+
17+
// recursive approach
18+
function factorial(number) {
19+
// console.trace();
20+
if (number < 0) { return undefined; }
21+
if (number === 1 || number === 0) { // base case
22+
return 1;
23+
}
24+
return number * factorial(number - 1);
25+
}
26+
27+
console.log('Recursive 5! =',factorial(5)); // Recursive 5! = 120
28+
29+
// to see the output of this file use the command: node src/09-recursion/02-factorial.js

‎src/09-recursion/03-callstack.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// src/09-recursion/03-callstack.js
2+
3+
// stack overflow
4+
let count = 0;
5+
function recursiveFn() {
6+
count++;
7+
recursiveFn();
8+
}
9+
10+
try {
11+
recursiveFn();
12+
} catch (ex) {
13+
console.log('count = ' + count + ' error: ' + ex);
14+
}
15+
16+
17+
// to see the output of this file use the command: node src/09-recursion/03-callstack.js

‎src/09-recursion/04-fibonacci.js‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// src/09-recursion/04-fibonacci.js
2+
3+
// iterative approach
4+
function fibonacciIterative(n) {
5+
if (n < 0) {
6+
throw new Error('Input must be a non-negative integer');
7+
}
8+
if (n < 2) { return n; }
9+
10+
let prevPrev = 0;
11+
let prev = 1;
12+
let current;
13+
14+
for (let i = 2; i <= n; i++) { // n >= 2
15+
current = prev + prevPrev; // f(n-1) + f(n-2)
16+
prevPrev = prev;
17+
prev = current;
18+
}
19+
20+
return current;
21+
}
22+
23+
console.log('fibonacciIterative(2)', fibonacciIterative(2)); // 1
24+
console.log('fibonacciIterative(3)', fibonacciIterative(3)); // 2
25+
console.log('fibonacciIterative(4)', fibonacciIterative(4)); // 3
26+
console.log('fibonacciIterative(5)', fibonacciIterative(5)); // 5
27+
28+
// recursive approach
29+
function fibonacci(n) {
30+
if (n < 0) {
31+
throw new Error('Input must be a non-negative integer');
32+
}
33+
if (n < 2) { return n; } // base case
34+
return fibonacci(n - 1) + fibonacci(n - 2); // recursive case
35+
}
36+
37+
console.log('fibonacci(5)', fibonacci(5)); // 5
38+
39+
// memoization approach
40+
function fibonacciMemoization(n) {
41+
if (n < 0) {
42+
throw new Error('Input must be a non-negative integer');
43+
}
44+
const memo = [0, 1];
45+
const fibonacci = (n) => {
46+
if (memo[n] != null) return memo[n];
47+
return memo[n] = fibonacci(n - 1) + fibonacci(n - 2);
48+
};
49+
return fibonacci(n);
50+
}
51+
52+
console.log('fibonacciMemoization(5)', fibonacciMemoization(5)); // 5
53+
54+
// to see the output of this file use the command: node src/09-recursion/04-fibonacci.js
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://leetcode.com/problems/fibonacci-number/description/
2+
3+
function fib(n: number): number {
4+
if (n < 2) { return n; }
5+
6+
let prevPrev = 0;
7+
let prev = 1;
8+
let current;
9+
10+
for (let i = 2; i <= n; i++) { // n >= 2
11+
current = prev + prevPrev; // f(n-1) + f(n-2)
12+
prevPrev = prev;
13+
prev = current;
14+
}
15+
16+
return current;
17+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// https://leetcode.com/problems/power-of-two/
2+
3+
// function isPowerOfTwo(n: number): boolean {
4+
// if (n <= 0) {
5+
// return false;
6+
// }
7+
8+
// return (n & (n - 1)) === 0;
9+
// }
10+
11+
// Time complexity: O(1)
12+
// Space complexity: O(1)
13+
14+
// Test cases
15+
16+
17+
// recursive solution
18+
function isPowerOfTwo(n: number): boolean {
19+
if (n <= 0) { // edge case for negative numbers
20+
return false;
21+
}
22+
if (n % 2 !== 0) { // edge case for odd numbers
23+
return false;
24+
}
25+
26+
if (n === 1) { // base case
27+
return true;
28+
}
29+
30+
return isPowerOfTwo(n / 2);
31+
}
32+
33+
// Time complexity: O(log n)
34+
// Space complexity: O(log n)
35+
36+
// Test cases
37+
console.log(isPowerOfTwo(1)); // true
38+
console.log(isPowerOfTwo(16)); // true
39+
console.log(isPowerOfTwo(3)); // false
40+
console.log(isPowerOfTwo(4)); // true
41+
console.log(isPowerOfTwo(5)); // false
42+
console.log(isPowerOfTwo(0)); // false
43+
console.log(isPowerOfTwo(-1)); // false

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /