Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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']
like image 267
KatGaea Avatar asked Jul 22 '11 16:07

KatGaea


2 Answers

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']
like image 93
Amber Avatar answered Oct 26 '22 22:10

Amber


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:

  1. zip is creating our 3 'axes' for us, each with 2 points (the upper / lower cases):
    [('A', 'a'), 
     ('B', 'b'), 
     ('C', 'c')]
    
  2. product takes the cartesian product of these axes, i.e. the 8 possible coordinates corresponding to the corners of the unit cube it creates:
enter image description here enter image description here
1. Create axes 2. Take Cartesian product
  1. ''.join concatenates the tuples to output a string: ('A','B','C') -> 'ABC'
like image 32
iacob Avatar answered Oct 26 '22 22:10

iacob