Quiz 1: Solutions

July 13th, 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.

Key Statistics

  • Median: 46.0
  • Mean: 43.71
  • Maximum: 50.0

You can find your answers on Gradescope. Let Tara 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 July 16th at 2:30pm PT. 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 Juliette or Tara's office hours, post on Ed, and come to LaIR.

I wasn’t able to solve many of the problems on the quiz. 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.

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 (20 Points)

There are two 1-wide holes a few squares apart on a flat landscape. Bit is at the bottom of the right hole facing up. Move bit over to the bottom of the left hole facing down. Paint every other square of the left hole blue, starting with the square above the hole. You may only use features from the Bit lectures (all allowable features can be found in the Bit Reference guide on the course website). Your code must for work for all worlds that fit this description (eg. a longer/shorter holes, or more/less distance between the holes).

Before:

After:

Solution to Problem 1
  
def bit_quiz1(filename):
    bit = Bit(filename)
    # move to the top of the right hole 
    while not bit.left_clear(): # or not bit.right_clear()
        bit.move()
    
    # rotate 90 degress
    bit.left()
    bit.move()
    
    # find the left hole
    while not bit.left_clear():
        bit.move()
    
    # face the bottom of the hole 
    bit.left()

    # checker the left hole
    bit.paint('blue')
    while bit.front_clear():
        bit.move()
        if bit.front_clear():
            bit.move()
            bit.paint('blue')
  

Problem 2: Images (20 Points)

Given an image filename and two int parameters a and b which are 0 or more, create and return a new out image as follows (see diagram below). The out image should have a vertical yellow stripe on the left side that is a pixels wide and b pixels taller than the original image. Next to the vertical yellow stripe, color a horizontal magenta stripe that is b pixels tall. Below the stripe, place a horizontally flipped copy of the original image. You do NOT need to make the black border around the out image that is shown in the diagram below. That is just to show you the dimensions of the out image.

Image:

Out:

The outline of the code is provided, fill in the missing bits of code:

1. Create the out image

2. Color the left yellow stripe

3. Color the top magenta stripe

4. Copy the horizontally flipped image

Solution to Problem 2
  
def do_image(filename, a, b):
    image = SimpleImage(filename)

    # 1) Create the out image
    out = SimpleImage.blank(image.width + a, image.height + b)

    # 2) Color the left yellow stripe
    for y in range(image.height + b) # or range(out.height)
        for x in range(a):
            out_pix = out.get_pixel(x, y)
            out_pix.blue = 0
    
    # 3) Color the top magenta stripe
    for y in range(b):
        for x in range(image.width):
            out_pix = out.get_pixel(x + a, y)
            out_pix.green = 0

    # 4) Copy the horizontally flipped image
    for y in range(image.height):
        for x in range(imgage.width):
            pixel = image.get_pixel(x, y)
            out_pix = out.get_pixel(image.width - 1 - x + a, y + b)
            out_pix.red = pixel.red
            out_pix.green = pixel.green
            out_pix.blue = pixel.blue

    
    return out

  

Problem 3: Strings (10 Points)

digits_plus(s): Given a string s, construct and return a string that, for every digit character in s, has that digit character followed by a '+' character. Ignore any non-digit chars. For example, the string 'CS106A' returns '1+0+6+'.

Solution to Problem 3
  
def digits_plus(s):
    result = ''
    for i in range(len(s)):
      if s[i].isdigit():
        result = result + s[i] + '+' # or result += s[i] + '+'
    return result
  

That’s all folks! Thanks for your hard work