# CS106A Quiz #1 Review Materials

Quiz #1 Date: April 16th, 2021

## Practice Exercises From Lecture and Section

For most of the problems below, you have watched someone walk through a solution, or you have solved it possibly glancing at a similar lecture example. Some of these will now seem very easy. That was fine then. But for the exam, you should be able to solve any of these without access to anything - just your knowledge of Python and maybe making a little drawing to think about your code. Below are section problems which make excellent practice. You should also be able to do the problems linked from each lecture, which often introduce the ideas then seen in section and on the homeworks.

Section Problems: all of the sections and their solutions are listed off our course page.

## 1. Bit

> all-blue (while + front_clear)

> change-blue (while + if/get_color)

> bit-rgb (while + first/last square)

> double-move (while + two moves in loop)

> fix-tree (while-until-color, our make-a-drawing example)

> reverse-coyote (while + right_clear)

> standard-coyote (while + left_clear + not)

> climb (multiple whiles)

> reverse-santa (multiple whiles)

> standard-santa (multiple whiles)

> falling-water (multiple whiles, even/odd issue)

Examples with multiple functions working together:

You should be familiar with your homework-1 solution code. The Checkerboard problem is a classic, difficult homework problem. However, we will note that quiz questions are typically smaller than the Checkerboard problem.

## 2. Image Loops

We're not doing the basic image-foreach, which we used in lecture to introduce images. The quiz will use nested-loop image code, like the homeworks.

You should also be familiar with your own hw2 solution code.

## 3. Functions + Strings

In week three, we've introduced basic functions, basic strings, and Doctests. The quiz does not include the Grid, which we will use on homework 3.

You can access the lecture examples and some additional examples on basic functions and strings at the following link to the experimental server:

## Sample Questions

Here are a few sample questions, mostly drawn from old exams. These are representative, but you should be familar with all the lecture examples and homework problems. The quiz will have just 3 or 4 problems, appropriate for something so short.

### Bit

Bit is in a 1 square tall tunnel facing the right side of the world. Move bit forward zero or more squares until a clear square appears above - a chimney Paint the square below the chimney opening green. Move bit straight up the chimney until blocked, painting every moved to square blue.

Before:

After:

Bit is in a 1 square tall tunnel facing the right side of the world. Move bit forward zero or more squares until a clear square appears above - a vertical pipe. Move bit straight up the pipe until blocked, painting every other square green, starting with the square where bit notices the pipe.

Before:

After:

def chimney(filename):
bit = Bit(filename)
pass

### Images

Given an image, create a new 'out' image which is three times as wide as the passed in image. Copy the original image to the rightmost third of the out image, but with left and right reversed.

def mirrorish(filename):
image = SimpleImage(filename)

Given an image, and an int n, create a new 'out image' which is n pixels taller than the given image. Add an n pixel 'hat' to the out image that is colored magenta. Copy the original image but horizontally and vertically flipped below the n pixel hat.

def funky_hat(filename, n):
image = SimpleImage(filename)

Given an image file and int margin that is 0 or more, create and return a new out image as follows (see diagram below). (a) The out image should be created as blank white image as usual, with space for two columns added at its sides, each with the width “margin”. The left column is blank, the right column is colored purple. The purple color can be made with green = 0, red and blue = 255 as on the homework. (b) Between the columns, there should be a top area the size of the original image which is left blank. (c) Below the blank area should be a copy of the image which is reversed horizontally but unchanged vertically.

In the diagram, each of the 4 areas is shown with a black border to show where the areas are. There are no black borders in the actual output. The boilerplate code is provided to the right, fill in the missing bits of code:

1. Create the out image
2. for y/x loop code to color in the purple columns
3. for y/x loop code to copy in the reversed image

def wave(filename, margin):
image = SimpleImage(filename)

#### Part a Create out ####
pass

#### Part b Color in column ####
pass

#### Part c Copy in reversed ###
pass

return out

### Functions

The lecture example functions are all representative quiz-type problems. Here are two examples.

digits_only(s): Given a string s. Return a string made of all the chars in s which are digits, so 'Hi4!x3' returns '43'. Use a for/i/range loop.

def digits_only(s):
pass

Given string s and int n which is zero or more. Return a version of the string with n copies of its last char added at its end. Use a for/i/range loop. If the string is too short to have a last char, return it unchanged.

def lastly(s, n):
pass

You could also have a question with this sort of function with Doctests, which would look like this. In this case, the Doctest is just a syntax for writing out the input and output of 1 test case.  >>> lastly('abc', 3) 'abcccc' 

alpha2x(s): Given a string s. Compute a string with 2 copies of each “alphabetic” char in s. Recall that char.isalpha() tests if a char is alphabetic. Return the computed string with a ‘@’ added at its beginning and end.

So for example: alpha2x(‘ab12x’) returns ‘@aabbxx@’.

def alpha2x(s):
pass

## Solutions

### Bit

def quizy(filename):
bit = Bit(filename)
# You code here
pass
while not bit.left_clear():
bit.move()
bit.left()
bit.paint('green')
while bit.front_clear():
bit.move()
bit.paint('blue')

def chimney(filename):
bit = Bit(filename)
while not bit.left_clear():
bit.move()
bit.left()
bit.paint('green')
while bit.front_clear():
bit.move()
if bit.front_clear():
bit.move()
bit.paint('green')

### Images

def mirrorish(filename):
image = SimpleImage(filename)
out = SimpleImage.blank(image.width * 3, image.height)
# loop x,y over original image
for y in range(image.height):
for x in range(image.width):
pixel = image.get_pixel(x, y)
# key: locate pixel in out image
pixel_out = out.get_pixel(out.width - x - 1, y)
pixel_out.red = pixel.red
pixel_out.green = pixel.green
pixel_out.blue = pixel.blue

def funky_hat(filename, n):
image = SimpleImage(filename)
out = SimpleImage.blank(image.width, image.height + n)
for y in range(n):
for x in range(image.width):
out_pixel = out.get_pixel(x, y)
out_pixel.green = 0
for y in range(image.height):
for x in range(image.width):
pixel = image.get_pixel(x, y)
pixel_bottom = out.get_pixel(out.width-x-1, out.height - y - 1)
pixel_bottom.red = pixel.red
pixel_bottom.green = pixel.green
pixel_bottom.blue = pixel.blue
return out

def wave(filename, margin):
image = SimpleImage(filename)

#### Part a Create out ####
out = SimpleImage.blank(image.width + margin * 2, image.height * 2)

#### Part b Color in column ####
for y in range(image.height * 2):
for x in range(margin):
# right_col
pixel_r = out.get_pixel(margin + image.width + x, y)
pixel_r.green = 0

#### Part c Copy in reversed ###
for y in range(image.height):  # loop over original
for x in range(image.width):
pixel = image.get_pixel(x, y)
pixel_out = out.get_pixel(margin + image.width - 1 - x, image.height + y)
pixel_out.red = pixel.red
pixel_out.green = pixel.green
pixel_out.blue = pixel.blue
return out

### Functions

def digits_only(s):
result = ''

# Loop over all index numbers
for i in range(len(s)):
# Access each s[i]
if s[i].isdigit():
result += s[i]

return result

def lastly(s, n):
if len(s) == 0:
return s

result = s
for i in range(n):
# Add last char of s
result += s[len(s) - 1]
return result

def alpha2x(s):
result = '@'
for i in range(len(s)):
if s[i].isalpha():
result = result + s[i] + s[i]
result += '@'
return result