import itertools
s = 'Fox'
map(''.join, itertools.product(*zip(s.upper(), s.lower())))
>>> ['FOX', 'FOx', 'FoX', 'Fox', 'fOX', 'fOx', 'foX', 'fox']
I always wanted to try this.
No idea if this fits your qualifications(it does work though).
str = raw_input()
def getBit(num, bit):
return (num & 1 << bit) != 0
for i in xrange(0,2**len(str)):
out = ""
for bit in xrange(0,len(str)):
if getBit(i,bit):
out += str[bit].upper()
else:
out += str[bit].lower()
print(out)
The idea is that as you increment in binary, you get every possible permutation of 1s and 0s.
Then you simply convert this list of 1s and 0s to a string, 1 meaning uppercase, 0 meaning lowercase.
This is the excellent, accepted answer by @ephemient modified a little bit.
Changes:
lower-case before upper-case, just so the list starts with "fox" instead of "FOX" (the question's example sequence starts with "fox")
use of a list comprehension instead of map()
(either way is fine, really)
broke out the code that generates the lower/upper case pairs to make it more clear
packaged it up into a function.
The code:
import itertools as it
def cap_permutations(s):
lu_sequence = ((c.lower(), c.upper()) for c in s)
return [''.join(x) for x in it.product(*lu_sequence)]
One liner using list comprehension:
from itertools import permutations
strs='fox'
combin=[''.join(x) for x in permutations(list(strs)+list(strs.upper()),3) if ''.join(x).lower()=='fox']
print(combin)
>>> ['fox', 'foX', 'fOx', 'fOX', 'Fox', 'FoX', 'FOx', 'FOX']
Using a for loop:
from itertools import permutations
strs='fox'
lis2=list(strs)+list(strs.upper())
for x in permutations(lis2,3):
if ''.join(x).lower()=='fox':
print(''.join(x))
>>> fox
foX
fOx
fOX
Fox
FoX
FOx
FOX
Use product (False, True) to find any permutations of change char in string for upper & lower:
def capitalize_char_permutation (string:str) -> str :
conditions = product((0,1), repeat=len(string))
for i in conditions:
result = ''
for j in range(len(i)):
if i[j]==0 :
result+= string[j].lower()
else:
result+= string[j].upper()
yield result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With