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):
# your code here
pass
This problem is similar to the Crypto homework. As a simplification, there will be no uppercase chars in this problem - the source, slug, and char will all contain lowercase chars, or chars like '$' which do not have an upper/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. For example, 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):
# your code here
pass
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'] '9a12' -> ['a12'] '123' -> []
def parse_a1(s):
search = 0
words = []
while True:
# your code here
pass
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, this function takes in the (left, top, width, height, n) of a figure positioned on the canvas, and draws n 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 starting at points across the top edge of the figure, all ending 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')
# your code here
pass
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
def encrypt_char(source, slug, char):
# your code here
pass
if char in source:
idx = source.index(char)
if idx == len(source) - 1:
return slug[0]
return slug[idx + 1]
return char
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
def draw(canvas, left, top, width, height, n):
canvas.draw_rect(left, top, width, height, color='lightblue')
pass
# your code here
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)