Overview
With our upcoming midterm next Wednesay, here are some problems to get additional review with the matesrial covered thus far!
Bit
Here are a few functions on the experimental server to get some practice with Bit:
- Encave
- Bit is on flat ground, facing the right side of the world.
- Move bit forward zero or more squares until a hole appears below.
- Go down the hole to find a red square which marks the entrance of a horizontal cave.
- Move into the cave (towards the left side of the world) until blocked.
- Paint every square in the cave and the red square green.
- Climb
- Bit is facing the right side of the world. Bit is going to climb up the side of a cliff.
- Move bit forward until blocked, this is the left side of the cliff bottom.
- Move bit straight up the side of the cliff, until an open space appears to the right.
- Move bit to the open space.
- Paint blue every square that bit occupies during the climb upward.
Parameters and Returns
Here are a few functions to get some practice dealing with parameters and returns (in param_fn.py):
def is_even(n), which takes in an integer parameter n and returns True if n is even and False otherwise. You may wish to use the % operator, which returns the remainder when dividing one number by the other. For instance, 7 % 3 evaluates to 1, since the remainder when dividing 7 by 3 is 1.def is_prime(n), which takes in an integer parameter n and returns True if n is prime and False otherwise. As a reminder, a prime number is a number which has no factors other than 1 and itself.def only_one_even(num1, num2), which takes in two integer parameters and returns True if and only if exactly one of them is even.- (challenge)
def is_power_of_two(n), which takes in an integer parameter n and returns True if n is a power of 2 and False otherwise. A power of 2 is a number which can be produced by repeatedly multiplying 1 by 2 (and consequently, continuously dividing the number by 2 will lead back to 1).
Tracing
Without running the program, what does main print?
def foo(x, y):
z = y - x
print(x, y, z)
def main():
x = 5
y = 7
z = y
print(x, y, z)
y = x
print(x, y, z)
foo(y, z)
Images
Write the following function (in image_fn.py):
def rotate_image_left(image)that takes in a filename representing an image and returns an out version of that image, rotated 90 degrees to the left.- To run the full program in the terminal you would do:
python3 image_fn.py[IMAGE FILENAME]- Example:
python3 image_fn.py monkey.jpg
Note that the source image's width may be different from its height, and that the dimensions of the result image reflect the rotation you have performed; the result image is as wide as the source was tall, and as tall as the source was wide.
Graphics and Animations
Here is a problem to review graphics and animations:
-
make_magic_circlesWrite a graphics program that does the following:Whenever the mouse is clicked, your program should draw a circle centered at the location of the mouse click with a random color and random radius between MIN_RADIUS and MAX_RADIUS
One potential breakdown is the following:
- Write a function called
draw_circlethat draws a circle centered at (x, y) given an x, y coordinate, radius and color. - Write a function called
draw_random_circlethat draws a circle of random radius and random color in COLORS given a canvas, and x,y coordinate representing the center. - In main, create the animation loop that waits for the click, grabs the mouse coordinates, and then draws a random circle at those coordinates.
Lists and Functions
def append_evens(n): Given some non-negative integer parameter, create a list containing all even numbers between 0 and n (inclusive) in descending order.
Tracing Answer:
5 7 7
5 5 7
5 7 2