Skip to content

Commit 451c1f3

Browse files
committed
add recursive fibonacci and reverse string
1 parent 600215e commit 451c1f3

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, it, expect } from "bun:test";
2+
import { fibonacciIterative, fibonacciRecursive } from "./recursion-fibonacci";
3+
4+
describe('Fibonacci Sequence', () => {
5+
// Test cases for both iterative and recursive implementations
6+
const testCases = [
7+
{ input: 0, expected: 0 },
8+
{ input: 1, expected: 1 },
9+
{ input: 2, expected: 1 },
10+
{ input: 3, expected: 2 },
11+
{ input: 4, expected: 3 },
12+
{ input: 5, expected: 5 },
13+
{ input: 6, expected: 8 },
14+
{ input: 7, expected: 13 },
15+
{ input: 8, expected: 21 },
16+
];
17+
18+
describe('Iterative Fibonacci', () => {
19+
it.each(testCases)('should return $expected for input $input', ({ input, expected }) => {
20+
expect(fibonacciIterative(input)).toBe(expected);
21+
});
22+
23+
it('should handle negative input', () => {
24+
expect(fibonacciIterative(-1)).toBe(-1);
25+
expect(fibonacciIterative(-5)).toBe(-1);
26+
});
27+
});
28+
29+
describe('Recursive Fibonacci', () => {
30+
it.each(testCases)('should return $expected for input $input', ({ input, expected }) => {
31+
expect(fibonacciRecursive(input)).toBe(expected);
32+
});
33+
34+
it('should handle negative input', () => {
35+
expect(fibonacciRecursive(-1)).toBe(-1);
36+
expect(fibonacciRecursive(-5)).toBe(-1);
37+
});
38+
39+
// Note: Be cautious with large inputs due to recursive call overhead
40+
it('should handle larger inputs', () => {
41+
expect(fibonacciRecursive(10)).toBe(55);
42+
});
43+
});
44+
45+
// Performance and edge case tests
46+
describe('Performance and Edge Cases', () => {
47+
it('iterative should be efficient for larger inputs', () => {
48+
const start = performance.now();
49+
const result = fibonacciIterative(20);
50+
const end = performance.now();
51+
52+
expect(result).toBe(6765);
53+
expect(end - start).toBeLessThan(1); // Should complete quickly
54+
});
55+
56+
it('recursive should handle base cases', () => {
57+
expect(fibonacciRecursive(0)).toBe(0);
58+
expect(fibonacciRecursive(1)).toBe(1);
59+
expect(fibonacciRecursive(2)).toBe(1);
60+
});
61+
});
62+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Given a number N return the index value of the Fibonacci sequence, where the sequence is:
2+
3+
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...
4+
// the pattern of the sequence is that each value is the sum of the 2 previous values, that means that for N=5 → 2+3
5+
6+
//For example: fibonacciRecursive(6) should return 8
7+
8+
9+
export function fibonacciIterative(n: number) {
10+
if (n < 0) return -1
11+
if (n == 0) return 0
12+
if (n == 1) return 1
13+
14+
let i = 2;
15+
let first = 0;
16+
let second = 1;
17+
let sum = 1;
18+
while (i <= n) {
19+
sum = first + second // 1, 2, 3, 5, 8
20+
first = second // 1, 1, 2, 3, 5
21+
second = sum // 1, 2, 3, 5, 8
22+
i++ // 3, 4, 5, 6, 7
23+
}
24+
return sum
25+
26+
}
27+
28+
console.log('fibonacciIterative(6)', fibonacciIterative(100));
29+
30+
31+
export function fibonacciRecursive(n: number): number {
32+
33+
// Điểm dừng
34+
if (n < 0) return -1
35+
if (n == 0) return 0
36+
if (n <= 2) return 1
37+
n--
38+
39+
return fibonacciRecursive(n) + fibonacciRecursive(n - 1)
40+
}
41+
// Ta có quy luật như sau: f(n) = f(n-1) + f(n-2). Ví dụ 8 = 5 + 3, 5 = 3 + 2
42+
// Nháp:
43+
//f(6) => f(5) + f(4) = f(4) + f(3) + f(3) + f(2) = f(3) + f(2)
44+
45+
// console.log("fibonacciRecursive", fibonacciRecursive(10));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function reverseString(str: string) {
2+
let result = ""
3+
const length = str.length
4+
for (let i = 0; i < length; i++) {
5+
result += str[length - 1 - i]
6+
}
7+
return result
8+
}
9+
10+
console.log(reverseString("yoyo master"));
11+
12+
function reverseStringRecursive(str: string): string {
13+
14+
// return Điểm dừng
15+
if (str.length == 0) return ""
16+
// Return value (Recursive)
17+
18+
return str[str.length - 1] + reverseStringRecursive(str.slice(0, str.length - 1))
19+
}
20+
21+
// console.log(reverseStringRecursive("yoyo master")) // output: "retsam oyoy"
22+
23+
// Step 1: str = "yoyo master" -> return 'r' + reverseStringRecursive("yoyo maste")
24+
// Step 2: str = "yoyo maste" -> return 'e' + reverseStringRecursive("yoyo mast")

src/algorithm/recursion/reverse-string.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)