diff --git a/DIRECTORY.md b/DIRECTORY.md index ce0e7c51..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) @@ -58,11 +75,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) @@ -101,20 +124,28 @@ * [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) + * [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 + * [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) 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 diff --git a/maths/test/ugly_numbers.test.ts b/maths/test/ugly_numbers.test.ts new file mode 100644 index 00000000..d25633dc --- /dev/null +++ b/maths/test/ugly_numbers.test.ts @@ -0,0 +1,6 @@ +import { UglyNumbers } from '../ugly_numbers'; + +test('Ugly Numbers', () => { + const uglyNumbers = UglyNumbers(); + expect(Array(11).fill(undefined).map(() => uglyNumbers.next())).toEqual([1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15]); +}) diff --git a/maths/ugly_numbers.ts b/maths/ugly_numbers.ts new file mode 100644 index 00000000..5b7f60af --- /dev/null +++ b/maths/ugly_numbers.ts @@ -0,0 +1,36 @@ +/** + * @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. + * @see [GeeksForGeeks](https://www.geeksforgeeks.org/ugly-numbers/) + */ +function* UglyNumbers() { + yield 1 + + let idx2 = 0, idx3 = 0, idx5 = 0 + const uglyNumbers = [1] + + let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number + + while(true) { + nextx2 = uglyNumbers[idx2] * 2 + nextx3 = uglyNumbers[idx3] * 3 + nextx5 = uglyNumbers[idx5] * 5 + + nextUglyNum = Math.min(nextx2, nextx3, nextx5) + yield nextUglyNum + + if(nextx2 === nextUglyNum) idx2++ + if(nextx3 === nextUglyNum) idx3++ + if(nextx5 === nextUglyNum) idx5++ + + uglyNumbers.push(nextUglyNum) + } +} + +export { UglyNumbers }