Skip to content

Add Backtracking / Generate permutations #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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