Python Workshops for Beginners/Saturday September 27th homework

From OpenHatch wiki
Jump to navigation Jump to search

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 on October 25. 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:

>>> import summerChar
>>> import summerNight

To access the list of characters, set the following variable:

>>> characters = summerChar.SummerChar

Note: character names are stored in the form <NAME>, which is how their parts are represented in the script.

To access the text of the play as a list (each line is an entry in the list):

>>> lines = summerNight.SummerNight


Exercise 2

You must have completed exercise 1 to do exercise 2. If you cannot complete exercise 1, just make up values for 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. Exercise 3 requires knowledge of regular expressions. If you need solutions to the wordplay exercises to see example of regular expressions in action, they can be found here.


Exercise 3

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 do this:

>>> 

Exercise 4