March 1st, 2021
Below is a historgram of the results.
Key Statistics
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 Monday March 8th 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
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
Reminder of grid functions:
grid = Grid.build([['row', '0'], ['row', '1']])
grid.in_bounds(x, y) - True if in bounds
grid.get(x, y) - returns contents at that x,y, or None if empty
grid.set(x, y, value) - sets new value into grid at x,y
Say we have the grid from the Sand homework, so every square is one of 's', 'r' or None. Write code for an is_sandy(grid, x, y) function as follows:
Given a grid and an x,y which is guaranteed to be in-bounds.
We'll say that x,y is "sandy"
If there is sand
either diagonally above-left, or diagonally below-right, or both from that x,y.
Return True if the given x,y is sandy, False otherwise.
For example in the grid shown below, the two 'r' squares are the only ones where is_sandy() would return True.
def is_sandy(grid, x, y):
if grid.in_bounds(x - 1, y - 1) and grid.get(x - 1, y - 1) == 's':
return True
if grid.in_bounds(x + 1, y + 1) and grid.get(x + 1, y + 1) == 's':
return True
return False
This problem is somewhat similar to the Crypto homework. This encryption will not make any adjustments for upper vs lower case.
Given source and slug lists of chars which are the same length. Compute and return the encrypted form of a char as follows: if the char appears in the source list, return the char at the next index in the slug list. So if the char is at index 2, its encrypted form is at index 3 in the slug list. Except, if the char is at the last source index, then its encrypted form is first char in the slug list. If the char does not appear in the source list, then return it unchanged.
Say we have these len-4 source and slug lists source = ['a', 'b', 'c', 'd'] slug = ['w', 'x', 'y', 'z'] encryption: 'a' -> 'x' 'c' -> 'z' 'd' -> 'w'
def encrypt_char(source, slug, char):
if char in source:
idx = source.index(char)
if idx == len(source) - 1:
return slug[0]
return slug[idx + 1]
return char
An "a1" word starts with a lowercase 'a' followed by a combination of digits and the char 'a'. Write nested loops to parse out and return a list of all the a1 word strings from the given string.
'x a123 xaa9ax' -> ['a123', 'aa9a'] 'a a9 999' -> ['a', 'a9'] 'x aaa' -> ['aaa'] '123' -> []
def parse_a1(s):
search = 0
words = []
while True:
start = s.find('a', search)
if start == -1:
break
end = start + 1
while end < len(s) and (s[end] == 'a' or s[end].isdigit()):
end += 1
word = s[start:end]
words.append(word)
search = end
return words
Reminder: the canvas draw_line function:
canvas.draw_line(x1, y1, x2, y2) - draws a line from x1,y1 to x2,y2
As on the Quilt homework, the function takes in the (left, top, width, height) of a figure positioned on the canvas, and draw lines within that figure.
Draw a blue rectangle at the outer edge of the figure (this code is included).
Given int n which is 2 or more. Draw a series of lines distributed across the top edge of the figure, all terminating at the lower-right corner. The first line should start at the upper left corner, the last line should start at the upper right corner, with the other lines distributed evenly in between.
def draw(canvas, left, top, width, height, n):
canvas.draw_rect(left, top, width, height, color='lightblue')
for i in range(n):
x_add = i / (n - 1) * (width - 1)
# to bottom-right
canvas.draw_line(left + x_add, top, left + width - 1, top + height - 1)