UW CSE logo uw

CSE 143: Computer Programming II, Winter 2009

arrow CSE Home arrow About Us arrow Search arrow Contact Info

CSE 143: The Python Strikes Back

Wall of Fame:

The following students wrote awesome Python games:

Past Slides:

Homework 6: Anagrams
Due: Fri 3/6, 11:30pm. Turn in HW6P here.
No late turnins accepted.

In unit 6 of the course we learned about recursive backtracking. Recursive backtracking is basically the same in Python; a method can call itself. There is no special syntax for it. The main change involved is in the types of collections used; we often just use lists in Python where in Java we would have used sets, stacks, arrays, etc.

For your assignment this week, create a Python file called anagrams.py that defines a class GrammarSolver much like in the Java Homework 6. Our solution to this program is 60 lines long.

Define the following operations in your class. These are described below in terms of the way they would be called by a client. In your code you must also declare a self parameter for each method.

In this assignment you will interact with provided LetterInventory objects, so you should say the following at the start of your file: from letterinventory import *

A LetterInventory object has the following constructors and methods:

Files:

Homework 5: Grammar Solver
Due: Fri 2/27, 11:30pm. Turn in HW5P here.
No late turnins accepted.

In week 5 of the course we learned about recursion. Recursion is basically the same in Python; a method can call itself. There is no special syntax for it.

For your assignment this week, create a Python file called grammarsolver.py that defines a class GrammarSolver much like in the Java Homework 5.

Define the following operations in your class. These are described below in terms of the way they would be called by a client. In your code you must also declare a self parameter for each method.

Files:

Links:

Homework 4: Assassin
Due: Sat 2/14, 11:30pm. Turn in HW4P here.
No late turnins accepted.

In week 4 of the course we learned about implementing linked lists. Python can do much the same thing, since variables that store objects in Python also do so by reference. So you can create a linked list in Python by creating many node objects, each with a next reference to another node.

The equivalent of null in Python is called None, and it behaves much the same way. You can test whether a variable is equal to None (best done by writing: if myVariable is None: ... rather than using ==), and you can store None in a variable, but None is not an object and cannot be dereferenced.

We are also going to practice exceptions in this assignment. To throw ("raise") an exception in Python, write the following:

raise Exception("your message here")

For your assignment this week, create a Python file called assassinmanager.py that defines a class AssassinManager much like in the Java Homework 4. You must implement your class using a collection of linked nodes; you may not create any Python lists or other collection objects.

Define the following operations in your class. These are described below in terms of the way they would be called by a client. In your code you must also declare a self parameter for each method.

You will interact with AssassinNode objects, so you should say the following at the start of your program: from assassinnode import *

An AssassinNode object has the following constructors and fields:

Files:

Links:

Homework 3: Stable Marriage
Due: Wed 2/11, 11:30pm. Turn in HW3P here.
No late turnins accepted.

In week 3 of the course we learned about using Java's Set and Map collections. Python has two built-in types called set and dict (short for "dictionary") that correspond to Sets and Maps. You can create a new empty dictionary by storing the value {}. You can store a key/value pair using [] brackets, such as my_map["Marty"] = "Stepp". You can use the len function to see how many key/value pairs are in a set or dictionary. You can use if ... in and for ... in to ask whether a dictionary contains a key or to loop over the keys respectively. Sets and dictionaries have other methods and properties you can read about from the links below.

For your assignment this week, create a Python file called matchmaker.py that defines a class MatchMaker much like in the Java Homework 3.

Define the following operations in your class. These are described below in terms of the way they would be called by a client. In your code you must also declare a self parameter for each method.

You will interact with Person objects, so you should say the following at the start of your program: from person import *

A Person object has the following useful members:

Files:

Links:

Homework 2: HTML Validator
Due: Wed 1/28, 11:30pm. Turn in HW2P here.
No late turnins accepted.

In week 2 of the course we learned about using Java's collections such as lists, stacks, and queues. Python's built-in list type essentially serves as all of these structures. A list can serve as a queue just by looping over its elements. It can serve as a stack by using its append method to push and its pop method to pop. To peek, just examine the element at index [0] for a queue or the element at index [-1] for a stack.

For your assignment this week, create a Python file called htmlvalidator.py that defines a class HtmlValidator much like in the Java Homework 2.

Define the following operations in your class:

You will interact with HtmlTag objects, so you should say the following at the start of your program: from htmltag import *

An HtmlTag object has the following methods:

Files:

Links:

Homework 1: SortedIntList
Due: Wed 1/21, 11:30pm. Turn in HW1P here.
No late turnins accepted.

In week 1 of the course we learned about the idea of collections and implemented an ArrayIntList collection together. Python also has collections built in; in particular, its built-in list type is much like an ArrayIntList, but it can store any type of data.

For your assignment this week, create a Python file called sortedintlist.py that implements a class SortedIntList. Each SortedIntList object stores a sorted list of values. You should extend our provided ArrayIntList class from the file arrayintlist.py. (The file names are misnomers because you could store any type of sortable value in the lists.) Your file should work with the provided testlist.py. You'll want to review the syntax for classes and (briefly) inheritance from weeks 8-9 before writing this program.

Define (and/or override from ArrayIntList) the following operations on your list:

To do a binary search in Python, use the bisect, bisect_left, or insort functions. Documentation for these functions is at the link below. To use them, remember to write: from bisect import *

Since the provided testlist.py is not a very good or exhaustive test, you are encouraged to write your own testing code to make sure your list works. If you like, you can turn in your own testlist.py or share your testing code with others on the message board.

Files:

Links:


General info about the CSE 143 Python program:

This quarter in CSE 143, 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.

This program is targeted at people who already participated in our Python program during CSE 142 in a past quarter. If you didn't, you can still try to follow along, but it may be more difficult.

What 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.

Why would I want to learn Python, in addition to Java?

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.

In addition, Python is used in a lot of other fields and disciplines, so it can be useful to have experience in it. Lastly, Python is a powerful language that does a lot of things more easily than Java, so it can be fun rewriting your past Java programs in Python and seeing how much shorter and cleaner they can be solved.

What will I do if I participate in this program?

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.

What reward do I get for doing this? Do I have to do it?

Participation is entirely optional. The reward for doing these projects will be small, to make sure that Python doesn't give students with prior experience an unfair advantage over new programmers. Right now, we're planning to reward students with 1 free late day for each Python program submitted. No grade points will be added or subtracted in any way for participating in this project.

How do I participate or learn more?

Just look at the slides and/or links above, and if you find it interesting, try writing the Python program for that week. If you finish it, you can turn it in from a link that we'll put up above on this page.

Students who are interested but did not do Python in 142 can also go to CSE 142's Python sessions this quarter. The sessions are held on Tuesdays from 3:30 - 4:20pm in SIG 134. (But most of the people there will be 142 students, and you'll know more programming than they do, so you have to behave yourself; don't be too much of a know-it-all and scare the 142 kids away!)

If you want to install and run Python programs on your own computer, follow our Python Installation Instructions below: