Finding all possible case permutations in Python
case-insensitive, permutation, python, string
Solution
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']
Problem
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'] ```