CSCI0050 Python Programming and State
CSCI0050 Homework: Python Programming with Variables

Everybody should do the first two sets of exercises (Data Design and Program Tracing), then one of the two Programming options (Standard or Advanced). Pick whichever programming option matches your level and/or interests.

Collaboration Policy: You may work on this assignment with others. Include a collaboration statement describing how you did the work.

Data Design Exercise (No Programming)

Put your answers to this in a plain text file named email.txt.

Imagine that you were asked to build an email-system. The system has users, each of which has a real name, a username, and a password to the system. Each email message has a sender, one or more recipients, when it was sent, a subject, and its contents (ignore attachments for this assignment). For simplicity, we’ll assume that all senders and recipients are part of the same system (ie, that everyone has a gmail address, for example, rather than some in gmail and some in yahoo).

The system tracks which messages each recipient has read. A user can ask to see just their unread messages, or all of their messages.

The system needs to support people sending new messages, reading messages, and deleting messages. (We will ignore replies for now, treating each "reply" as a new message).

What data structures would you propose for maintaining information on users, email messages, and which messages have been read (by which recipients)? Your answer should specify which kind of data (lists, tables, data blocks, tuples, numbers, etc), including the types of any subcomponents or columns. You can think through this in either Pyret or Python (your choice). We just want to see how you envision organizing the data, with an eye towards supporting the operations that people want to do.

You are not being asked to write the code for the email system. This is only a question about the data structures you would use.

Tracing Program Execution (No Programming)

Put your answers to this in a plain text file named tracing.txt.

For each of the following two programs, show the memory contents at each point marked "memory point here" in the program (that point may be reached more than once, depending on how the program executes). Present your contents as a series of mappings from names to values (written in comments), in the form of:

  x --> 5

  y --> [1, 2, 3]

  z --> the same list that y points to

Python Programming: Standard

An environmental agency gathers and tracks data on air-quality. The agency collects readings of key air-quality factors from various locations, combines the data, and uses it to issue reports about which locations have concerning air-quality data. In the USA, air-quality is tracked through 5 measurements: this assignment will work with two of them: ozone levels, and sulfur dioxide (so2) levels.

Here are data types and tuples for air-quality readings. Use these as part of solving the problems that follow.

  Date = NamedTuple("Date", [('month', int), ('day', int), ('year', int)])

  Reading = NamedTuple("Reading", [('type', str), ('date', Date),

                                   ('level', float), ('location', str)])

  DateType = TypeVar('DateType', int, int, int)

  ReadingType = TypeVar('ReadingType', str, DateType, float, str)

The type float is for non-integer numbers (like 3.14).

Python Programming: Challenge

If you don’t feel comfortable writing loops or assigning variables, do the standard problems instead. This problem set is more a test of your ability to write functions over complex data structures.

An IT company wants to be able to write programs that explore directories of files. Think of the directories (folders) on your own computer: you have folders, each of which may contain files and other folders. This is again a form of tree-shaped data, similar to the family trees that referenced people’s children.

The core tuples are as follows. subdirs is the list of folders within another folder. (I didn’t make types for these because I haven’t had time to figure out how to make recursive types in Python).

  File = NamedTuple("File", [('name', str), ('size', int), ('contents', str)])

  Folder = NamedTuple("Folder", [('name', str), ('subdirs', list), ('files', list)])

In writing your code, you might want to be able to check whether a value is from a particular named tuple. If you need this, you can use isinstance (which returns a boolean) as follows:

  isinstance(3, int)

  isinstance(Time(12, 22), Time)

Warning: Think about recursion here!. You can make this problem harder than necessary by working with global variables. Imagine how you would have done this in Pyret (when you didn’t have assignment), then replace each function that would have iterated over a list with a simple for loop. These problems are very natural with recursion; you should NOT need any global variables.

Tips and Hints

Sorting Lists

Python has a built-in operator for sorting lists. If you write

  L = [2, 3, 1]



Python displays [1, 2, 3] – in other words, Python modifies the list to put the arguments in sorted order. When you have something more complicated that numbers, you have to tell Python which parts of the data to use for sorting, and in what order to consider the parts. Here is a function that sorts a list of dates into earliest to latest order (year most important, then month, then day):

  def sort_by_date(date_list : List[Date]) -> None:

      date_list.sort(key = lambda d: (d.year, d.month,

To get the list sorted from latest to earliest order, we add an argument to sort telling it to use reverse order:

  def sort_by_date(date_list) -> None:

      date_list.sort(key = lambda d: (d.year, d.month,,

                     reverse = True)

You should be able to modify these procedures as needed to sort a list of readings by date, as required in this problem.

Grading Expectations

We’ll be looking for appropriate choice of loops or operators and careful attention to test cases in grading this assignment. Use the file from class for writing tests.

What to Turn In

Submit a zip file with your files from parts 1 and 2, and a zip of your project folder from the programming questions.