From 2d59f57dbbf089a99603c1e814a2c773adc65632 Mon Sep 17 00:00:00 2001 From: autoprettier Date: Mon, 7 Oct 2024 09:05:57 +0000 Subject: [PATCH 1/5] Update DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 185bae95..672b13ea 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -156,6 +156,8 @@ ## Search * [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/binary_search.ts) + * [Exponential Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/exponential_search.ts) + * [Fibonacci Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/fibonacci_search.ts) * [Interpolation Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/interpolation_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) From 1cc65e6bf3f67486a75881c47d90b2fb39b5c6f4 Mon Sep 17 00:00:00 2001 From: IcarusTheFly Date: Mon, 7 Oct 2024 20:26:12 +1100 Subject: [PATCH 2/5] Add Generate Permutations backtracking algorithm --- backtracking/GeneratePermutations.ts | 38 ++++++++++++++++++ .../test/GeneratePermutations.test.ts | 39 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 backtracking/GeneratePermutations.ts create mode 100644 backtracking/test/GeneratePermutations.test.ts diff --git a/backtracking/GeneratePermutations.ts b/backtracking/GeneratePermutations.ts new file mode 100644 index 00000000..c6196574 --- /dev/null +++ b/backtracking/GeneratePermutations.ts @@ -0,0 +1,38 @@ +/* + * Problem Statement: Generate all distinct permutations of an array (all permutations should be in sorted order); + * + * What is permutations? + * - Permutation means possible arrangements in a set (here it is an array); + * + * Reference to know more about permutations: + * - https://www.britannica.com/science/permutation + * + */ + +const swap = (arr: T[], i: number, j: number): T[] => { + const newArray: T[] = [...arr] + + const temp: T = newArray[i] + newArray[i] = newArray[j] + newArray[j] = temp + + return newArray +} + +const permutations = (arr: T[]): T[][] => { + const P: T[][] = [] + const permute = (arr: T[], low: number, high: number): T[][] => { + if (low === high) { + P.push([...arr]) + return P + } + for (let i = low; i <= high; i++) { + arr = swap(arr, low, i) + permute(arr, low + 1, high) + } + return P + } + return permute(arr, 0, arr.length - 1) +} + +export { permutations } diff --git a/backtracking/test/GeneratePermutations.test.ts b/backtracking/test/GeneratePermutations.test.ts new file mode 100644 index 00000000..dc0947a2 --- /dev/null +++ b/backtracking/test/GeneratePermutations.test.ts @@ -0,0 +1,39 @@ +import { permutations } from '../GeneratePermutations' + +const factorial = (n: number): number => { + if (n === 0 || n === 1) { + return 1 + } + return n * factorial(n - 1) +} + +describe('Permutations', () => { + it('Permutations of [a]', () => { + const perms = permutations(['a']) + expect(perms).toHaveLength(factorial(1)) + expect(perms).toContainEqual(['a']) + }) + + it('Permutations of [true, false]', () => { + const perms = permutations([true, false]) + expect(perms).toHaveLength(factorial(2)) + expect(perms).toContainEqual([true, false]) + expect(perms).toContainEqual([false, true]) + }) + + it('Permutations of [1, 2, 3]', () => { + const perms = permutations([1, 2, 3]) + expect(perms).toHaveLength(factorial(3)) + expect(perms).toContainEqual([1, 2, 3]) + expect(perms).toContainEqual([1, 3, 2]) + expect(perms).toContainEqual([2, 1, 3]) + expect(perms).toContainEqual([2, 3, 1]) + expect(perms).toContainEqual([3, 1, 2]) + expect(perms).toContainEqual([3, 2, 1]) + }) + + it('Permutation counts across larger input arrays', () => { + expect(permutations([1, 2, 3, 4, 5, 6, 7, 8])).toHaveLength(factorial(8)) + expect(permutations([1, 2, 3, 4, 5, 6, 7, 8, 9])).toHaveLength(factorial(9)) + }) +}) From ffea122dbca102843ff3726dfa72105f2b8157f6 Mon Sep 17 00:00:00 2001 From: autoprettier Date: Mon, 7 Oct 2024 09:27:09 +0000 Subject: [PATCH 3/5] Formatting filenames 1cc65e6b --- backtracking/{GeneratePermutations.ts => generatepermutations.ts} | 0 ...{GeneratePermutations.test.ts => generatepermutations.test.ts} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename backtracking/{GeneratePermutations.ts => generatepermutations.ts} (100%) rename backtracking/test/{GeneratePermutations.test.ts => generatepermutations.test.ts} (100%) diff --git a/backtracking/GeneratePermutations.ts b/backtracking/generatepermutations.ts similarity index 100% rename from backtracking/GeneratePermutations.ts rename to backtracking/generatepermutations.ts diff --git a/backtracking/test/GeneratePermutations.test.ts b/backtracking/test/generatepermutations.test.ts similarity index 100% rename from backtracking/test/GeneratePermutations.test.ts rename to backtracking/test/generatepermutations.test.ts From 878d4af923279733714581448e0d8b34846c7a33 Mon Sep 17 00:00:00 2001 From: autoprettier Date: Mon, 7 Oct 2024 09:27:09 +0000 Subject: [PATCH 4/5] Update DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 672b13ea..066d27f8 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -2,9 +2,11 @@ ## 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) + * [Generatepermutations](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/generatepermutations.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) + * [Generatepermutations.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/generatepermutations.test.ts) ## Bit Manipulation * [Add Binary](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/add_binary.ts) From d554c842e2de47224fc7996dd9338ebc3d13054e Mon Sep 17 00:00:00 2001 From: IcarusTheFly Date: Mon, 7 Oct 2024 20:31:02 +1100 Subject: [PATCH 5/5] Rename reference --- backtracking/test/GeneratePermutations.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/test/GeneratePermutations.test.ts b/backtracking/test/GeneratePermutations.test.ts index dc0947a2..27cf52d2 100644 --- a/backtracking/test/GeneratePermutations.test.ts +++ b/backtracking/test/GeneratePermutations.test.ts @@ -1,4 +1,4 @@ -import { permutations } from '../GeneratePermutations' +import { permutations } from '../generatepermutations' const factorial = (n: number): number => { if (n === 0 || n === 1) {