CS21 Week 14: Wrap-up and Recap
Week 14 Topics
Writing Classes wrapup
Working with two dimensional data
Let’s start with a brief recap/wrap of writing classes.
Much of this course has focused on variables that store a single item, or a one dimensional list of data. However, much of the data computers process is multi-dimensional. Today we’ll briefly explore two dimensional data using a list of lists.
We’ll restrict ourselves to a two dimensional data set consisting of multiple rows and columns of the same type, e.g., integers. Examples of similar data sets include image data (a 2D grid of pixel color values), or spreadsheet of lab grades for a class.
Consider the file
numbers.txt in your
inclass/w14-2Ddata folder. This file contains four rows of five columns each. The file
read_data.py shows how to read the data from a file and print the data in a nice format.
Note that we begin reading the file the usual way with
readlines() and then using
split(). Instead of building a list of objects, we build a list of lists. After returning the data, we can access a row
i and column
j in the grid using the syntax
grid[i] refers to a single row, but each row is a list of the columns in that row.
print_data, we see a way to nicely print out the grid by printing all the elements in one row in a single line. Here we use a slightly modified
end="" option. This tells python not to print a new line after printing a single element. In this way, we can print all the items in a row using the inner
j loop and then print one new line after the
j loop to move to the next row.
Exercise: Get Max by Row
Working with a partner, design and implement a
get_max_by_row function that computes for each row, the maximum value within the row. You should create a new list to hold the max value for each row (do not modify the grid). Once the max values for each row are found, return the list containing the max values. Your implementation should work for a grid of any size. Think about how you can determine the number of rows and number of columns in the grid.
Exercise: Get Max by Column
Repeat the exercise above, but compute for each column the maximum value in that column across all rows. How is this the same as the previous exercise, and how is it different.
Optional: Advanced file processing
numbers.txt contains just the integer values, but often data files have a header row describing each column, and perhaps a label associated with each row. Consider
labeled_number.txt which labels the columns with days of the week and each row with a salesperson. Each item in the grid is the number of sales for a particular day for a particular salesperson. Can you modify the read_data function to parse this data file? You should still just return the list of list of numbers, but you will conceptually need to ignore the labels when reading the data.
Today is primarily a wrap up of the course.
The course final will be Friday 6 May 9am-noon in Chang Hou Hall (Sci 101). The format will be similar to the quizzes (no computers or calculators) but longer. Typically there are 7-8 multi-part questions. The final exam is cumulative.
You do not need to memorize common methods of the list, string, file, or graphics classes. A list of common methods will be included in the exam.
Past quizzes and quiz study guides are good review guide. You may also attend any or all of the review sessions next Monday through Wednesday, 2p-3:30p in Sci 181. These review sessions will be an opportunity for you to ask questions about the course. Instructors will not be providing new content.
Quiz and course evaluations
You should have received a link to the course evaluation from Lauri Courtenay, our academic support coordinator. Lauri will release evaluations to the instructors after course grades have been submitted. Let us know how we did!
Thanks for a great semester.