Practice problems: Week 4ΒΆ

This set of practice problems will allow you to test your understanding of some the material we have covered so far this quarter. While these problems will be good practice for exam #1, we do not promise that they cover the full range of topics that might appear on the exam.

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

As usual, the problems are broken up into two parts: a set of “be-a-computer” warm-up exercises and a set of programming problems. For the programming problems, we provide the code that handles the input/output of the problem. Make sure you read the comments in the provided code so you understand exactly what part of the code you need to modify.

Warm-up exercise #1: This warm-up exercise uses 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']]

What is the output of the following code?

class Stock_Info(object):
    def __init__(self, date, ticker, opening_price, closing_price):
        self._date = date
        self._ticker = ticker
        self._opening_price = opening_price
        self._closing_price = closing_price

    @property
    def date(self):
        return self._date

    @property
    def ticker(self):
        return self._ticker

    @property
    def opening_price(self):
        return self._opening_price

    @property
    def closing_price(self):
        return self._closing_price

    @closing_price.setter
    def closing_price(self, correct_value):
        self._closing_price = correct_value

    def __repr__(self):
        return "|".join([self.date, self.ticker, 
                         self.opening_price, self.closing_price])



def create_dict(data):
    rv = {}
    for row in data:
        tkr = row[TICKER]
        s = Stock_Info(row[DATE], tkr, row[OPEN], row[CLOSE])
        if tkr not in rv:
            rv[tkr] = []
        rv[tkr].append(s)

    return rv
        

def mystery(d, list0):
    for (x, y, z) in list0:
        for a in d[x]:
            if a.date == y:
                a.closing_price = z
                break


d = create_dict(stocks)
entries = [["GOOG", '2010-11-09', '625.00'], 
           ['GOOG', '2010-11-10', '634.82']]
mystery(d, entries)
for s in d["GOOG"]:
    print(s)

Programming problem #1: Pet

Programming problem #2: Planting Trees

Programming problem #3: Engineering English

Programming problem #4: Flow Layout

Programming problem #5: Quick Brown Fox

Programming problem #6: Mine Sweeper

Programming problem #7: Verify This, Your Majesty