Quiz 1: Solutions

February 3rd, 2021


The purpose of the quiz was to help you self assess where you can improve. Every single person in this class has some area of the material where they could use some extra practice. In this document we are going to go over each of the problems in the diagnostic.

Below is a historgram of the results.


Key Statistics
  • Median: 48.0
  • Mean: 45.87
  • Standard Deviation: 11.19

You can find your answers on Gradescope. Let Juliette know if you do not have access to Gradescope.

Regrade Requests
We try to grade as consistently as possible, but we are human and we might make mistakes. If you feel like one of your problems was misgraded, please file a regrade request on Gradescope before Wednesday February 10th at 2:30pm. Note that this is the only way to have your regrade request considered; in particular, asking your section leader to take a quick look to see whether a problem was misgraded isn't a way of short circuiting this process. We want you to have the credit you deserve, but filing a formal request helps us make sure that your request goes to the right person.

Can I get extra help?
Yes! Come to Nick or Juliette's office hours, post on Ed, and come to LaIR.

How can I check my answers?
With this document you can check your answer. For each problem we include

  1. Several common solutions
  2. Comments which explain the solutions
  3. The major concepts that the problem covers
  4. Which readings and which lecture videos relate to the problem

I wasn’t able to solve many of the problems on the diagnostic. Should I panic?
Absolutely not! It is normal to find these problems hard. If you didn’t do as well as you would have liked you have a two step procedure. Step 1: make sure you know the underlying concepts (for example, using the i variable in a for loop). Then if you know the concepts but you weren’t able to come up with solutions in time the simple solution is practice and review! Many people incorrectly think that they are not good at this. False! We all can code. You just need more experience. Check out the review session.

My answer is different from the provided solution. Is my answer wrong?
Not necessarily. You will learn a lot by looking at the difference between the teaching team solution and your work. Your answer might be great. There are many solutions.

Can I discuss my solution on ed?
Yes! You are free to share your solutions


Problem 1: Bit (15 Points)

Bit is in a 1 square tall tunnel facing the right side of the world. Move bit forward zero or more squares until a clear square appears above - a vertical pipe. Move bit straight up the pipe until blocked, painting every other square green, starting with the square where bit notices the pipe.

Before:

After:

Solution to Problem 1
  
def quiz1(filename):
    bit = Bit(filename)
    while not bit.left_clear():
        bit.move()
    bit.left()
    while bit.front_clear():
        bit.paint('green')
        bit.move()
        if bit.front_clear():
            bit.move()
  

Problem 2: Images (30 Points)

Write the code for parts a, b, and c on the right hand side of this page. You can get the points for each part independently of the other parts.

Given an image file and int margin that is 0 or more, create and return a new out image as follows (see diagram below). (a) The out image should be created as blank white image as usual, with space for two columns added at its sides, each with the width “margin”. The left column is blank, the right column is colored purple. The purple color can be made with green = 0, red and blue = 255 as on the homework. (b) Between the columns, there should be a top area the size of the original image which is left blank. (c) Below the blank area should be a copy of the image which is reversed horizontally but unchanged vertically.

In the diagram, each of the 4 areas is shown with a black border to show where the areas are. There are no black borders in the actual output. The boilerplate code is provided to the right, fill in the missing bits of code:

  1. Create the out image
  2. for y/x loop code to color in the purple columns
  3. for y/x loop code to copy in the reversed image
Solution to Problem 2
  
def quizy2(filename, margin):
    image = SimpleImage(filename)
    
    #### Part a Create out ####
    out = SimpleImage.blank(image.width + margin * 2, image.height * 2)
    
    #### Part b Color in column ####
    for y in range(image.height * 2):
        for x in range(margin):
             # right_col
             pixel_r = out.get_pixel(margin + image.width + x, y)
             pixel_r.green = 0
    
    #### Part c Copy in reversed ###
    for y in range(image.height):  # loop over original
        for x in range(image.width):
            pixel = image.get_pixel(x, y)
            pixel_out = out.get_pixel(margin + image.width - 1 - x, image.height + y)
            pixel_out.red = pixel.red
            pixel_out.green = pixel.green
            pixel_out.blue = pixel.blue
    return out

  

Problem 3: Strings (15 Points)

Given a string s. Compute a string with 2 copies of each “alphabetic” char in s. Recall that char.isalpha() tests if a char is alphabetic. Return the computed string with a ‘@’ added at its beginning and end.

So for example: alpha2x(‘ab12x’) returns ‘@aabbxx@’.

Solution to Problem 3
  
def alpha2x(s):
    result = '@'
    for i in range(len(s)):
      if s[i].isalpha():
        result = result + s[i] + s[i]
    result += '@'
    return result
  

That’s all folks! Thanks for your hard work