Skip to content

Instantly share code, notes, and snippets.

@qtc-de
Last active March 19, 2022 06:31
Show Gist options
  • Save qtc-de/d4d224509903c69ccdbfb8200fdf9a7a to your computer and use it in GitHub Desktop.
Save qtc-de/d4d224509903c69ccdbfb8200fdf9a7a to your computer and use it in GitHub Desktop.
Swap the case of each ASCII letter within a string and print each possible combination to stdout.
#!/usr/bin/env python3
import string
import argparse
import itertools
from typing import Iterator
def swaperoo(target: str) -> Iterator[str]:
'''
Swap the case of ASCII letters within a string and return
each possible combination in form of an Iterator.
Parameters:
string input string to perform the swaperoo on
Returns:
Iterator[str] each possible swap combination
'''
input_str = list(target)
filtered = filter(lambda x: x[1] in string.ascii_letters, enumerate(input_str))
ascii_positions = list(map(lambda x: x[0], filtered))
for ctr in range(1, len(ascii_positions) + 1):
for index_set in itertools.combinations(ascii_positions, ctr):
current = input_str.copy()
for index in index_set:
current[index] = current[index].swapcase()
yield "".join(current)
parser = argparse.ArgumentParser(description='Swap the case of ASCII letters within a string')
parser.add_argument('string', help='string to swap ASCII letters in')
args = parser.parse_args()
for item in swaperoo(args.string):
print(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment