Ruotong's MOAR Dictionary Exercises

by Ruotong Yang

05 Apr 2016

** Exercise 1 **

The first exercise is like the examples in the textbook (if I understood it correctly). At first I just made a dictionary of all the words. After I found there are both “you” and “You”, and “\{em” in it, I added two lines to remove the capital letters and punctuations.

** Exercise 2-5 **

The second exercise is a combination of regex and dictionary. One thing need to keep in mind always is the type. At first I wrongly used line = line.split() before the re.search() and it will give me an error since search method needs a string while I split the line into a list of items.
The third exercise is more with the combination of regex and dictionary. The problem I have encountered is how to get the right email addresses. I first used the regular expression in the textbook: [a-zA-Z0-9]\S@\S[a-zA-Z]. But this will give me some incorrect addresses: source@collab.sakaiproject.org (135), postmaster@collab.sakaiproject.org (27) and apache@localhost (27). I added “.” in x[0] and “org” not in x[0] in the conditional expression to deal with them. apache@localhost is obviously incorrect. souce@… and postmaster@… are not shown in examples, so I also removed them.
I tried to use following lines first, but these lines will give me error. I have no idea about it since I think there is no difference if I put them in the next conditional expression (which works). if “org” in x[0]: continue *
The fourth exercise uses the previous idea of iteration. The point is distinguish dictionary’s keys and values.
The fifth exercise uses the feature of regex that we can search something and only get part of the result if we want.
Same question about the conditional as in the third exercise.*

Ruotong Yang is a master student in STOR department at UNC Chapel Hill. Find Ruotong Yang on Twitter, Github, and on the web.