Last active
January 4, 2017 02:56
-
-
Save offchan42/4b563d1b290b3beddfcd70fca4be3e52 to your computer and use it in GitHub Desktop.
Generate all 4096 permutations of "happy new year" in Python 3
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
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:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The sentence can be changed but you will also have to change the last line (print statement) to reflect your segmentation.