r/dailyprogrammer • u/nint22 1 2 • Jan 07 '13
[01/07/13] Challenge #116 [Easy] Permutation of a string
(Easy): Permutation of a string
Write a function that prints all of the permutatons of the unique characters of a given string. For example, permute("baz") would print:
baz
bza
abz
azb
zba
zabFind all the permutations of daily.
Author: skeeto
Formal Inputs & Outputs
Input Description
Your function should accept a single string variable which is guaranteed to be at least 1 character long.
Output Description
Print all permutations of the given string variable.
Sample Inputs & Outputs
Sample Input
Let the string argument be "ab"
Sample Output
All permutations of "ab" would be ["ab", "ba"]
Challenge Input
Let the string argument be "abbccc"
Challenge Input Solution
abbccc abcbcc abccbc abcccb acbbcc acbcbc acbccb accbbc accbcb acccbb babccc bacbcc baccbc bacccb bbaccc bbcacc bbccac bbccca bcabcc bcacbc bcaccb bcbacc bcbcac bcbcca bccabc bccacb bccbac bccbca bcccab bcccba cabbcc cabcbc cabccb cacbbc cacbcb caccbb cbabcc cbacbc cbaccb cbbacc cbbcac cbbcca cbcabc cbcacb cbcbac cbcbca cbccab cbccba ccabbc ccabcb ccacbb ccbabc ccbacb ccbbac ccbbca ccbcab ccbcba cccabb cccbab cccbba
Note
- Bonus 1: Instead of printing, be functional. Return a collection (array, etc.) of the permutations.
- Bonus 2: Correctly handle the case when the input contains a character multiple times. That is, don't output repeats and ensure the output contains the same number of characters as the input. For example, there are three permutations of foo: foo, ofo, oof.
- Note that this challenge is a near-duplicate of challenge #12, hence why there is the above "bonus" challenges
19
u/GodComplex2 0 1 Jan 08 '13
For people having trouble understanding how to do this, I've tried to write an algorithm that's easy to understand Python:
5
u/aredna 1 0 Jan 10 '13
As a MSSQL Query. Horribly inefficient, but in theory would work for strings up nvarchar limits.
MS SQL String Permutations
For some reason I get the following error every time I try to post something with a CTE to reddit, even with extensions disabled. I'd love to be able to post the solution inline if anyone has any suggestions.
5
u/ixid 0 0 Jan 08 '13 edited Jan 08 '13
Bogo-permute, written in the D language. An intentionally stupid method that randomly rearranges the array until it's in an order that's not been seen before with the added bonus of getting stuck if it's already done all permuations.
2
u/Zolrath Jan 08 '13
Done in Go, solving all bonuses.
2
u/jeff303 0 2 Jan 08 '13 edited Jan 08 '13
Bonus 2 solution, in Python, cheating:
I'll update this comment later if I have a chance to do a "non-cheating" solutionUpdate
OK, this turned out to be much more difficult than I expected - far moreso than the previous "intermediate" challenge. Here is my non-cheating solution, in Python (assuming you don't count using itertools to remove duplicates as described here cheating). It satisfies both bonus conditions and can calculate permutations of any type of list (not just strings).
4
u/Rapptz 0 0 Jan 07 '13
I assume this is cheating?
C++. Both bonuses should pass.
10
u/[deleted] Jan 08 '13
J:
A.is theAnagramoperator:x A. yfinds thexth permutation ofy. We pass the list of integers from0ton!-1toA.to have it generate alln!possible permutations. Then, we filter unique items using~. (Nub).9
u/skeeto -9 8 Jan 07 '13
These all satisfy both bonuses. Removing an element from the middle of a sequence seems to be ugly in every language. slice slice append
Clojure,
JavaScript,
Common Lisp / Emacs Lisp,
6
u/nudan1 Jan 07 '13
C++: (hint: this problem screams recursion!)
6
u/[deleted] Jan 08 '13
I feel that this is sort of cheating but, a quick python one-liner using itertools.permutations does it nicely while handling both the bonuses:
So here we go with some C code, quick and dirty (read: has some bugs and doesn't satisfy the bonuses):
2
u/DangerousDuck Jan 07 '13
Python:
Although I wouldn't even think of using this over itertools.permuntations, here's a recursive approach using a set to filter out all duplicates.
4
u/Glassfish 0 0 Jan 07 '13 edited Jan 07 '13
Scala:
Edit:
Without using the built-int function:
4
u/redried Jan 08 '13
Javascript with underscore for a clumsy functional approach. (Don't quite get it, but this was fun.)
3
u/marekkpie Jan 08 '13 edited Jan 18 '13
Lua.
Didn't see a Lua version here. Lua's strings cannot be nicely indexed, so I have to turn the string into a table before I can permute through it.
Also, the mathematical term for Bonus 2 is a combination
Erlang:
3
u/javamonk Jan 08 '13
A recursive solution in ruby:
3
u/HandOfTheCEO Jan 08 '13 edited Jan 08 '13
Haskell solution:
EDIT: It worked for input "daily". Doesn't work for repetitions. Will update it when done.
3
u/foreveranewbie Jan 08 '13 edited Jan 08 '13
It works and achieves the bonus challenges. I'm pretty sure the nested while statements are inadvisable but that was all I could think of without cheating. Any other critiques would be appreciated.
Python:
3
u/hamishiam 1 0 Jan 08 '13 edited Jan 08 '13
Another recursive Java solution, including bonuses.
Worked on optimisation a fair bit. Points of interest:
Notes:
Edit: Fixed formatting
3
u/[deleted] Jan 09 '13
Java
Seems to work with bonuses (permute finishes with a Hash set, use an array to print it)
Not the cleanest ending in the world, but the code seems pretty good. sorry for lack of comments.
4
u/SplashAttacks 0 1 Jan 08 '13
Java with Recursion
2
u/[deleted] Jan 07 '13
In Go:
2
u/robertmeta Jan 07 '13 edited Jan 07 '13
Erlang: (escript allows running as a script rather than compiled, + both bonuses)
2
u/kirsybuu 0 1 Jan 08 '13 edited Jan 08 '13
Fun in the D Language: On-demand permutation range (both bonuses)
Currently only allows [a-z] chars for efficiency, but could be reworked for more if necessary.
2
u/niomaster Jan 08 '13
My haskell solution without the permutations function, plus bonus one:
2
u/[deleted] Jan 09 '13 edited Jan 09 '13
Python:
2
u/jprimero Jan 09 '13
Hi guys, just found this. Great idea, I will definitely be on board for the next challenges.
Anyway, here is Java
And this is the same in F#
With bonuses, i guess
2
u/PuffGuru Jan 10 '13
Writes all permutations to a file, and counts number of permutations. Constructive criticism always welcomed.
Implementation in C:
2
u/capncanuck Jan 10 '13
Here is my solution in Java. All bonuses are satisfied.
2
u/[deleted] Jan 10 '13
// /r/dailyprogrammer // 1/10/13 // Challenge 116 // code: jakenherman
public class RedditDP {
}
// I realize this is probably not the correct way to do it, // however I AM a beginning programmer!
2
u/shaggorama Jan 13 '13
Python:
2
u/Quasimoto3000 1 0 Jan 14 '13
I really like string slicing in python.
2
u/[deleted] Jan 14 '13 edited Jan 14 '13
C++ backtracking.
4
u/Selfmadecelo Jan 07 '13 edited Jan 08 '13
Ruby:
Edit: Fairly new to Ruby so if anybody has a better way I'd appreciate the input!
3
u/Splanky222 0 0 Jan 08 '13
Not really fair in MATLAB, solution and both bonuses are a simple one-liner
2
u/Saxasaurus Jan 08 '13
Java: Solves all challenges.
2
u/UltimateHater Jan 07 '13 edited May 20 '13
EDIT: Hrrm. Not quite sure how to set this to hidden.
1
u/[deleted] Jan 07 '13
Python:
1
u/[deleted] Jan 08 '13 edited Jan 08 '13
Python, using sets
edit: updated it to use 2.7's built-in set class
1
u/batailleye Jan 22 '13
Java
1
u/poorbowelcontrol Jan 24 '13
Solution in Javascript:
1
u/shubhamVerma Mar 09 '13
Python:
1
u/JustinBieber313 Mar 28 '13
C++, iterative solution.
1
u/Frichjaskla Jan 07 '13
Python both bonus challenges is handled
Tests
print permute("abc") print permute("abbccc")
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
['abbccc', 'abcbcc', 'abccbc', 'abcccb', 'acbbcc', 'acbcbc', 'acbccb', 'accbbc', 'accbcb', 'acccbb', 'babccc', 'bacbcc', 'baccbc', 'bacccb', 'bbaccc', 'bbcacc', 'bbccac', 'bbccca', 'bcabcc', 'bcacbc', 'bcaccb', 'bcbacc', 'bcbcac', 'bcbcca', 'bccabc', 'bccacb', 'bccbac', 'bccbca', 'bcccab', 'bcccba', 'cabbcc', 'cabcbc', 'cabccb', 'cacbbc', 'cacbcb', 'caccbb', 'cbabcc', 'cbacbc', 'cbaccb', 'cbbacc', 'cbbcac', 'cbbcca', 'cbcabc', 'cbcacb', 'cbcbac', 'cbcbca', 'cbccab', 'cbccba', 'ccabbc', 'ccabcb', 'ccacbb', 'ccbabc', 'ccbacb', 'ccbbac', 'ccbbca', 'ccbcab', 'ccbcba', 'cccabb', 'cccbab', 'cccbba']
1
u/Laremere 1 0 Jan 08 '13
Python
Outputs:
set(['bccacb', 'cbccba', 'bacccb', 'bbaccc', 'abcbcc', 'bacbcc', 'bcbcac', 'acccbb', 'ccabbc', 'ccbcba', 'bccbca', 'bcccab', 'cccabb', 'abcccb', 'bbccca', 'acbccb', 'cccbba', 'bcccba', 'abccbc', 'cbcacb', 'acbbcc', 'bccbac', 'cabcbc', 'accbbc', 'bcacbc', 'ccbacb', 'cacbbc', 'babccc', 'cbbacc', 'cbcbac', 'cbcbca', 'bccabc', 'ccbbca', 'baccbc', 'ccbcab', 'ccbbac', 'bcbcca', 'abbccc', 'acbcbc', 'cbccab', 'cbacbc', 'bcbacc', 'caccbb', 'ccabcb', 'cbcabc', 'bbcacc', 'accbcb', 'bbccac', 'cabccb', 'cbabcc', 'bcaccb', 'ccacbb', 'cabbcc', 'ccbabc', 'bcabcc', 'cbbcac', 'cbbcca', 'cbaccb', 'cacbcb', 'cccbab'])
60
60
1
u/calzoneman Jan 08 '13 edited Jan 08 '13
Python 3. Pretty standard recursive approach
Becomes unusably slow for strings of length 10 due to the sheer number of recursive calls required.
1
u/r4mtha Jan 08 '13
Java:
It should be noted that dynamic programming is probably a great way to solve this problem; my recursive solution executes something like 700 times to return 60 actual values; dynamic programming would eliminate this issue by storing previously solved subproblems (recursion + state == dynamic programming as they say). I'll try to solve again dynamically if I have time tomorrow.
Edit: Solves all bonus problems (trick is treating each value in array as unique integer).
0
u/Wedamm Jan 07 '13
Haskell:
Both bonuses ;-]
0
u/[deleted] Jan 07 '13
[deleted] — view deleted comment
0
u/[deleted] Jan 14 '13
[deleted] — view deleted comment