Skip to content

Commit 973b92e

Browse files
authored
Merge branch 'master' into docs/remove_whitespaces
2 parents 8113797 + 90212fa commit 973b92e

File tree

94 files changed

+1867
-291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1867
-291
lines changed

.gitpod.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tasks:
2+
- init: |
3+
npm install && npm test
4+
5+

DIRECTORY.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11

2+
## Backtracking
3+
* [All Combinations Of Size K](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/all_combinations_of_size_k.ts)
4+
* [Generateparentheses](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/generateparentheses.ts)
5+
* Test
6+
* [All Combinations Of Size K.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/all_combinations_of_size_k.test.ts)
7+
* [Generateparentheses.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/generateparentheses.test.ts)
8+
9+
## Bit Manipulation
10+
* [Add Binary](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/add_binary.ts)
11+
* [Is Power Of 2](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_2.ts)
12+
* [Is Power Of 4](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_4.ts)
13+
* [Log Two](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/log_two.ts)
14+
* Test
15+
* [Add Binary.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/add_binary.test.ts)
16+
* [Is Power Of 2.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_2.test.ts)
17+
* [Is Power Of 4.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_4.test.ts)
18+
* [Log Two.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/log_two.test.ts)
19+
220
## Ciphers
321
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts)
422

@@ -10,8 +28,7 @@
1028
* Heap
1129
* [Heap](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/heap.ts)
1230
* Test
13-
* [Max Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/max_heap.test.ts)
14-
* [Min Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/min_heap.test.ts)
31+
* [Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/heap.test.ts)
1532
* List
1633
* [Doubly Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/doubly_linked_list.ts)
1734
* [Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/linked_list.ts)
@@ -51,18 +68,30 @@
5168
* [Binary Search Tree](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tree/binary_search_tree.ts)
5269
* Test
5370
* [Binary Search Tree.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tree/test/binary_search_tree.test.ts)
71+
* Tries
72+
* [Tries.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tries/test/tries.test.ts)
73+
* [Tries](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tries/tries.ts)
5474

5575
## Dynamic Programming
5676
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts)
77+
* [Lcs](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/lcs.ts)
5778

5879
## Graph
5980
* [Bellman Ford](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bellman_ford.ts)
81+
* [Bipartite Graph](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bipartite_graph.ts)
6082
* [Dijkstra](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/dijkstra.ts)
83+
* [Floyd Warshall](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/floyd_warshall.ts)
84+
* [Johnson](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/johnson.ts)
6185
* [Kruskal](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/kruskal.ts)
86+
* [Prim](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/prim.ts)
6287
* Test
6388
* [Bellman Ford.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bellman_ford.test.ts)
89+
* [Bipartite Graph.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bipartite_graph.test.ts)
6490
* [Dijkstra.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/dijkstra.test.ts)
91+
* [Floyd Warshall.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/floyd_warshall.test.ts)
92+
* [Johnson.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/johnson.test.ts)
6593
* [Kruskal.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/kruskal.test.ts)
94+
* [Prim.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/prim.test.ts)
6695

6796
## Maths
6897
* [Absolute Value](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/absolute_value.ts)
@@ -78,6 +107,7 @@
78107
* [Factors](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factors.ts)
79108
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/fibonacci.ts)
80109
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/find_min.ts)
110+
* [Gaussian Elimination](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/gaussian_elimination.ts)
81111
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/greatest_common_factor.ts)
82112
* [Hamming Distance](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/hamming_distance.ts)
83113
* [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_divisible.ts)
@@ -88,6 +118,7 @@
88118
* [Is Square Free](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_square_free.ts)
89119
* [Juggler Sequence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/juggler_sequence.ts)
90120
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts)
121+
* [Matrix Multiplication](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/matrix_multiplication.ts)
91122
* [Number Of Digits](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/number_of_digits.ts)
92123
* [Pascals Triangle](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pascals_triangle.ts)
93124
* [Perfect Cube](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_cube.ts)
@@ -101,25 +132,38 @@
101132
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts)
102133
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts)
103134
* [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts)
135+
* [Square Root](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/square_root.ts)
136+
* [Ugly Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/ugly_numbers.ts)
104137
* [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts)
105138

106139
## Other
140+
* [Is Sorted Array](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/is_sorted_array.ts)
107141
* [Parse Nested Brackets](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/parse_nested_brackets.ts)
142+
* [Shuffle Array](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/shuffle_array.ts)
108143
* Test
144+
* [Is Sorted Array.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/test/is_sorted_array.test.ts)
109145
* [Parse Nested Brackets.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/test/parse_nested_brackets.test.ts)
146+
* [Shuffle Array.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/test/shuffle_array.test.ts)
110147

111148
## Search
112149
* [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/binary_search.ts)
150+
* [Interpolation Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/interpolation_search.ts)
113151
* [Jump Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/jump_search.ts)
114152
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/linear_search.ts)
153+
* [Sentinel Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/sentinel_search.ts)
115154

116155
## Sorts
156+
* [Bogo Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/bogo_sort.ts)
117157
* [Bubble Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/bubble_sort.ts)
158+
* [Counting Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/counting_sort.ts)
118159
* [Cycle Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/cycle_sort.ts)
119160
* [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/gnome_sort.ts)
161+
* [Heap Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/heap_sort.ts)
120162
* [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/insertion_sort.ts)
121163
* [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/merge_sort.ts)
122164
* [Quick Select](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_select.ts)
123165
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_sort.ts)
124166
* [Selection Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/selection_sort.ts)
125167
* [Shell Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/shell_sort.ts)
168+
* [Swap Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/swap_sort.ts)
169+
* [Tree Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/tree_sort.ts)

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ TypeScript Repository of TheAlgorithms, which implements various algorithms and
99

1010
[![Contributions Welcome][welcome]](CONTRIBUTING.md)
1111
[![Discord chat][chat]][discord-server]
12-
13-
12+
<a href="https://gitpod.io/#https://github.com/TheAlgorithms/TypeScript">
13+
<img src="https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod&style=flat-square" height="20" alt="Gitpod Ready-to-Code">
14+
</a>
1415
</div>
1516

1617
---

backtracking/generateparentheses.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Given a number n pairs of parentheses, generate all combinations of valid parentheses
3+
* @param {number} n: Number of given parentheses
4+
* @return {string[]} result: Array that contains all valid parentheses
5+
* @see https://leetcode.com/problems/generate-parentheses/
6+
*/
7+
8+
const generateParentheses = (n: number): string[] => {
9+
const result: string[] = [];
10+
11+
const solve = (chars: string, openParentheses: number, closedParentheses: number) => {
12+
if (openParentheses === n && closedParentheses === n) {
13+
result.push(chars);
14+
return;
15+
}
16+
17+
if (openParentheses <= n) {
18+
solve(chars + "(", openParentheses + 1, closedParentheses);
19+
}
20+
21+
if (closedParentheses < openParentheses) {
22+
solve(chars + ")", openParentheses, closedParentheses + 1);
23+
}
24+
};
25+
26+
solve("", 0, 0);
27+
28+
return result;
29+
};
30+
31+
export { generateParentheses };

backtracking/test/all-combinations-of-size-k.test.ts renamed to backtracking/test/all_combinations_of_size_k.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { generateCombinations } from "../all-combinations-of-size-k";
1+
import { generateCombinations } from "../all_combinations_of_size_k";
22

33
const cases = [
44
[
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { generateParentheses } from "../generateparentheses";
2+
3+
const cases: [number, string[]][] = [
4+
[0, [""]],
5+
[1, ["()"]],
6+
[2, ["(())", "()()"]],
7+
[3, ["((()))", "(()())", "(())()", "()(())", "()()()"]],
8+
[
9+
4,
10+
[
11+
"(((())))",
12+
"((()()))",
13+
"((())())",
14+
"((()))()",
15+
"(()(()))",
16+
"(()()())",
17+
"(()())()",
18+
"(())(())",
19+
"(())()()",
20+
"()((()))",
21+
"()(()())",
22+
"()(())()",
23+
"()()(())",
24+
"()()()()",
25+
],
26+
],
27+
[
28+
5,
29+
[
30+
"((((()))))",
31+
"(((()())))",
32+
"(((())()))",
33+
"(((()))())",
34+
"(((())))()",
35+
"((()(())))",
36+
"((()()()))",
37+
"((()())())",
38+
"((()()))()",
39+
"((())(()))",
40+
"((())()())",
41+
"((())())()",
42+
"((()))(())",
43+
"((()))()()",
44+
"(()((())))",
45+
"(()(()()))",
46+
"(()(())())",
47+
"(()(()))()",
48+
"(()()(()))",
49+
"(()()()())",
50+
"(()()())()",
51+
"(()())(())",
52+
"(()())()()",
53+
"(())((()))",
54+
"(())(()())",
55+
"(())(())()",
56+
"(())()(())",
57+
"(())()()()",
58+
"()(((())))",
59+
"()((()()))",
60+
"()((())())",
61+
"()((()))()",
62+
"()(()(()))",
63+
"()(()()())",
64+
"()(()())()",
65+
"()(())(())",
66+
"()(())()()",
67+
"()()((()))",
68+
"()()(()())",
69+
"()()(())()",
70+
"()()()(())",
71+
"()()()()()",
72+
],
73+
],
74+
];
75+
76+
describe("Generate Parentheses", () => {
77+
test.each(cases)(
78+
"generate all valid parentheses of input %n",
79+
(n: number, expected: string[]) => {
80+
expect(generateParentheses(n)).toStrictEqual(expected);
81+
}
82+
);
83+
});

bit_manipulation/add_binary.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Adds two binary strings and returns the result as a binary string.
3+
*
4+
* @param firstBinaryNo - The first binary string.
5+
* @param secondBinaryNo - The second binary string.
6+
* @returns The binary sum of the input strings.
7+
*/
8+
export function addBinary(firstBinaryNo: string, secondBinaryNo: string): string {
9+
let lengthOfFirstNumber: number = firstBinaryNo.length - 1;
10+
let lengthOfSecondNumber: number = secondBinaryNo.length - 1;
11+
let solution: string[] = [];
12+
let carry: number = 0;
13+
14+
while ( lengthOfFirstNumber >= 0 || lengthOfSecondNumber >= 0) {
15+
let sum: number = carry;
16+
if (lengthOfFirstNumber >= 0) sum += parseInt(firstBinaryNo.charAt(lengthOfFirstNumber));
17+
if (lengthOfSecondNumber >= 0) sum += parseInt(secondBinaryNo.charAt(lengthOfSecondNumber));
18+
solution.push((sum % 2).toString());
19+
carry = Math.floor(sum / 2);
20+
lengthOfFirstNumber--;
21+
lengthOfSecondNumber--;
22+
}
23+
24+
if (carry !== 0) solution.push(carry.toString());
25+
26+
return solution.reverse().join('');
27+
}

bit_manipulation/log_two.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @author dev-madhurendra <https://github.com/dev-madhurendra>
3+
* @see https://handwiki.org/wiki/Binary_logarithm
4+
* Approximate log2 using bitwise operators
5+
* @param {number} n
6+
* @returns {number} Log2 approximation equal to floor(log2(n))
7+
*/
8+
export const logTwo = (n: number): number => {
9+
let result = 0
10+
while (n >> 1) {
11+
n >>= 1
12+
result++
13+
}
14+
return result
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { addBinary } from "../add_binary";
2+
3+
describe('Add Binary Number', () => {
4+
it('should add two binary numbers with no carry', () => {
5+
const result = addBinary('1101', '1011');
6+
expect(result).toBe('11000');
7+
});
8+
9+
it('should add two binary numbers with carry', () => {
10+
const result = addBinary('1111', '1111');
11+
expect(result).toBe('11110');
12+
});
13+
14+
it('should add two different-length binary numbers', () => {
15+
const result = addBinary('1101', '111');
16+
expect(result).toBe('10100');
17+
});
18+
19+
it('should add two empty binary numbers', () => {
20+
const result = addBinary('', '');
21+
expect(result).toBe('');
22+
});
23+
24+
it('should add one empty binary number to a non-empty number', () => {
25+
const result = addBinary('1010', '');
26+
expect(result).toBe('1010');
27+
});
28+
29+
it('should add one non-empty binary number to an empty number', () => {
30+
const result = addBinary('', '1101');
31+
expect(result).toBe('1101');
32+
});
33+
});

bit_manipulation/test/log_two.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { logTwo } from "../log_two"
2+
3+
describe('LogTwoTests' , () => {
4+
test.each([...Array(100).keys()].map(i => [i + 1]))(
5+
'log2(%i)',
6+
(input) => {
7+
expect(logTwo(input)).toBe(Math.floor(Math.log2(input)));
8+
}
9+
);
10+
})

0 commit comments

Comments
 (0)