Last active
December 24, 2015 05:19
-
-
Save kulicuu/6750178 to your computer and use it in GitHub Desktop.
permute a string
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
_ = require 'underscore' | |
c = console.log | |
mia = ['a', 'b', 'c', 'd'] | |
# or | |
miString = 'abcd' | |
mia = miString.split("") | |
# you can put any string you like, then call the recursive routine below: | |
permute = (a) -> | |
c 'permute and mia ', a | |
if a.length == 1 | |
return a | |
turning = [] | |
for i in a | |
b = [] | |
for j in a | |
b.push j | |
b.splice(_.indexOf(b, i), 1) | |
rays = permute b | |
for k in rays | |
turning.push i + k | |
return turning | |
# show the brute results: | |
lastly = permute mia | |
c 'lastly ' + lastly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment