Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate all 4096 permutations of "happy new year" in Python 3
sentence = 'happynewyear'
n = len(sentence)
total_perms = 2 ** n
for perm in range(total_perms):
generated = []
for i in range(n):
ch = sentence[i]
if (1 << i) & perm: # see explanation: https://www.quora.com/What-is-bitmasking-What-kind-of-problems-can-be-solved-using-it/answer/Piyush-Kumar-8
ch = ch.upper()
generated.append(ch)
result = ''.join(generated)
print('{} {} {}'.format(result[:5], result[5:8], result[8:]))
@off99555

This comment has been minimized.

Copy link
Owner Author

@off99555 off99555 commented Jan 4, 2017

The sentence can be changed but you will also have to change the last line (print statement) to reflect your segmentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment