Be-A-Computer: Weeks 5-8

The purpose of these problems is to allow you to test your understanding of Classes and Recursion. Each section should not take more than an hour to complete.

You should do these warm-up exercises by hand. We provide files that will allow you to check your answers.

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

Classes and Objects

You can check your answers by running the Python program classes.py in bac5-8

Warm-up exercise: 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)

Recursion

You can check your answers by running the Python program recursion.py in bac5-8

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

def mystery(a, b, c, d):
  if c == 0:
    return d
  elif c % 2 == 0:
    return b + mystery(a, b, c - 2, d)
  else:
    return a + mystery(a, b, c - 1, d)

print("Warmup Exercise #1")
print(mystery("x", "y", 6, "z"))
print(mystery("x", "y", 5, "z"))
print()

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

def flip(s, a):
  if a == 0:
    return s
  for i in range(a):
    if s[i] == "*":
      s[i] = "-"
    else:
      s[i] = "*"
  return flip(s, a//2)

print("Warmup Exercise #2")
s = []
for i in range(64):
  s.append("*")
s = flip(s, 64)
t = ""
for c in s:
  t = t + c
print(t)