Skip to content

Commit ecc6a9b

Browse files
authored
Merge branch 'master' into add-double-factorial
2 parents 8d3f5c3 + 26ac72d commit ecc6a9b

File tree

115 files changed

+1702
-419
lines changed

Some content is hidden

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

115 files changed

+1702
-419
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: upload_coverage_report
3+
4+
'on':
5+
workflow_dispatch:
6+
push:
7+
branches:
8+
- master
9+
pull_request:
10+
11+
jobs:
12+
upload_coverage_report:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- uses: actions/setup-node@v4
18+
with:
19+
node-version: "18.x"
20+
21+
- name: Install dependencies
22+
run: npm ci
23+
24+
- name: Generate coverage report
25+
run: npm test -- --coverage
26+
27+
- name: Upload coverage to codecov
28+
uses: codecov/codecov-action@v3
29+
with:
30+
files: "coverage/coverage-final.json"
31+
fail_ci_if_error: true
32+
...

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ yarn-error.log*
1414

1515
# intelliJ workspace folder
1616
.idea
17+
18+
/coverage

.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+

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ should add unique value.
6363
#### Commit Messages Formatting
6464

6565
- Prefer to use the [Conventional Commits](https://www.conventionalcommits.org/) format: `<type>: <short summary>`. If necessary, put any extra information in the description.
66-
- Commit types include (but are not limited to):
66+
- Commit types include (but are not limited to):
6767
- **docs**: Documentation only changes
6868
- **feat**: A new feature
6969
- **fix**: A bug fix
7070
- **test**: Adding or fixing tests
7171
- **chore**: CI / code quality / minor quality of life improvements
7272

73-
- **Examples**:
73+
- **Examples**:
7474
- `feat: add quicksort algorithm`
7575
- `chore: fix spelling`
7676
- `fix: improper error message`

DIRECTORY.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
* [Generateparentheses.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/generateparentheses.test.ts)
88

99
## Bit Manipulation
10+
* [Add Binary](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/add_binary.ts)
1011
* [Is Power Of 2](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_2.ts)
1112
* [Is Power Of 4](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_4.ts)
1213
* [Log Two](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/log_two.ts)
1314
* Test
15+
* [Add Binary.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/add_binary.test.ts)
1416
* [Is Power Of 2.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_2.test.ts)
1517
* [Is Power Of 4.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_4.test.ts)
1618
* [Log Two.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/log_two.test.ts)
@@ -26,8 +28,7 @@
2628
* Heap
2729
* [Heap](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/heap.ts)
2830
* Test
29-
* [Max Heap.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/heap/test/max_heap.test.ts)
30-
* [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)
3132
* List
3233
* [Doubly Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/doubly_linked_list.ts)
3334
* [Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/linked_list.ts)
@@ -72,23 +73,30 @@
7273
* [Tries](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tries/tries.ts)
7374

7475
## Dynamic Programming
76+
* [Coin Change](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/coin_change.ts)
7577
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts)
7678
* [Lcs](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/lcs.ts)
7779

7880
## Graph
7981
* [Bellman Ford](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bellman_ford.ts)
82+
* [Bipartite Graph](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bipartite_graph.ts)
8083
* [Dijkstra](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/dijkstra.ts)
8184
* [Floyd Warshall](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/floyd_warshall.ts)
8285
* [Johnson](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/johnson.ts)
86+
* [Kosajaru](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/kosajaru.ts)
8387
* [Kruskal](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/kruskal.ts)
8488
* [Prim](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/prim.ts)
89+
* [Tarjan](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/tarjan.ts)
8590
* Test
8691
* [Bellman Ford.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bellman_ford.test.ts)
92+
* [Bipartite Graph.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bipartite_graph.test.ts)
8793
* [Dijkstra.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/dijkstra.test.ts)
8894
* [Floyd Warshall.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/floyd_warshall.test.ts)
8995
* [Johnson.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/johnson.test.ts)
96+
* [Kosajaru.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/kosajaru.test.ts)
9097
* [Kruskal.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/kruskal.test.ts)
9198
* [Prim.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/prim.test.ts)
99+
* [Tarjan.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/tarjan.test.ts)
92100

93101
## Maths
94102
* [Absolute Value](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/absolute_value.ts)
@@ -101,10 +109,12 @@
101109
* [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/degrees_to_radians.ts)
102110
* [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/digit_sum.ts)
103111
* [Double Factorial Iterative](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/double_factorial_iterative.ts)
112+
* [Euler Totient](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/euler_totient.ts)
104113
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factorial.ts)
105114
* [Factors](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factors.ts)
106115
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/fibonacci.ts)
107116
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/find_min.ts)
117+
* [Gaussian Elimination](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/gaussian_elimination.ts)
108118
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/greatest_common_factor.ts)
109119
* [Hamming Distance](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/hamming_distance.ts)
110120
* [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_divisible.ts)
@@ -115,10 +125,12 @@
115125
* [Is Square Free](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_square_free.ts)
116126
* [Juggler Sequence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/juggler_sequence.ts)
117127
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts)
128+
* [Matrix Multiplication](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/matrix_multiplication.ts)
118129
* [Number Of Digits](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/number_of_digits.ts)
119130
* [Pascals Triangle](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pascals_triangle.ts)
120131
* [Perfect Cube](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_cube.ts)
121132
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts)
133+
* [Prime Factorization](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/prime_factorization.ts)
122134
* [Primes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/primes.ts)
123135
* [Pronic Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pronic_number.ts)
124136
* [Radians To Degrees](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/radians_to_degrees.ts)
@@ -128,6 +140,7 @@
128140
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts)
129141
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts)
130142
* [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts)
143+
* [Square Root](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/square_root.ts)
131144
* [Ugly Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/ugly_numbers.ts)
132145
* [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts)
133146

@@ -142,6 +155,7 @@
142155

143156
## Search
144157
* [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/binary_search.ts)
158+
* [Interpolation Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/interpolation_search.ts)
145159
* [Jump Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/jump_search.ts)
146160
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/linear_search.ts)
147161
* [Sentinel Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/sentinel_search.ts)
@@ -152,10 +166,12 @@
152166
* [Counting Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/counting_sort.ts)
153167
* [Cycle Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/cycle_sort.ts)
154168
* [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/gnome_sort.ts)
169+
* [Heap Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/heap_sort.ts)
155170
* [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/insertion_sort.ts)
156171
* [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/merge_sort.ts)
157172
* [Quick Select](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_select.ts)
158173
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_sort.ts)
159174
* [Selection Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/selection_sort.ts)
160175
* [Shell Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/shell_sort.ts)
161176
* [Swap Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/swap_sort.ts)
177+
* [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/all_combinations_of_size_k.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* and repeat the same process for the next number.
1111
*/
1212
export function generateCombinations(n: number, k: number): number[][] {
13-
let combinationsAcc: number[][] = [];
14-
let currentCombination: number[] = [];
13+
const combinationsAcc: number[][] = [];
14+
const currentCombination: number[] = [];
1515

1616
function generateAllCombos(
1717
n: number,

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+
const 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+
}
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+
});

data_structures/heap/heap.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export abstract class Heap<T> {
5050
}
5151

5252
public extract(): T {
53-
let maxElement = this.heap[0];
53+
const maxElement = this.heap[0];
5454
this.heap[0] = this.heap[this.size() - 1];
5555
this.heap.pop();
5656
this.sinkDown();
@@ -162,8 +162,8 @@ export class PriorityQueue<T> extends MinHeap<T> {
162162
}
163163

164164
protected swap(a: number, b: number) {
165-
let akey = this.keys_index(this.heap[a]);
166-
let bkey = this.keys_index(this.heap[b]);
165+
const akey = this.keys_index(this.heap[a]);
166+
const bkey = this.keys_index(this.heap[b]);
167167
[this.keys[akey], this.keys[bkey]] = [this.keys[bkey], this.keys[akey]];
168168
super.swap(a, b);
169169
}
@@ -188,7 +188,7 @@ export class PriorityQueue<T> extends MinHeap<T> {
188188
this.insert(value);
189189
return;
190190
}
191-
let key = this.keys[idx];
191+
const key = this.keys[idx];
192192
if (this.compare(this.heap[key], value)) {
193193
// Do not do anything if the value in the heap already has a higher priority.
194194
return;

data_structures/heap/test/min_heap.test.ts renamed to data_structures/heap/test/heap.test.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
1-
import { MinHeap, PriorityQueue } from "../heap";
1+
import { MaxHeap, MinHeap, PriorityQueue } from "../heap";
2+
3+
describe("MaxHeap", () => {
4+
let heap: MaxHeap<number>;
5+
const elements: number[] = [
6+
12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18,
7+
];
8+
9+
beforeEach(() => {
10+
heap = new MaxHeap();
11+
for (const element of elements) {
12+
heap.insert(element);
13+
}
14+
});
15+
16+
it("should initialize a heap from input array", () => {
17+
expect(heap.isEmpty()).toEqual(false);
18+
heap.check();
19+
});
20+
21+
it("should remove and return the max element in the heap", () => {
22+
const maxValue = heap.extract();
23+
24+
expect(maxValue).toEqual(81);
25+
heap.check();
26+
});
27+
28+
it("should insert a new element and bubble Up the element to it correct index in the heap", () => {
29+
heap.insert(61);
30+
heap.check();
31+
});
32+
33+
const extract_all = (heap: MaxHeap<number>) => {
34+
[...elements].sort((a, b) => b - a).forEach((element: number) => {
35+
expect(heap.extract()).toEqual(element);
36+
});
37+
heap.check();
38+
expect(heap.size()).toEqual(0);
39+
}
40+
41+
it("should remove and return the max elements in order", () => {
42+
extract_all(heap);
43+
});
44+
45+
it("should insert all, then remove and return the max elements in order", () => {
46+
heap = new MaxHeap();
47+
elements.forEach((element: number) => {
48+
heap.insert(element);
49+
});
50+
heap.check();
51+
expect(heap.size()).toEqual(elements.length);
52+
extract_all(heap);
53+
});
54+
});
255

356
describe("MinHeap", () => {
457
let heap: MinHeap<number>;
@@ -8,7 +61,7 @@ describe("MinHeap", () => {
861

962
beforeEach(() => {
1063
heap = new MinHeap();
11-
for (let element of elements) {
64+
for (const element of elements) {
1265
heap.insert(element);
1366
}
1467
});
@@ -53,7 +106,7 @@ describe("MinHeap", () => {
53106
});
54107

55108
it("should increase priority", () => {
56-
let heap = new PriorityQueue((a: number) => { return a; }, elements.length);
109+
const heap = new PriorityQueue((a: number) => { return a; }, elements.length);
57110
elements.forEach((element: number) => {
58111
heap.insert(element);
59112
});

0 commit comments

Comments
 (0)