CS106A Quiz Preparation

We're having our first quiz soon, timing and logistics details on the course page. The quiz will be short, 15 minutes or fewer, and the format will involve writing solution code to little coding problems as seen below. We're going to use a system where you type code on your computer, but the format is the same as the paper-style questions below. We will grade the problems by hand, giving partial credit etc., just as with a paper exam.

The quiz will concentrate on coding problems very similar to the functions you have seen:

Topics on quiz-1: Bit code, 2-d image loop code, basic int and string functions, Doctests of those functions (Basically, hw1, hw2, and through first half of lecture-8)

Not on the quiz: Grid, decomposing a problem into functions, code style, using the command line, using PyCharm. Either we have not done enough of these yet, or they are more naturally homework topics than quiz topics.

The quiz problems will use the same sorts of patterns you have seen on the lecture examples and homework problems. So studying and practicing with those examples is a good way to study for the quiz.

The exam grading will focus on getting the correct output, so if you use a 'for/range' to get the right output, and our solution uses a 'while' .. either solution can get full credit so long as it works correctly. Sometimes the problem statement will indicate a particular code structure we want you to use.

How To Study for a Coding Exam

What are we trying to accomplish with CS106A? We want everyone to pick up core coding knowledge and skills. These core skills are what we do in lecture, and section, and then on the homeworks. The exams are .. the exact same topics. The exams, lectures etc. .. all resemble each other. The topics on the exam are predictable. Predictable = studying will pay off.

Three levels of working a code concept...

1. See a code concept in lecture

2. Write code in section or on homework while looking at lecture examples

3. Write code on blank sheet of paper exam

These 3 are very different. It's a trap to get stuck at step 2. Your brain will just say "oh yeah, I see how to do that." The blank sheet of paper has a way of emptying out all that stuff you thought you knew. The good news..

(1) You can repeatedly practice these code patterns so you really know them. (2) The lecture examples and homework and section problems provide a lot of practice problems. (3) We will be extremely forgiving about wrong syntax when grading. We want the right algorithmic ideas, and we give partial credit for partially correct code (unlike the autograder!).

How to study:

Open Note / Internet

During the exam, you are free to look at any of our published examples or your own work or any resource on the internet. You are not allowed to collaborate with other people, either during the exam, or sharing information about the exam afterwards. There is some older "do not look at the internet" text in the Bluebook software which you can ignore. Bluebook has not been updated for Covid times, but it works well otherwise so we are happy to use it.

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

> go-green

> Change-Blue

> Fix Tree

> 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:

> Fill Example

> Cover Example

> Hurdles

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.

> Darker Nested

> Darker Out

> Aqua Stripe

> Mirror1

> Mirror2

> Shrink

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:

> Experimental Server Functions

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.


b1. 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.




g3. 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)
    # your code here

g4. A sort of funhouse-mirror effect governed by the int parameter n: e.g. if n is 2, squeeze width by 2x. Create a new 'out' image the same height as the original, but with width divided by int n. Copy the original image to out, squeezing it horizontally. For example for n=4, out pixel at x=0 comes from original x=0, x=1 comes from x=4, x=2 comes from x=8, and so on. n=1 should copy the image without squeezing.

squeeze_width(filename, n):
    # some boilerplate provided
    image = SimpleImage(filename) 
    out = SimpleImage.blank(...)


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):

f1. 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):

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' ```



def quizy(filename):
    bit = Bit(filename)
    # You code here
    while not bit.left_clear():
    while bit.front_clear():



def mirrorish(filename):
    image = SimpleImage(filename)
    # your code here
    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 squeeze_width(filename, n):
    image = SimpleImage(filename)
    out = SimpleImage.blank(image.width // n, image.height)
    # strategy: iterate x,y over the out pixels, compute
    # scaled * n pixel in original image.
    for y in range(out.height):
        for x in range(out.width):
            pixel_out = out.get_pixel(x, y)
            pixel = image.get_pixel(x * n, y)
            pixel_out.red = pixel.red
            pixel_out.green = pixel.green
            pixel_out.blue = pixel.blue
    return out


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
    # Start with s, loop to add on
    result = s
    for i in range(n):
        # Add last char of s
        result += s[len(s) - 1]
    return result