Last active
August 29, 2015 14:16
-
-
Save rohit-jamuar/d40cdcbab517a2179609 to your computer and use it in GitHub Desktop.
Generates all combinations of case-alternating alphabets from string argument
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
| def generate_combinations(s): | |
| ''' | |
| Generates all combinations of case-alternating alphabets from string 's' | |
| for e.g. if 's' was '0abc', the function would output: | |
| 0abc | |
| 0abC | |
| 0aBc | |
| 0aBC | |
| 0Abc | |
| 0AbC | |
| 0ABc | |
| 0ABC | |
| ''' | |
| from string import digits | |
| alpha_count, only_digits = 0, [] | |
| for i in range(len(s)): | |
| if s[i] in digits: | |
| only_digits.append(s[i]) | |
| else: | |
| only_digits.append('-') | |
| alpha_count += 1 | |
| for i in range(2 ** alpha_count): | |
| temp = only_digits[:] | |
| x = bin(i)[2:] | |
| while len(x) != alpha_count: | |
| x = '0' + x | |
| alpha_index = 0 | |
| for index in range(len(s)): | |
| if temp[index] == '-': | |
| temp[index] = s[index].upper() \ | |
| if x[alpha_index] == '1' else s[index] | |
| alpha_index += 1 | |
| print ''.join(temp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment