Created
August 8, 2018 23:44
-
-
Save JupyterJones/0adf61c4b0d382867576cf49031ea96b to your computer and use it in GitHub Desktop.
Get all possible combinations and permutations of numbers or words
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
#!/user/local/bin/python | |
# Enter a number or a phrase | |
from itertools import permutations | |
Xset = set() | |
SEARCH = raw_input("Search Phrase: ") | |
SEARCH = SEARCH.split(" ") | |
searchs = list(SEARCH) | |
print searchs | |
print "------------------------------------" | |
perm = permutations(searchs) | |
# Print the obtained permutations | |
for i in list(perm): | |
X = " ".join(i) | |
Xset.add(X) | |
full = set() | |
for SEARCH in Xset: | |
#print SEARCH | |
search = SEARCH.split(" ") | |
XX = " ".join(search) | |
full.add(XX) | |
n = len(search) | |
ln = range(n) | |
for num in ln: | |
for comb in itertools.combinations(search, num): | |
YY = " ".join(comb) | |
full.add(YY) | |
for line in full: | |
print "".join(line),"|", | |
>>> Search Phrase: 1 2 3 | |
>>> ['1', '2', '3'] | |
>>> ------------------------------------ | |
>>> | 2 1 | 2 3 | 3 1 2 | 1 2 | 1 3 | 2 3 1 | 1 3 2 | 1 | 3 | 2 | 3 2 1 | 3 1 | 3 2 | 2 1 3 | 1 2 3 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment