Skip to content

Instantly share code, notes, and snippets.

@JupyterJones
Created August 8, 2018 23:44
Show Gist options
  • Save JupyterJones/0adf61c4b0d382867576cf49031ea96b to your computer and use it in GitHub Desktop.
Save JupyterJones/0adf61c4b0d382867576cf49031ea96b to your computer and use it in GitHub Desktop.
Get all possible combinations and permutations of numbers or words
#!/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