CS 111
Summer 1 2017

Quiz 3 Info

Quiz 3 Information

Quiz details

Material covered

The quiz will focus on the material that we have discussed in class from the beginning of the summer session through Tuesday, June 13th. This corresponds to the following readings:

Note: You are not responsible for details from the case studies in the readings (e.g., the information about how sorting works). However, you are responsible for the programming and problem-solving concepts that those case studies were used to illustrate.

In addition, you should be familiar with concepts that were introduced in the pre-class videos, in-class discussion, or on the homeworks that were not covered in the readings.

Preparing for the quiz

Additional practice problems

  1. Write a Python function [years_needed][years_needed] that takes three inputs:

    • principal, which is the initial amount of money deposited in an interest-bearing account
    • rate, which is the annual interest rate in decimal form
    • target, which is the final value that the investor wants to reach

    The function should use a loop to determine the number of years of compounded annual interest that are needed for the investment to reach or exceed the specified target. Note: After each year, the new principal is computed as

    principal = principal * (1 + rate)
  2. Write a Python function [find(ch, s)][find] that returns the index of the first occurrence of a character ch in a string s, or −1 if the character is not found. You may assume that ch is a single character. Use a loop.

  3. Write a Python function [count_vowels(s)][count_vowels] that counts and returns the number of vowels in a string. Use a loop.

  4. Write a Python function stars(n) where n is a positive integer. It should print n lines of stars with 1 star on first line, 2 stars on second line, and so forth. For example, stars(4) should print


    Use nested loops. You are not allowed to use the * operator (*). You should use print('*', end= '') to print a single asterisk at a time while remaining on the same line, and an empty print() to go down to the next line.

  5. Write a function [all_perfect(lst)][all_perfect] that takes a list of numbers lst and returns True if all of the numbers are 100 and False otherwise. Use a loop.

  6. What is printed by the following Python program?

    def loopy(x, y):
        print('starting loopy:', x, y)
        while x < y:
            x += 1
            y -= 2
        print('after loop:', x, y)
        return x
    x = 1
    y = 8
    y = loopy(x, y)
    print('after first call:', x, y)
    loopy(y, x)
    print('after second call:', x, y)

    Hint: Use two different tables – one for the global scope and one for loopy – to keep track of the values of the variables.

  7. Draw one or more memory diagram (the text uses the term “box-and-arrow” diagram) to illustrate the execution of the following Python program:

    a = [1, 2, 3, 4]
    b = a
    a[3] = 5
    b[1] = 7
    print('a is', a)
    print('b is', b)

    In addition, write a few sentences that refer to your diagram(s) and that explain the result of the program.

  8. Draw memory diagrams that demonstrate why we get different results from the following two Python programs:

    ### Program 1 ###
    def foo(a):
        a = 2 * a
    b = [1, 2, 3]
    for i in range(len(b)):
    print('b is', b)
    ### Program 2 ###
    def bar(lst, i):
        lst[i] = 2 * lst[i]
    b = [1, 2, 3]
    for i in range(len(b)):
        bar(b, i)
    print('b is', b)

    In addition, write a few sentences that refer to your diagrams and that explain the difference in output.

  9. Write a function create_2d that takes as input two integers height and width, and that creates and returns a 2D list (i.e., a list of lists) with values that are the row number multiplied by the column number. For example:

    >>> create_2d(3, 5)
    [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
  10. Write a function add_one that takes an input grid that is a 2D list (a list of lists). Your function should add 1 to each element of grid, but it should not return anything. For example:

    >>> my_grid = create_2d(3, 5)
    >>> add_one(my_grid)
    >>> my_grid
    [[1, 1, 1, 1, 1], [1, 2, 3, 4, 5], [1, 3, 5, 7, 9]]