Last active
September 29, 2018 18:33
-
-
Save laran/68c763350f594a5a4526af95ec84d4be to your computer and use it in GitHub Desktop.
All permutations of an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"presets": [ | |
"env" | |
], | |
"env": { | |
"test": { | |
"presets": [ | |
"env" | |
] | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Logs | |
logs | |
*.log | |
npm-debug.log* | |
yarn-debug.log* | |
yarn-error.log* | |
# Runtime data | |
pids | |
*.pid | |
*.seed | |
*.pid.lock | |
# Directory for instrumented libs generated by jscoverage/JSCover | |
lib-cov | |
# Coverage directory used by tools like istanbul | |
coverage | |
# nyc test coverage | |
.nyc_output | |
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | |
.grunt | |
# Bower dependency directory (https://bower.io/) | |
bower_components | |
# node-waf configuration | |
.lock-wscript | |
# Compiled binary addons (https://nodejs.org/api/addons.html) | |
build/Release | |
# Dependency directories | |
node_modules/ | |
jspm_packages/ | |
# TypeScript v1 declaration files | |
typings/ | |
# Optional npm cache directory | |
.npm | |
# Optional eslint cache | |
.eslintcache | |
# Optional REPL history | |
.node_repl_history | |
# Output of 'npm pack' | |
*.tgz | |
# Yarn Integrity file | |
.yarn-integrity | |
# dotenv environment variables file | |
.env | |
# next.js build output | |
.next | |
build | |
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "@laran/gists-permutations-js", | |
"version": "1.0.0", | |
"description": "All permutations of an array", | |
"main": "build/permutations.js", | |
"scripts": { | |
"build": "babel permutations.js --out-dir build", | |
"test": "jest", | |
"test:watch": "jest --watchAll" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+ssh://git@gist.github.com/68c763350f594a5a4526af95ec84d4be.git" | |
}, | |
"author": "Laran Evans <laran@laranevans.com> (http://www.laranevans.com/)", | |
"license": "MIT", | |
"bugs": { | |
"url": "https://gist.github.com/68c763350f594a5a4526af95ec84d4be" | |
}, | |
"homepage": "https://gist.github.com/68c763350f594a5a4526af95ec84d4be", | |
"dependencies": { | |
"@laran/readable-helpers": "^1.0.5" | |
}, | |
"jest": { | |
"verbose": true | |
}, | |
"devDependencies": { | |
"babel-cli": "^6.26.0", | |
"babel-preset-env": "^1.7.0", | |
"jest": "^23.6.0", | |
"yarn": "^1.9.4" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {isLessThan, isEqualTo, swap, notExists, isNotArray, isString} from '@laran/readable-helpers'; | |
export default class Permutations { | |
// permute an array of objects | |
static of(a = [], n, permutations = []) { | |
if (isString(a)) { | |
return Permutations.of(a.split(''), 0, permutations); | |
} else { | |
if (isNotArray(a)) { | |
throw "Only arrays can be permuted"; | |
} | |
if (notExists(n)) { // permute the whole array | |
return Permutations.of(a, 0, permutations); | |
} else if(isEqualTo(n, a.length - 1)) { | |
permutations.push(a.slice()); // copy the array to capture it's state | |
} else { | |
for (let i = n; isLessThan(i, a.length); i++) { | |
swap(a, n, i); | |
Permutations.of(a, n + 1, permutations); | |
swap(a, n, i); | |
} | |
} | |
} | |
return permutations; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Permutations from './permutations'; | |
describe("Permutations", () => { | |
it('should permute an array of numbers', () => { | |
expect(Permutations.of([1, 2, 3])).toEqual([ | |
[1, 2, 3], | |
[1, 3, 2], | |
[2, 1, 3], | |
[2, 3, 1], | |
[3, 2, 1], | |
[3, 1, 2] | |
]); | |
}); | |
it('should permute a string', () => { | |
expect(Permutations.of('abc')).toEqual([ | |
['a', 'b', 'c'], | |
['a', 'c', 'b'], | |
['b', 'a', 'c'], | |
['b', 'c', 'a'], | |
['c', 'b', 'a'], | |
['c', 'a', 'b'] | |
]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment