string - Finding all possible case permutations in Python -
i need work out list of possible permutations of case only in python example input of ar return [ 'ar','ar','ar','ar']
or arc [ 'arc','arc','arc','arc','arc','arc'] , know there nice method life of me can not figure out.
def all_casings(input_string): if not input_string: yield "" else: first = input_string[:1] if first.lower() == first.upper(): sub_casing in all_casings(input_string[1:]): yield first + sub_casing else: sub_casing in all_casings(input_string[1:]): yield first.lower() + sub_casing yield first.upper() + sub_casing
>>> [x 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']
Comments
Post a Comment