CSE 142: Computer Programming I, Summer 2008 |
CSE Home | About Us Search Contact Info |
Our Python sessions are every Tuesday, 10:50am - 11:50am, in MOR room 220. For those of you who are unable to make it to the first section, we would still like to see you at the next one! You should feel free to hop on board at any point in the quarter. If you are interested, we recommend that you subscribe to the mailing list. You will need to click on the 'subscribe' button under the 'Have my u.washington.edu email address entered' bullet. You can also e-mail Hélène, Kim or John and we can add you by hand. We will use this to notify everyone about error corrections, things we forgot to mention, etc.
The assignment: Turn in a file personalitytest.py
or nltkfun.py
. If you do personalitytest.py
, make your program contain a Python version of your program from Homework 7.
If you choose to do nltkfun.py
, answer the following questions/problems:
nltk.tokenizer.PunktWordTokenizer
leaves periods at the end of words it figures are abbreviations. Let's say an abbreviation can't be longer than 4 letters, not including the period. If it has more than one period, it is definitely an abbreviation and should remain as one token. Write a function cse142_tokenize
that takes a string and returns a tokenized list using PunktWordTokenizer
with the period problem fixed (e.g "Booyah."
-> ["Booyah", "."]
but "Mrs."
-> ["Mrs."]
, because "Mrs."
only has 3 letters).
main
, demonstrate your cse142_tokenize
method on a webpage. The following code will retrieve a webpage and strip it down to just the non-HTML parts, ready for you to tokenize. Pick any PG webpage to demonstrate. In comments, explain how your method performs. How could it be improved?
from urllib import urlopen address = "http://www.example.com" page = urlopen(address) page = nltk.clean_html(page)
Look up all the possible tags in the Penn Treebank:
Now write a method top_words
that returns a list of tuples, where each tuple contains the most frequent word of a particular tag paired with the tag itself, i.e. (top_word, tag).
NOTE: there are a lot of words in the Treebank, so your method may take a while to run.
Revisit the nltk.chat.eliza
package and try out nltk.chat.eliza.eliza_chat()
. Then in comments, answer the following questions.
As a reference, our sample solutions are 80 and 55 lines long respectively. The reward for turning in this program is 1 extra late day for use on your normal Java programs, plus baked goods, eternal good looks, and overall excellent karma. (Since it's the end of the quarter, getting extra late days is less motivating than it used to be. Mostly you'd just be doing this one for the learning experience.)
The assignment: Turn in a file guessinggame.py
or babynames.py
(lowercase) containing a Python version of your program from Homework 5 or 6. You can choose either of these past two programs as your next Python assignment. As a reference, our sample solutions are 55 and 80 lines long respectively. The reward for turning in this program is 1 extra late day for use on your normal Java programs, plus baked goods, eternal good looks, and overall excellent karma.
The assignment: Turn in a file practice.py
containing Python solutions to at least 2 of the following programming problems of your choice. These are intended to be similar to the kind of questions you'd see on your midterm exam. Write the function specified along with a main
area of the program that calls your function a few times to verify that it works. The reward for turning in this program is 1 extra late day for use on your normal Java programs, plus a super cool snake icon on your Facebook profile, if you use our Facebook application.
Write a function shift
that accepts two parameters, a string and an integer n, and returns the string with each of its characters shifted to the right by n places. If n is negative, shift to the left. Characters that run off the end should wrap around to the beginning. For example, shift("Attack", 1)
returns "kAttac"
and shift("Attack", -9)
returns "ackAtt"
.
Write a function exp_sums
that accepts two integer parameters, an exponent and a maximum value. Your function will examine each number from 1 through the maximum inclusive. It will raise each number in that range to the power represented by the exponent parameter, and print a message showing the result. Once this has been done for all numbers in the range, your function will report the sum of all the even terms and of all the odd terms.
For example, the call of exp_sums(3, 6)
should produce the following output:
1 ^ 3 = 1 2 ^ 3 = 8 3 ^ 3 = 27 4 ^ 3 = 64 5 ^ 3 = 125 6 ^ 3 = 216 Even sum = 288 Odd sum = 153
The call of exp_sums(2, 8)
should produce the following output:
1 ^ 2 = 1 2 ^ 2 = 4 3 ^ 2 = 9 4 ^ 2 = 16 5 ^ 2 = 25 6 ^ 2 = 36 7 ^ 2 = 49 8 ^ 2 = 64 Even sum = 120 Odd sum = 84
The call of exp_sums(1, 4)
should produce the following output:
1 ^ 1 = 1 2 ^ 1 = 2 3 ^ 1 = 3 4 ^ 1 = 4 Even sum = 6 Odd sum = 4
Write a function score_couplet
like the one written in the 7/18 Java lecture. You should accept two strings as parameters, and return their score. The score should receive one point for each of:
Any string comparisons should be case-insensitive.
For example, the call of score_couplet("I joined the CS party", "Just like LN and MARTY")
should return 2. The call of score_couplet("And it's still about the Benjamins", "Big faced hundreds and whatever other synonyms")
should return 0.
Write a function print_letters
that accepts an integer parameter representing an interval, and prints out lowercase letters starting with "a" and incrementing by the given parameter until reaching the end of the alphabet. These should be printed in a comma separated list. You may assume that the interval will be at least 1. For example, the call of print_letters(3)
should print the following output:
a, d, g, j, m, p, s, v, y
The call of print_letters(5)
should print the following output:
a, f, k, p, u
The call of print_letters(26)
should print the following output:
a
The assignment: Turn in a file globes.py
(lowercase g) containing a Python version of your program that produces the same output as globes.png below. As a reference, our sample solution is 40 lines long. The reward for turning in this program is 1 extra late day for use on your normal Java programs, plus a super cool snake icon on your Facebook profile, if you use our Facebook application.
The main change between this program and the Java version is that rather than drawing concentric circles in each subfigure, this version draws two sets of contentric ovals that give each subfigure a "globe-like" appearance. In one set, all ovals have equal heights but varying widths. The other set have equal widths and varying heights. In both cases, the amount of change in oval size is the same as the "gap" from the Java HW3-Circles assignment. The dimensions and counts for this assignment are identical to those of the Java HW3 assignment.
The other change from the Java program is that the colors of each subfigure and grid have been parameterized. Use string parameters to represent the colors used in the screenshot, such as orange (top-right), red (bottom-middle), and blue (bottom-right). Use default parameter values and/or parameter keywords as appropriate. You won't be able to check your output since our Python DrawingPanel doesn't know how to compare images. But we will be lenient on whether the graphical output exactly matches.
The assignment: Turn in a file spaceneedle.py
(lowercase s) containing a Python version of your HW2 Space Needle program, producing the same output. The program should use any feature of Python that you have seen so far to simplify the program. As a reference, our sample solution is 35 lines long. The rewards you'll get for turning in this program are the following:
The assignment: Turn in a file song.py
(lowercase s) containing a Python version of your HW1 Song program, producing the same output. As a reference, our sample solution is 65 lines long. The rewards you'll get for turning in this program are the following:
This quarter in CSE 142, we will conduct a special optional program to offer students a chance to learn a second programming language as you're learning Java. The second language's name is Python.
Python is a language that's good for writing programs to process text and other data. It's used heavily in the Linux operating system and at companies like Google.
Learning two programming languages is a bit like growing up in a bilingual family: you'll not only learn those two languages well, but you may also learn some higher concepts about programming and programming languages in general.
To start using Python on your own machine, follow the Python Installation Instructions. The Python Website is a great place to begin learning the basics.
Our Python program will be hosted by Hélène Martin along with two of our TAs, Kimberly Todd and John Kurkowski. Each week, they will hold a 1-hour session to teach you the equivalent of that week's Java course material into Python, along with any related issues.
The work involved in this program would be the following:
Primarily, these projects will consist of solving the same problem as that week's Java programming assignment, but in Python, and perhaps with minor modifications to the assignment spec.
Participation is purely optional. The reward for doing these projects would be small; right now, we're thinking of rewarding these students with 1 free late day for each Python program completed. No grade points will be added or subtracted in any way for participating in this project.
Just go to the next Python session at the time listed above, and if you find it interesting, try writing the Python program. If you finish it, you can turn it in from a link that we'll put at the top of this page.