Starting Python with Stephen Colbert
A Stephen Colbert Tweet prompts my first Python expedition !
"I'm off next week, so until I come back, please recombine the letters in this tweet into new ones for your enjoyment."
Using some online anagram generators, I came up with this: "Sweet nonentities wet poetless ,insociable muck; mix off to keen fury, monotony; win hot intemerate belcher jeer."
But I wanted to ensure that I have used exactly the same letters. This looked like a good time to try out Python. The broad algorithm I used is:
Given strings str1 and str2,
- get a signature from each string by sorting all letters in each string (in lowercase letters, ignoring punctuation)
- do a simple string compare
So, here is the simple program that resulted :
import sys import string def main(): str1 = 'This. is a shell program' str2 = 'Shell? Is this a ...program !?' str1Sign = getSignature(str1) str2Sign = getSignature(str2) if(str1Sign == str2Sign): print("Woohoo!") else: print("Boo!") return 0 # the signature is decided to be a lowercased, sorted list of the characters in a string def getSignature(strg): # args for string.translate tbl = string.maketrans("","") delchars = string.punctuation + string.whitespace # cleanup the string strg = strg.lower() strg = strg.translate( tbl, delchars ) # get the signature strChars = sorted(strg) return strChars if __name__ == "__main__": main()
Couple of observations about Python , IDLE :
- History in IDLE is weird ... Alt+P , why not UP or some modifier + UP ?
if __name__ ..section has to follow the declaration of main()