Skip to content

Instantly share code, notes, and snippets.

@LeonardoCardoso
Created November 29, 2016 12:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeonardoCardoso/27da2545550901b18ba3bcc690d78b4a to your computer and use it in GitHub Desktop.
Save LeonardoCardoso/27da2545550901b18ba3bcc690d78b4a to your computer and use it in GitHub Desktop.
Generate all combinations of a string, uppercase and lowercase.
def recurse (pref,suff):
# If no characters left, just print prefix.
if suff == "":
print pref
return
# Otherwise add lowercase of first suffix letter to prefix
# and recur with that and the remainder of the suffix.
# Then do the same for uppercase.
# If you wanted smarts, this is where you'd detect if the
# upper and lower were the same and only recurse once.
recurse (pref + suff[0:1].lower(), suff[1:])
recurse (pref + suff[0:1].upper(), suff[1:])
# Test the function with "macOS".
recurse ("","macos")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment