Variables & Control Flow


Images: Blur

This problem comes from a past CS 106A assignment. As a result, it's a little more involved than what we would reasonably put on an exam. That said, we're including it in the handout because it's a great overview of the different kinds of things you'd need to do in image problems.

In this problem you are going to write a function that simulates a filter on an image. This filter is going to blur the given image. To do this, implement the following method:

def blur(img)

Which takes as parameter a SimpleImage and returns a blurred version of that image. One way to do this is to create a new image whose pixel values are averaged with the values of their immediate neighbors from the source image; this simulates a "blurring" effect between pixels.

The general idea is that for a pixel located at row r and column c in the source image, you will set its red, green, and blue components to be the average (rounded down to the nearest integer) of the nine red, green, and blue components in the pixels at locations (r-1, c-1) through (r+1, c+1).

For example, in the diagram below, the pixel (row 1, column 2) should be modified to store the average of the nine pixels (0, 1), (0, 2), (0, 3), (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), and (2, 3). These are the eight neighbors of (1, 2) as well as (1, 2) itself. So the red part of (1, 2) would be changed from 32 to (84+74+16+66+32+95+28+47+31)/9 = 52. The green component would be changed from 67 to (22+38+17+53+67+65+49+21+41)/9 = 41. The blue component would be changed from 12 to (99+69+18+88+12+35+31+94+51)/9 = 55. Therefore the overall pixel value at (1, 2) in the result image would be (52, 41, 55).

Blur diagram

A special case is the set of pixels along the edges of the image. When blurring those pixels, they do not have eight neighbors like other pixels do, so the average includes fewer data points. For example, in the diagram above, the pixel at (0, 0) has no neighbors above or left of it, so it should become the average of the four pixels (0, 0), (0, 1), (1, 0), and (1, 1). So the red component of (0, 0) would become (14+84+21+66)/4 = 46, and so on. The pixel at (3, 3) has no neighbors below it, so it should become the average of the six pixels (2, 2), (2, 3), (2, 4), (3, 2), (3, 3), and (3, 4). The red component of (3, 3) would become (47+31+246+15+60+188)/6 = 97, and so on. Take care that your algorithm does not crash by trying to access outside the bounds of the image.

A common bug in this algorithm is to try to modify the image in-place. You should not do this; you should create a new second pixel image to store the result image's pixels. The reason is because you don't want modifications made to one pixel to impact another pixel in the same pass over the image. In our previous example, we already stated that pixel (1, 2) should be changed from (32, 67, 12) to (52, 41, 55). But if you store (52, 41, 55) into this pixel and then use that value for further calculations on pixels in the same pass over the array, their averages will be incorrect. For example, when computing the average for pixel (1, 3), the pixel (1, 2) is one of its neighbors. But you should use that pixel's original value of (32, 67, 12) when computing that average.


Drawing


Strings


Grids


Lists: Camel Casing

Lower camel case is a way to write phrases--used commonly for variable naming in programming--such that each word other than the first in the phrase starts with a capital letter and the words are not separated by spaces. The first word in the phrase is not capitalized in lower camel case. Some examples of words in lower camel case are iPhone, lowerCamelCase and eBay

Specifically, implement the following function:

def make_camel_case(phrases)

which takes as parameter a list of phrases where a phrase is a string of two or more words separated by spaces, and returns a list of strings where each string is the camel case version of the corresponding phrase. You may assume that each string won't have any punctuation or non-space whitespace. For example, make_camel_case(['camels are awesome', 'live love camel case']) would return the list ['camelsAreAwesome', 'liveLoveCamelCase'].


Parsing


Files and File Reading


Dictionaries


Tuples: student residences

Given a list of tuples all_housing_assignments in which the first value is a student's name and the second value is the name of a dorm they have lived in, write a function map_students_to_dorms to create a and return a dictionary which associates each student with a list of all dorms they have lived in throughout their time at Stanford. The list contains undergraduates of all years so some students may only have one residence while others may have multiple.


Sorting, lambdas and List Comprehensions

Each of these problems should be solved with one line of code.


More sorting: stock prices

You are hired to help a company analyze their stock. To start, they give you a list of tuples in which the first element is an int representing the time in minutes since the stock market opened that day, and the second value is the price of the company's stock at that time. They have different lists for each day and the lists are in no particular order (you can see they definitely need your help). To save space, a tuple value is only created and stored in the list if the stock price has changed (either increased or decreased) since the previous value was recorded. The value at T=0, or the time when the stock market opens, is always recorded.

First, implement the following function:

def find_first_and_last(stock_prices, a,b)

which returns a tuple containing the first and last stock prices within the given time interval where a is the start of the interval and b is the end. Both a and b are included in the time range.

Next, implement the following function:

def find_lowest_price(stock_prices, a,b)

which returns the time at which the stock had the lowest price in the given time interval.


Weather Map

This problem puts together various pieces of material from the class to solve a slightly more difficult problem. This is out of the scope of the final, but is a good way of testing that you fully understand each individual component.

The local weatherman has called in sick for the week. Unfortunately, we are not climatologists, but we can use our python programming skills to analyze the weather map and step in for the weatherman.

Given an image representing a weathermap, find all of the areas that are going to be severely impacted by the incoming snowstorm. An area is predicted to be hit by the snowstorm if its blue pixel component value is at least 128.

To help, you are given the function translate_coordinate_to_location(x,y) which takes as parameters the x and y coordinate of a given pixel, and returns a string representing the name of the location that the pixel represents on the weathermap image.

Your job is to implement the following function:

def help_weatherman(img)

which takes as parameter an image of a weather map and returns a list of all towns/cities that are going to be affected by the incoming snow storm. To make this list useful, make sure to only include the name of each town or city once in the list. You may assume you have a constant MINIMUM_BLUE_THRESHOLD that is equal to 128.