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

imported>Ynasser
imported>Ehashman
 
(18 intermediate revisions by 4 users not shown)
Line 1:
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.
 
= 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!
Line 10 ⟶ 14:
== Shakespeare Exercises ==
 
Here's a link to the [https://openhatch.org/wiki/Shakespeare Shakespeare] handout from last time: [].
 
Using the command line (or "command prompt"), enter into the directory named '''shakespeare-exercises'''.
Line 20 ⟶ 24:
To get started, import the two modules which provide the character list and text of the play:
<pre>
>>> importfrom summerChar import characters
>>> importfrom summerNight import lines
</pre>
 
Character names are stored with all capital letters, which is how their parts are represented in the script.
To access the list of characters, set the following variable:
<pre>
>>> characters = summerChar.SummerChar
</pre>
 
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.
To access the text of the play as a list (each line is an entry in the list):
<pre>
>>> lines = summerNight.SummerNight
</pre>
 
 
'''Exercise 2'''
 
You must have completed exercise 1 to do 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:
Line 54 ⟶ 52:
 
== Wordplay Exercises ==
Here's a [https://openhatch.org/wiki/Wordplay_handout 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, [https://github.com/jesstess/Wordplay 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:
 
<pre>
>>> pattern = re.compile("in[^g]")
</pre>
 
To get started, recall that you need to import the regular expressions and scrabble modules:
<pre>
>>> import re
>>> import scrabble
</pre>
 
The scrabble wordlist can be accessed by:
<pre>
>>> words = scrabble.wordlist
</pre>
'''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:
<pre>
>>> word = "pokemon"
>>> word[::-1]
"nomekop"
</pre>
Anonymous user