Created
October 31, 2012 20:38
-
-
Save KartikTalwar/3989707 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 itertools | |
possible = ['1', '2', '3', '4', '5'] | |
doubles = [] | |
brute = [] | |
def stringPermutations(string): | |
rez = [] | |
if len(string) < 2: | |
rez.append(string) | |
else: | |
for position in range(len(string)): | |
perms = string[:position] + string[position+1:] | |
for i in stringPermutations(perms): | |
rez.append(string[position:position+1] + i) | |
return rez | |
def stringCombinations(string, right = '', min=2): | |
if not string: | |
if len(right) == min: | |
doubles.append(right) | |
return | |
stringCombinations(string[1:], string[0] + right) | |
stringCombinations(string[1:], right) | |
stringCombinations(''.join(possible)) | |
for i in doubles: | |
separate = [c for c in i] | |
leftover = [m for m in possible if m not in separate] | |
permutes = stringPermutations(''.join(leftover)) | |
newlist = leftover + ['-' + i + '-'] | |
allnew = [s for s in itertools.product(newlist, repeat = 4)] | |
allnew = [t for t in allnew if len(set(t)) == 4] | |
brute += [''.join(p) for p in allnew ] | |
for i, j in enumerate(brute): | |
print "%03d)" % (i+1) , j.lstrip('-').rstrip('-') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment