Python Workshops for Beginners/Saturday September 27th homework

Hopefully you haven't forgotten too much from the last few sessions. Here are some homework problems you should try out before the next session.

= September 27 Homework Exercises =

The exercises are based on the Shakespeare and Wordplay exercises from last time. Regardless of which afternoon session you attended, you should be able to do both, unless otherwise specified.

Remember: indentation in Python matters!

Setup

 * Download PWFB-homework.zip
 * Unzip the file
 * Use the command line to navigate into the directory PWFB-homework

Shakespeare Exercises
Here's a link to the Shakespeare handout from last time.

Using the command line (or "command prompt"), enter into the directory named shakespeare-exercises.

Exercise 1

Write a program that creates a dictionary with keys as the characters from A Midsummer Night's Dream and the values being the number of times each particular character had a speaking part.

To get started, import the two modules which provide the character list and text of the play: >>> from summerChar import characters >>> from summerNight import lines

Character names are stored with all capital letters, which is how their parts are represented in the script.

Each line of the play is an entry in the list. At the end of some lines you may see things like "/r" or "/n". You can ignore these symbols, as they're just special characters used to denote things such as new lines (like when you press the enter key) or tabs.

Exercise 2

If you cannot complete exercise 1, just make up values for a dictionary and try this exercise anyway.

Write a program which determines the percentage of the total number of speaking parts each character had. Use the dictionary you created in exercise 1. Here's an example to make the problem more clear:

Suppose we had the following dictionary of characters, where the values represent the number of speaking parts each has: >>> characters = {Harry: 3, Hermione: 4, Ron: 5}

A program which determines what percentage of the total each had might output something like this: Ron has 41.666% of the speaking parts. Harry has 25% of the speaking parts. Hermione has 30% of the speaking parts.

Wordplay Exercises
Here's a link to the Wordplay handout if you need a review.

Make sure to navigate to the wordplay-exercises directory and put your python files there for this problem.

Exercise 3

This exercise requires knowledge of regular expressions. If you need solutions to the Wordplay exercises to see examples of regular expressions in action, they can be found here.

Write a program to find all words in the scrabble dictionary which contain a q which isn't followed by a u.

To do this problem, you'll need something which isn't covered in the Wordplay handout. If you want to look for a string which contains "in" but isn't immediately followed by a "g", you'd use this as your pattern:

>>> pattern = re.compile("in[^g]")

To get started, recall that you need to import the regular expressions and scrabble modules: >>> import re >>> import scrabble

The scrabble wordlist can be accessed by: >>> words = scrabble.wordlist Exercise 4

Search through the scrabble wordlist for all words that are palindromes (i.e. the same forwards and backwards).

As a hint, remember that you can reverse strings and lists using the "slice" syntax: >>> word = "pokemon" >>> word[::-1] "nomekop"