CS 124
Fall 2023

Problem Set 11 FAQ

Problem Set 11 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)
            cell = grid[r][c]    # the value at row r and column c
    

    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. When I change one element of my grid, why do other rows change as well?

    This is likely caused by having a grid where each row refers to the same list. Every row in a grid should refer to its own list. When creating a grid, make sure that you follow an approach like the one that we use in the create_grid function.

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

  6. Do we need to change anything in hmcpng.py?

    No, you should not change any code inside of hmcpng.py.

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

  8. 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])
    

Problem 1

  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 8 above.

Problem 2

  1. In mult_rows, what type of value is the input called factor?

    factor could be either an int or a float. Your code should work the same either way. In the assignment, we give an example of each case.

  2. Why don’t our functions that modify a matrix need to return anything?

    These functions are given a reference to the matrix that is being modified. As a result, any modifications that the functions make to that matrix will still be there when the function returns.

    For example, consider the negate_matrix function that we’ve provided. This function modifies the matrix it is given by multiplying every element in the matrix by -1. The function doesn’t return the negated matrix because it doesn’t need to. All of its changes to the internals of the matrix will still be there after it returns.

    If this seems unclear, it may be helpful to draw out a call to negate_matrix using a memory diagram like the ones we did in lecture and the lab.

  3. Can we use list comprehensions in Problem 3?

    We don’t recommend doing so. When modifying a 2-D list, the imperative style of using loops should be easier.

  4. One of my functions for Problem 3 works in the shell, but it doesn’t work in main(). What I am doing wrong?

    If your helper function modifies a matrix, be sure you are passing it a reference to the matrix in main(). If your helper function returns a matrix (the transpose function), make sure that you change the matrix in main() to refer to this new matrix. Ask yourself: which variable in main() refers to the matrix that it is repeatedly displayed to the user?

Problem 3

  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?