Practice problems: Week 3ΒΆ

The purpose of these problems is to allow you to test your understanding of the material we have covered so far. It is intended to take an hour or so, assuming you have done the reading.

To get started run git pull upstream master, which will pick up a directory named pp3.

The problems are broken up into two parts: a set of “be-a-computer” warm-up exercises and a set of programming problems. You should do the warm-up exercises by hand. You can check your answer by running the Python program warmup.py in pp3. We will be using a system named Kattis for the programming problem. Kattis is a website that allows students to submit solutions to programming problems, and have them evaluated automatically by running a series of test cases on the submitted solutions. You can find information about how to use Kattis here. You can find the skeleton code needed for the programming problems in pp3.

Both warm-up exercises and the challenge exercise use the following constants and data:

(DATE, TICKER, OPEN, CLOSE) = (0, 1, 2, 3)

stocks = [['Date', 'Ticker Symbol', 'Open', 'Close'],
          ['2010-11-09', 'AMD', '8.22', '7.91'],
          ['2010-11-09', 'GOOG', '630.00', '624.82'],
          ['2010-11-09', 'QQQ', '53.95', '54.26'],
          ['2010-11-10', 'AMD', '8.22', '8.72'],
          ['2010-11-10', 'BSB', '620.00', '630.40'],
          ['2010-11-10', 'GOOG', '630.00', '630.40'],
          ['2010-11-10', 'QQQ', '53.95', '53.45'],
          ['2010-11-11', 'AMD', '8.22', '8.40'],
          ['2010-11-11', 'GOOG', '630.00', '634.82'],
          ['2010-11-11', 'QQQ', '53.95', '53.45']]

Warm-up exercise #1: What is the output of the following code?

def f1(data):
    d = {}
    for row in data[1:]:
        ticker = row[TICKER]
        if (float(row[CLOSE]) - float(row[OPEN])) < 0:
            d[ticker] = d.get(ticker, 0) + 1
    return d

print("Warmup exercise 1:")
print(f1(stocks))
print()

Warm-up exercise #2: What is the output of the following code?

def f2(data, threshold):
    d = {}
    for row in data[1:]:
        date = row[DATE]
        ticker = row[TICKER]
        spread = float(row[CLOSE]) - float(row[OPEN])
        if threshold <= spread:
            if date not in d:
                d[date] = []
            d[date].append(ticker)
    return d

print("Warmup exercise 2:")
print(f2(stocks, 0))
print()

Programming problem #1: Babelfish

Programming problem #2: Odd man out

Here are a couple problems for those of you who like a challenge!

Challenge exercise: What is the output of the following code?

def f3(data):
    d = {}
    for row in data[1:]:
        date = row[DATE]
        ticker = row[TICKER]
        close = float(row[CLOSE])
        if date not in d:
            d[date] = [close, [ticker]]
        elif d[date][0] < close:
            d[date] = [close, [ticker]]
        elif d[date][0] == close:
            d[date][1].append(ticker)
    return d

print("Challenge exercise:")
print(f3(stocks))
print()

Challenge programming problem: Secure doors