Skip to content

Instantly share code, notes, and snippets.

@offchan42
Last active January 4, 2017 02:56
Show Gist options
  • Save offchan42/4b563d1b290b3beddfcd70fca4be3e52 to your computer and use it in GitHub Desktop.
Save offchan42/4b563d1b290b3beddfcd70fca4be3e52 to your computer and use it in GitHub Desktop.
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:]))
@offchan42
Copy link
Author

offchan42 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