BYU logo Computer Science

Midterm study guide

The midterm will cover:

  • Bit
    • while loops
    • if statements
  • Images
    • for loops
    • nested for loops
    • coordinate transformations
    • compositing images
  • Strings
    • looping
    • string tests
    • indexing
    • finding
    • slicing
    • function patterns (if, if/else, if/not, early return, accumulator)
  • Functions
  • Decomposition
  • Doctests

You can use this guide to study and then test yourself using the practice exam and solution for the practice exam.

You will see these types of questions:

  • What does this code print?
  • Writing doctests
  • Writing code
  • Choosing the correct implementation of a function
  • Decomposition

The exam will be entirely on pen and paper. For any question where you write code, we will not run your code or expect it to run. Instead, we will look at the code, identify each part that is correct, and award partial credit. For example, if a student wrote this for moving Bit:

    bit.move_forward[]

We could still give that full credit, even though the name of the function is wrong and the function is using square brackets instead of parentheses. If we can see the right idea in there, that’s good enough for us. We are interested in the algorithm in the solution code, not the syntax.

We will provide a basic reference for the Python functions we’ve used (see the practice exam for an example), but other than that the exam is closed note. Closed note exams are actually better for the students, because we can use simple examples like what you have seen before. With an open note exam we would have to think of more difficult questions that are not too similar to what you have already done in class. Ideally, as you practice for the exam, you should feel that you can work out a good solution to any problem we did in lecture or lab.

How to study

Our goal in CS 110 is for everyone to be able to learn basic coding knowledge and skills. These basic skills are what we teach in lecture and then have you practice on labs and projects. The exams cover the exact same topics. This means the topics on the exam are predictable, so studying will pay off.

You might consider three levels of understanding a concept we cover in this course:

  1. See a code in lecture with some understanding.

  2. Write code in a lab or on a project while looking at lecture examples.

  3. Write code on a blank sheet of paper without just a basic reference.

You want to avoid getting stuck at step 2. Your brain will look at one of our examples and think “oh yeah, I see how to do that.” On the day of the exam, you may look at that first empty screen waiting for you to type something, and find your mind has gone completely blank. The key is, you need to practice with the blank sheet, writing the code out, not just looking at the solution code.

To help you get to step 3, we publish many examples of the core concepts. You can see all our lecture notes, redo the labs, and practice on extra problems on the Resources menu.We also are giving you some practice problems below, and a practice exam with a solution for it. Take your time to be sure you understand the concepts and can work on code independently. Ask questions in office hours or on Ed.

Practice problems

Try using the practice problems below. Remember, for the ones where you need to write code, see if you can get the concept right without looking at a reference. You’re not concerned about running your code, you want to be sure to have the algorithm right. Draw pictures where you think they may be helpful.

Function call

What does the caller() function print when it runs?

def one_more(a):
    b = a + 1
    return b

def caller():
    a = 1
    b = 2
    c = one_more(b)
    print(a, b, c)

Bit puzzles

See the bit puzzles reference.

Image problems

Stripes

Write a function called stripes(), shown below. It takes two parameters:

  • filename: the name of a file that contains an image
  • n: a number between 0 and the width of the image

Consider two vertical stripes, n pixels wide, running down the left and right sides of the image. Write nested loop code to set all the red values within the stripes to 0. Return the changed image.

from byuimage import Image

def stripes(filename, n):
    image = Image(filename)
    # your code here


    return image

Mirrorish

Write a function called mirrorish(), shown below. It takes one parameter:

  • filename: the name of a file that contains an image

The function creates a new image that is three times as wide as the original image. Copy the original image to the rightmost third of the new image, but with left and right reversed. Return the new image.

def mirrorish(filename):
    image = Image(filename)
    # your code here



    return new_image

Wider

Write a function called wider(), shown below. It takes two parameters:

  • filename: the name of a file that contains an image
  • n: number of pixels

Create a new image that is n pixels wider than the original. In a vertical stripe, n pixels wide, running down the left side of the output, set all the green values to 0, making it purple. Copy the original image to the area to the right of the stripe. Return the new image.

def wider(filename, n):
    image = Image(filename)
    # your code here

String problems

uppers

Given a string s, return a string made of the upper case chars from s, so ‘Hi TherE’ returns ‘HTE’.

def uppers(s):

brackets

Given a string s, return, find the first < and the first > in the string. If these chars are not present or the < is not before the >, return the empty string. Otherwise, return the chars that are between < and >. So xx<yyy>xxx returns yyy.

def brackets(s):
    # your code here

nutty

This nutty function returns a string made up of characters from s. For every character in s, if the character is a digit, compute the integer value of that lone digit. If that integer is a valid index number into s, append the character at that index to the result. So for example ‘xyz029’, returns ‘xz’ since ‘x’ is at index 0 and ‘z’ is at index 2.

def nutty(s):
    # your code here

partial flip

Given a string s of even length, if the string length is 2 or less, return it unchanged. Otherwise take off the first and last chars. Consider the remaining middle piece. Split the middle into front and back halves. Swap the order of these two halves, and return the whole thing with the first and last chars restored. So ‘x1234y’ yields ‘x3412y’.

def partial_flip(s):
    # your code here

Solutions

Function-call

The output is:

1 2 3

Image problems

def stripes(filename):
    image = Image(filename)
    for y in range(image.height):
        for x in range(n):
            # setting both left and right sides
            # with one loop. Could be solved with
            # multiple loops.
            pixel_left = image.get_pixel(x, y)
            pixel_left.red = 0
            pixel_right = image.get_pixel(image.width - 1 - x, y)
            pixel_right.red = 0
    return image


def mirrorish(filename):
    image = Image(filename)
    # your code here
    out = Image.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 - 1 - x, y)
            pixel_out.red = pixel.red
            pixel_out.green = pixel.green
            pixel_out.blue = pixel.blue
    return out


def wider(filename, n):
    image = Image(filename)
    # your code here
    out = Image.blank(image.width + n, image.height)
    # purple stripe at left
    for y in range(image.height):
        for x in range(n):
            pixel_stripe = out.get_pixel(x, y)
            pixel_stripe.green = 0

    # copy image to space n pixels over
    for y in range(image.height):
        for x in range(image.width):
            pixel = image.get_pixel(x, y)
            pixel_out = out.get_pixel(n + x, y)
            pixel_out.red = pixel.red
            pixel_out.green = pixel.green
            pixel_out.blue = pixel.blue

    return out

Strings

def uppers(s):
    result = ''
    for ch in s:
        if ch.isupper():
            result += ch
    return result


def brackets(s):
    start = s.find('<')
    end = s.find('>')
    if start == -1 or end == -1 or end < start:
        return ''
    return s[start + 1:end]


def nutty(s):
    result = ''
    for i in range(len(s)):
        # using for/i/range, but
        # for/ch/s works fine too
        if s[i].isdigit():
            val = int(s[i])
            if val < len(s):  # valid index
                result += s[val]
    return result


def partial_flip(s):
    if len(s) <= 2:
        return s
    first = s[0]
    last = s[len(s) - 1]
    mid = s[1:len(s) - 1]
    halfway = len(mid) // 2  # int div needed
    return first + mid[halfway:] + mid[:halfway] + last

Credits

Many of these problems and advice come from CS 106A at Stanford.