CS 111
Spring 2018

Old version

This is the CS 111 site as it appeared on May 10, 2018.

Problem Set 7 FAQ

If you don’t see your question here, post it on Piazza or come to office hours! See the links in the navigation bar for both of those options.

General questions

  1. Do we need to include docstrings in all of our functions?

    Yes. For now and evermore!

  2. I keep getting index out of bounds errors. What should I do?

    This is an excellent opportunity to hone your debugging skills! If you get this error, it means you are trying to get an element of a list that is outside the boundary of the list. To solve this, look at what ranges of values your indices can have. If the ranges seem alright, add print statements to display the indices. For example, the following code prints the row index and column index of every cell that we look at:

    for r in range(len(grid)):
        for c in range(len(grid[0]):
            print('r  =', r, ' and c =', c)
            # the rest of the loop goes here
    

    When you get the index out of bound error, look at the values of r and c. This should point you in the direction of what’s wrong with your code.

  3. What are the differences between a grid, a matrix, and a collection of pixels?

    In this homework assignment, all three of these things are represented using a 2-D list.

  4. My function’s output doesn’t make any sense. What should I do?

    Most of the code you are writing in this assignment consists of small functions. When you are working with references and things don’t make any sense, you should get a piece of paper and draw out a memory diagram for your function. This is the same thing we did in class. Answering the following questions will help you debug your code.

    • What does each variable refer to?

    • Which lists and/or which components of a given list am I modifying in this function?

    • Which ones do I need to stay the same?

    Be sure to also read over the hints that we’ve given you before you start writing code.

  5. What does the following error message mean? TypeError: list indices must be integers, not list

    This error means that you have tried accessing an element of a list using an index that is not an integer. For example, If I want to get the element in a matrix at row r and column c, I could do the following

    r = 0
    c = 2
    
    x = matrix[r][c]
    

    but if I make one of the indices be a list, I will get a type error, because it doesn’t make sense to address a column via a list.

    r = 0
    r = [2]
    
    x = matrix[r][c]
    

    If you get this error, the best way to solve it is to do some debugging. Try to use print statements inside of your loops to see what values are assigned to your indices. This should help you find the case in which one of them gets the wrong type.

  6. How do I get the height and the width of a grid, matrix, or image?

    All three of these data structures are represented as a 2-D list. The height is given by the number of rows in the 2-D list, and the width is given by the number of columns:

    height = len(grid)
    width = len(grid[0])
    
  7. For one of my functions using 2-D lists, I’ve written code that should change the value of one or more cells in the 2-D grid, but the changes aren’t taking affect. What could I be doing wrong?

    Make sure that you are using the assignment operator (=) to make the changes, rather than the == operator.

    Instead of writing something like

    grid[r][c] == 1    # this does *not* change grid[r][c]
    

    you should write this instead:

    grid[r][c] = 1     # this *does* change grid[r][c]
    

Problem 2

  1. For the standard deviation, how do I compute the square root?

    You should use the math.sqrt() function. In order to do so, you will need to add the following line near the top of the file (if it is not present in the starter code):

    import math
    
  2. How do I approach the “time-travel” investment strategy (option 7)?

    Look over the diff function from lecture, which returns the smallest absolute difference between any value in one list and any value in another list. Then look at the subsequent function called diff_indices that computes the two list indices of the values that gave us this smallest difference.

    For this task, the indices of the list represent days. We are interested in finding the largest positive difference between any two prices and the days on which they occur. This problem is similar to diff_indices, so make sure to read it over and understand how it works.

    The hard part will be creating your nested for loops. Answering these two questions should help you get started:

    • For each stock price, what other prices should you compare it with?

    • Can you get the necessary prices by somehow slicing the original prices list? (Hint: This should give you the list for your inner for loop.)

Problem 3

  1. How do I access just the “inner” cells in the grid?

    To ensure that you only consider the inner cells, you will need to limit the ranges used by your nested loops so that they iterate over only those cells. Don’t forget that range can take two parameters–one indicating the starting value, and one that is one more than the desired ending value.

  2. How do I get the height and the width of the grid given to copy?

    See question 6 under General Questions above.

Problem 4

  1. What does it mean for a cell to be alive?

    A cell is alive if its value in the grid is equal to 1. Otherwise, it’s value is 0 and the cell is dead.

  2. When we write next_gen, can we assume that the cells on the boundary will be 0?

    Yes. And make sure that in next_gen you only call count_neighbors on cells that are inside the inner grid.

Problem 5

  1. How can I implement flip_vert?

    For this problem, you will first want to create a new image (you can use blank_image for this purpose). Next, you will go through every pixel in the original image, and decide where to copy it into the new image. When we flip an image vertically, every pixel will stay in the same column, but it will be put into a new row.

    Before you try to write code for this, it may help to draw a table that maps where pixels go in the flipped image based on their row in the original image. Suppose h is the height of the image. You will end up with a table that looks like this:

    row r in original     row in flipped
    -----------------     --------------
           0                 h - 1
           1                 h - 2
           2                 h - 3
    

    Given the table above, what is a general formula you could come up with to determine a pixel’s new row in the flipped image?

  2. How should I start reduce?

    As mentioned in the problem, one way to approach this problem is to have your nested loops to go through each pixel in the new image and copy the appropriate pixel from the old image.

    Answering the following questions should get you started.

    • What are the height and width of the new image that reduce will produce?

    • Which pixel from the old image will go at 0,0 (top left corner) in the new image?

    • Which pixel from the old image will go at row 0, col 1 in the new image?

    • Which pixel from the old image will go at row 1, col 0 in the new image?

    • Which pixel from the old image will go at r,c for any row r or column c in the new image?