January 30th, 2022
Written by Juliette Woodrow, Brahm Capoor, Nick Parlante, Anna Mistele, and John Dalloul
This week in section you will work through problems that will help you study for the midterm. This handout only has a few problems that we think are great practice problems. Later this week, we will release another handout with more midterm review problems.
Please take the first few minutes of section to fill out the mid-quarter evaluation. Nick and Juliette read each and every piece of feedback that you provide so that we can adjust the course to best meet your needs. Mid-Quarter Evaluation
For each ??, write what value comes from the Python expression on the above line.
>>> 1 + 2 - 3 + 4 ?? >>> >>> 3 + 4 * 2 ??
Similar to the above problem, this is a tracing problem. Rather than copying it into Pycharm and running it to see what it prints out, we are going to trace through it by hand to see how values are passed between functions. This may seem like a silly exercise but tracing through code is a good skill to have, especially when you take the midterm as you will not be able to run your code.
def main():
x = 4
y = 8
z = x + y
y = b(x, z)
print("Main variables: ", x, y, z)
def b(y, x):
z = x + y
print("b variables: ", x, y, z)
if z % 2 == 0: # if (x+y) is even
return z + 1
return z // 2
Write code for the follwing functions:
double_digit(s): Given string s. Return a string made of two copies of each digit char in s, so '42ab3$' returns '442233'
def double_digit(s):
pass
First we will describe the double-slug encryption system used in this problem, then you will write the decrypt function below. As a simplification, we will assume that uppercase chars do not exist for this problem.
Double-slug encryption uses one source list and two slug lists, all containing lowercase chars. The source list is even length, and the two slugs are each half its length. The slug1 list holds the encrypted form of chars in the first half of the source list. The slug2 list holds the encrypted form of chars in the second half of the source list. No char is in both slug1 and slug2. Here is an example with a length-4 source list:
Encrypt examples with source/slugs above:
The encrypt of 'a' is 'd' The encrypt of 'c' is 'b'
Write the code for the decrypt function:
Given the lists of lowercase chars: source, slug1, slug2.
And given lowercase ch which is encrypted if possible.
Return the decrypted form of ch, or return ch unchanged if it
is not encrypted (e.g. '$'). The index in the source list where
the slug2-encryption begins is at midway = len(source) // 2,
which is included in the starter code.
Decrypt examples with source/slugs above:
The decrypt of 'd' is 'a' The decrypt of 'b' is 'c'
def decrypt(source, slug1, slug2, ch):
midway = len(source) // 2
pass
There are two functions for this problem, and you can get full credit for each function independent of what you do on the other function.
We have a Grid representing Yellowstone park, and every square is either a person 'p', a yelling person 'y', a bear 'b', or empty None.
a. is_scared(grid, x, y): Given a grid and an in-bounds x,y. Return True if there is a person or yelling person at that x,y, and there is a bear to the right of x,y or above x,y. Return False otherwise. (One could check more squares, but we are just checking two.)
b. run_away(grid): This function implements the idea that when a person is scared of a bear they they begin yelling and try to move one square left. Loop over the squares in the grid in the regular top to bottom, left to right order (this loop is provided in the starter code).
For each square, if is_scared() is True, (1) First set the person stored in the grid to 'y' (i.e. they are yelling). (2) Second, if there is an empty square to their left, then move the person to that square leaving their original square empty. You may call is_scared() even if you did not implement it.
def is_scared(grid, x, y):
pass
def run_away(grid):
for y in range(grid.height):
for x in range(grid.width):
pass