From 2070cdf38eca7829b8dfa5f48f2efada9bbe04fd Mon Sep 17 00:00:00 2001 From: autoprettier Date: Sun, 16 Jul 2023 16:39:18 +0000 Subject: [PATCH 1/8] Update DIRECTORY.md --- DIRECTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index ce0e7c51..5f4e6ef0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -58,11 +58,17 @@ ## Graph * [Bellman Ford](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bellman_ford.ts) * [Dijkstra](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/dijkstra.ts) + * [Floyd Warshall](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/floyd_warshall.ts) + * [Johnson](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/johnson.ts) * [Kruskal](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/kruskal.ts) + * [Prim](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/prim.ts) * Test * [Bellman Ford.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bellman_ford.test.ts) * [Dijkstra.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/dijkstra.test.ts) + * [Floyd Warshall.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/floyd_warshall.test.ts) + * [Johnson.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/johnson.test.ts) * [Kruskal.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/kruskal.test.ts) + * [Prim.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/prim.test.ts) ## Maths * [Absolute Value](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/absolute_value.ts) From 5de5e97d0922aa229e88a71f9d272ef85fbb3ef1 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Sun, 16 Jul 2023 22:17:46 +0530 Subject: [PATCH 2/8] =?UTF-8?q?Added=20ugly=20numbers=20=F0=9F=A7=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DIRECTORY.md | 1 + maths/test/ugly_numbers.test.ts | 5 +++++ maths/ugly_numbers.ts | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 maths/test/ugly_numbers.test.ts create mode 100644 maths/ugly_numbers.ts diff --git a/DIRECTORY.md b/DIRECTORY.md index 5f4e6ef0..4a96f28e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -108,6 +108,7 @@ * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts) * [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts) * [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts) + * [Ugly Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/ugly_numbers.ts) ## Other * [Parse Nested Brackets](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/parse_nested_brackets.ts) diff --git a/maths/test/ugly_numbers.test.ts b/maths/test/ugly_numbers.test.ts new file mode 100644 index 00000000..3f8dac08 --- /dev/null +++ b/maths/test/ugly_numbers.test.ts @@ -0,0 +1,5 @@ +import { UglyNumbers } from '../ugly_numbers'; + +test.each([[1, 1], [7, 8], [11, 15]])('Ugly Numbers', (number: number, result: number) => { + expect(UglyNumbers(number)).toBe(result) +}) \ No newline at end of file diff --git a/maths/ugly_numbers.ts b/maths/ugly_numbers.ts new file mode 100644 index 00000000..438902be --- /dev/null +++ b/maths/ugly_numbers.ts @@ -0,0 +1,39 @@ +/** + * @function UglyNumbers + * @description Returns the nth ugly number + * @summary Ugly numbers are natural numbers whose only prime factors are 2, 3 and 5. + * They can be represented in the form 2^a * 3^b * 5*c. By convention, 1 is also considered to be + * an ugly number. + * The first few terms of the sequence are: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20... + * + * For the provided n, the nth ugly number shall be computed. + * @param n The nth ugly number to find + * @returns {number} The nth ugly number + * @see [GeeksForGeeks](https://www.geeksforgeeks.org/ugly-numbers/) + * @example UglyNumbers(1) = 1 + * @example UglyNumbers(7) = 8 + */ +const UglyNumbers = (n: number) => { + const uglyNums = [1]; + let idx2 = 0; + let idx3 = 0; + let idx5 = 0; + let nextx2: number, nextx3: number, nextx5: number, nextNum: number; + + while (uglyNums.length < n) { + nextx2 = uglyNums[idx2] * 2; + nextx3 = uglyNums[idx3] * 3; + nextx5 = uglyNums[idx5] * 5; + nextNum = Math.min(nextx2, nextx3, nextx5); + + uglyNums.push(nextNum); + + if (nextx2 === nextNum) idx2++; + if (nextx3 === nextNum) idx3++; + if (nextx5 === nextNum) idx5++; + } + + return uglyNums[n - 1]; +} + +export { UglyNumbers } \ No newline at end of file From ced3e65ee6e97838a0aaf9f0f7f09d0e52d05739 Mon Sep 17 00:00:00 2001 From: autoprettier Date: Sun, 16 Jul 2023 16:48:04 +0000 Subject: [PATCH 3/8] Update DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 4a96f28e..49fc8bdf 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -107,8 +107,8 @@ * [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts) * [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts) - * [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts) * [Ugly Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/ugly_numbers.ts) + * [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts) ## Other * [Parse Nested Brackets](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/parse_nested_brackets.ts) From e50adb046a17572ac7dac4333b10cc7d42baa96a Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Tue, 3 Oct 2023 18:02:20 +0530 Subject: [PATCH 4/8] =?UTF-8?q?=F0=9F=9A=80=20feat:=20use=20generator=20fo?= =?UTF-8?q?r=20ugly=20nums?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maths/test/ugly_numbers.test.ts | 9 ++++++- maths/ugly_numbers.ts | 45 +++++++++++++++------------------ 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/maths/test/ugly_numbers.test.ts b/maths/test/ugly_numbers.test.ts index 3f8dac08..c8dea07b 100644 --- a/maths/test/ugly_numbers.test.ts +++ b/maths/test/ugly_numbers.test.ts @@ -1,5 +1,12 @@ import { UglyNumbers } from '../ugly_numbers'; test.each([[1, 1], [7, 8], [11, 15]])('Ugly Numbers', (number: number, result: number) => { - expect(UglyNumbers(number)).toBe(result) + const uglyNumber = UglyNumbers(); + + for(let i = 0; i < (number - 1); i++) + uglyNumber.next(); + + const e = uglyNumber.next().value; + console.log(e); + expect(e).toBe(result); }) \ No newline at end of file diff --git a/maths/ugly_numbers.ts b/maths/ugly_numbers.ts index 438902be..5b7f60af 100644 --- a/maths/ugly_numbers.ts +++ b/maths/ugly_numbers.ts @@ -1,39 +1,36 @@ /** - * @function UglyNumbers - * @description Returns the nth ugly number + * @generator + * @description Generates ugly numbers * @summary Ugly numbers are natural numbers whose only prime factors are 2, 3 and 5. * They can be represented in the form 2^a * 3^b * 5*c. By convention, 1 is also considered to be * an ugly number. * The first few terms of the sequence are: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20... * * For the provided n, the nth ugly number shall be computed. - * @param n The nth ugly number to find - * @returns {number} The nth ugly number * @see [GeeksForGeeks](https://www.geeksforgeeks.org/ugly-numbers/) - * @example UglyNumbers(1) = 1 - * @example UglyNumbers(7) = 8 */ -const UglyNumbers = (n: number) => { - const uglyNums = [1]; - let idx2 = 0; - let idx3 = 0; - let idx5 = 0; - let nextx2: number, nextx3: number, nextx5: number, nextNum: number; +function* UglyNumbers() { + yield 1 - while (uglyNums.length < n) { - nextx2 = uglyNums[idx2] * 2; - nextx3 = uglyNums[idx3] * 3; - nextx5 = uglyNums[idx5] * 5; - nextNum = Math.min(nextx2, nextx3, nextx5); + let idx2 = 0, idx3 = 0, idx5 = 0 + const uglyNumbers = [1] - uglyNums.push(nextNum); + let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number - if (nextx2 === nextNum) idx2++; - if (nextx3 === nextNum) idx3++; - if (nextx5 === nextNum) idx5++; - } + while(true) { + nextx2 = uglyNumbers[idx2] * 2 + nextx3 = uglyNumbers[idx3] * 3 + nextx5 = uglyNumbers[idx5] * 5 + + nextUglyNum = Math.min(nextx2, nextx3, nextx5) + yield nextUglyNum - return uglyNums[n - 1]; + if(nextx2 === nextUglyNum) idx2++ + if(nextx3 === nextUglyNum) idx3++ + if(nextx5 === nextUglyNum) idx5++ + + uglyNumbers.push(nextUglyNum) + } } -export { UglyNumbers } \ No newline at end of file +export { UglyNumbers } From a02c1c9fcf9f8cd4efba18b0cddcf6b519d2e4ef Mon Sep 17 00:00:00 2001 From: autoprettier Date: Tue, 3 Oct 2023 12:34:26 +0000 Subject: [PATCH 5/8] Formatting filenames ad9848e0 --- ...ll-combinations-of-size-k.ts => all_combinations_of_size_k.ts} | 0 backtracking/{generateParentheses.ts => generateparentheses.ts} | 0 ...tions-of-size-k.test.ts => all_combinations_of_size_k.test.ts} | 0 .../{generateParentheses.test.ts => generateparentheses.test.ts} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename backtracking/{all-combinations-of-size-k.ts => all_combinations_of_size_k.ts} (100%) rename backtracking/{generateParentheses.ts => generateparentheses.ts} (100%) rename backtracking/test/{all-combinations-of-size-k.test.ts => all_combinations_of_size_k.test.ts} (100%) rename backtracking/test/{generateParentheses.test.ts => generateparentheses.test.ts} (100%) diff --git a/backtracking/all-combinations-of-size-k.ts b/backtracking/all_combinations_of_size_k.ts similarity index 100% rename from backtracking/all-combinations-of-size-k.ts rename to backtracking/all_combinations_of_size_k.ts diff --git a/backtracking/generateParentheses.ts b/backtracking/generateparentheses.ts similarity index 100% rename from backtracking/generateParentheses.ts rename to backtracking/generateparentheses.ts diff --git a/backtracking/test/all-combinations-of-size-k.test.ts b/backtracking/test/all_combinations_of_size_k.test.ts similarity index 100% rename from backtracking/test/all-combinations-of-size-k.test.ts rename to backtracking/test/all_combinations_of_size_k.test.ts diff --git a/backtracking/test/generateParentheses.test.ts b/backtracking/test/generateparentheses.test.ts similarity index 100% rename from backtracking/test/generateParentheses.test.ts rename to backtracking/test/generateparentheses.test.ts From e729770f2152f0050d23f20fe4264a0655b024aa Mon Sep 17 00:00:00 2001 From: autoprettier Date: Tue, 3 Oct 2023 12:34:26 +0000 Subject: [PATCH 6/8] Update DIRECTORY.md --- DIRECTORY.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 49fc8bdf..3237c4e5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1,4 +1,18 @@ +## Backtracking + * [All Combinations Of Size K](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/all_combinations_of_size_k.ts) + * [Generateparentheses](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/generateparentheses.ts) + * Test + * [All Combinations Of Size K.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/all_combinations_of_size_k.test.ts) + * [Generateparentheses.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/generateparentheses.test.ts) + +## Bit Manipulation + * [Is Power Of 2](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_2.ts) + * [Is Power Of 4](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_4.ts) + * Test + * [Is Power Of 2.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_2.test.ts) + * [Is Power Of 4.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_4.test.ts) + ## Ciphers * [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts) @@ -51,6 +65,9 @@ * [Binary Search Tree](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tree/binary_search_tree.ts) * Test * [Binary Search Tree.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tree/test/binary_search_tree.test.ts) + * Tries + * [Tries.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tries/test/tries.test.ts) + * [Tries](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tries/tries.ts) ## Dynamic Programming * [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts) @@ -111,17 +128,24 @@ * [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts) ## Other + * [Is Sorted Array](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/is_sorted_array.ts) * [Parse Nested Brackets](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/parse_nested_brackets.ts) + * [Shuffle Array](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/shuffle_array.ts) * Test + * [Is Sorted Array.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/test/is_sorted_array.test.ts) * [Parse Nested Brackets.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/test/parse_nested_brackets.test.ts) + * [Shuffle Array.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/test/shuffle_array.test.ts) ## Search * [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/binary_search.ts) * [Jump Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/jump_search.ts) * [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/linear_search.ts) + * [Sentinel Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/sentinel_search.ts) ## Sorts + * [Bogo Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/bogo_sort.ts) * [Bubble Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/bubble_sort.ts) + * [Counting Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/counting_sort.ts) * [Cycle Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/cycle_sort.ts) * [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/gnome_sort.ts) * [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/insertion_sort.ts) From 2b023a40eb2ef474c5bbbf766147a3f768b41ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Tue, 3 Oct 2023 15:03:02 +0200 Subject: [PATCH 7/8] Test first 11 ugly numbers --- maths/test/ugly_numbers.test.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/maths/test/ugly_numbers.test.ts b/maths/test/ugly_numbers.test.ts index c8dea07b..dd3d1648 100644 --- a/maths/test/ugly_numbers.test.ts +++ b/maths/test/ugly_numbers.test.ts @@ -1,12 +1,6 @@ import { UglyNumbers } from '../ugly_numbers'; -test.each([[1, 1], [7, 8], [11, 15]])('Ugly Numbers', (number: number, result: number) => { - const uglyNumber = UglyNumbers(); - - for(let i = 0; i < (number - 1); i++) - uglyNumber.next(); - - const e = uglyNumber.next().value; - console.log(e); - expect(e).toBe(result); -}) \ No newline at end of file +test('Ugly Numbers', () => { + const uglyNumbers = UglyNumbers(); + expect(Array(11).map((_, i) => uglyNumbers.next())).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]); +}) From 2c10537a9f0307315dee018d5e54893302b3f3d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Tue, 3 Oct 2023 15:08:51 +0200 Subject: [PATCH 8/8] fix oopsie --- maths/test/ugly_numbers.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/test/ugly_numbers.test.ts b/maths/test/ugly_numbers.test.ts index dd3d1648..d25633dc 100644 --- a/maths/test/ugly_numbers.test.ts +++ b/maths/test/ugly_numbers.test.ts @@ -2,5 +2,5 @@ import { UglyNumbers } from '../ugly_numbers'; test('Ugly Numbers', () => { const uglyNumbers = UglyNumbers(); - expect(Array(11).map((_, i) => uglyNumbers.next())).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]); + expect(Array(11).fill(undefined).map(() => uglyNumbers.next())).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]); })