Finding all possible case permutations in Python [duplicate]
I need to return a list of all possible case permutations of a string in python.
For example, input "ar"
should return:
[ 'ar','Ar','aR','AR']
or "arc"
:
[ 'arc','Arc','ARc','aRc','aRC','ARC']
Solution 1:
def all_casings(input_string):
if not input_string:
yield ""
else:
first = input_string[:1]
if first.lower() == first.upper():
for sub_casing in all_casings(input_string[1:]):
yield first + sub_casing
else:
for sub_casing in all_casings(input_string[1:]):
yield first.lower() + sub_casing
yield first.upper() + sub_casing
>>> [x for x in all_casings("foo")]
['foo', 'Foo', 'fOo', 'FOo', 'foO', 'FoO', 'fOO', 'FOO']
>>> list(all_casings("foo"))
['foo', 'Foo', 'fOo', 'FOo', 'foO', 'FoO', 'fOO', 'FOO']
Solution 2:
You can achieve this by zipping the upper and lower case letters and taking their cartesian product:
import itertools
chars = "abc"
results = list(map(''.join, itertools.product(*zip(chars.upper(), chars.lower()))))
print(results)
>>>['ABC', 'ABc', 'AbC', 'Abc', 'aBC', 'aBc', 'abC', 'abc']
To visualise how this works:
-
zip
is creating our 3 'axes' for us, each with 2 points (the upper / lower cases):[('A', 'a'), ('B', 'b'), ('C', 'c')]
-
product
takes the cartesian product of these axes, i.e. the 8 possible coordinates corresponding to the corners of the unit cube it creates:
1. Create axes | 2. Take Cartesian product |
-
''.join
concatenates the tuples to output a string:('A','B','C') -> 'ABC'