Python Workshops for Beginners/Saturday September 27th homework: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Ynasser
imported>Ynasser
Line 79: Line 79:


The scrabble wordlist can be accessed by:
The scrabble wordlist can be accessed by:
<pre>
<pre> words = scrabble.wordlist
>>> words = scrabble.wordlist
</pre>
</pre>
'''Exercise 4'''
'''Exercise 4'''

Revision as of 06:11, 18 October 2014

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.

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 example 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"