Quiz 1: Solutions

April 21st, 2021

Below is a historgram of the results.

Key Statistics
• Median: 48.0
• Mean: 45.64

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

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

Bit is in a world that contains a single rectangle of filled squares. Bit is to the right of the rectangle, facing a block on its right side as shown in this representative example:

Move bit forward to the edge of the rectangle. Then move bit down past the lower right corner of the rectangle. Finally, move bit to the left side of the world, painting every square occupied blue. Your code should work for any position and size rectangle, and bit will always start facing a block on the right side of the rectangle as in the example above.

Solution to Problem 1
  
def do_quiz(filename):
bit = Bit(filename)
while bit.front_clear():
bit.move()
bit.left()
while not bit.right_clear():
bit.move()
bit.paint('blue')
bit.right()
while bit.front_clear():
bit.move()
bit.paint('blue')




Problem 2: Images (30 Points)

Given an image file and 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 yellow rectangle a pixels wide and b pixels high at the top-left of the out image. Below the yellow rectangle, place a vertically flipped copy of the original image. The width of the original image will be greater than a, so the flipped image will be wider than than the yellow rectangle. Leave the area to the right of the yellow rectangle blank.

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

1. Create the out image
2. Color the yellow rectangle
3. Copy the vertically flipped image
Solution to Problem 2
  
def do_image(filename, a, b):
image = SimpleImage(filename)
# a Create the out image
out = SimpleImage.blank(image.width, 2*image.height)

# b Color the yellow rectangle
for y in range(b):
for x in range(a):
pixel = out.get_pixel(x, y)
pixel.blue = 0

# c Copy the vertically flipped image
for y in range(image.height)
for x in range(image.width)
pixel = image.get_pixel(x, y)
pixel_out = out.get_pixel(x, b + image.height - 1 - y)
pixel_out.red = pixel.red
pixel_out.green = pixel.green
pixel_out.blue = pixel.blue




Problem 3: Strings (15 Points)

alpha_star(s): Given a string s. Construct and return a string that, for every alphabetic char in s, has that alphabetic char with an asterisk '*' in front of it. Ignore non-alphabetic chars. So for example, the string '\$a@@bC66' returns '*a*b*C'. Use a for/i/range loop to look at every char in s.

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