CSci 150: Foundations of computer science
Home Syllabus Readings Projects Tests

Bonus Quiz: Questions

BQ.1.

[8 pts] Suppose the file pops.txt contains a series of lines, each containing three tab-separated values: a city name, a state name, the city's current population, and the city's population three years ago. Here are five example lines from the file.

Chicago        Illinois        9899902      9840929
Los Angeles    California      18238998     17877006
New York       New York        23362099     23076664
San Francisco  California      8370967      8153696
Washington     DC              9331587      9051961

Complete the below program to display the name of each city with a current population exceeding 5,000,000. (The order in which the names appear does not matter.)

infile = open('pops.txt')
for line in infile:
BQ.2.

[12 pts] Without using loops or any built-in functions or methods except len, write a recursive function named multiply_list that takes two parameters, a number and a list of numbers, and returns a list of everything in the parameter list multiplied by the integer.

For example, if nums is [2, 3, 5, 7, 11], then multiply_list(7nums) should return the list [14, 21, 35, 49, 77].

Bonus Quiz: Solutions

BQ.1.
infile = open('pops.txt')
for line in infile:
    cols = line.rstrip().split('\t')
    if int(cols[2]) >= 5000000:
        print(cols[0])
BQ.2.
def multiply_list(multnums):
    if len(nums) == 1:
        return [mult * nums[0]]
    else:
        first = mult * nums[0]
        rest = multiply_list(multnums[1:])
        return [first] + rest