Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -156,6 +158,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)
Expand Down
38 changes: 38 additions & 0 deletions backtracking/generatepermutations.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(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 = <T>(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 }
39 changes: 39 additions & 0 deletions backtracking/test/generatepermutations.test.ts
Original file line number Diff line number Diff line change
@@ -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))
})
})
Loading