Skip to content

Instantly share code, notes, and snippets.

@kulicuu
Last active December 24, 2015 05:19
Show Gist options
  • Save kulicuu/6750178 to your computer and use it in GitHub Desktop.
Save kulicuu/6750178 to your computer and use it in GitHub Desktop.
permute a string
_ = 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