Be-A-Computer: Weeks 1-3¶
The purpose of these problems is to allow you to test you understanding of the material we have covered so far. They are divided into three sections; 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 bac1-3
.
Basic Concepts¶
You can check your answers by running the Python program basics.py
in bac1-3
Warm-up exercise #1: What is the value of w
after evaluating the
following code?
x = 7
y = 5.0
z = 10.0
w = x % 2 + y / z + z + y / (z + z)
Warm-up exercise #2: What is the value of c
after evaluating the following code?
c = True
d = False
c = c and d
c = not c or d
Warm-up exercise #3: What is the output generated by the following program?
d = 0
for p in range(0, 5):
if p % 4 == 0:
d = d + (p-1) * 25;
else:
d = d + 100;
print("$" + str(d//100) + "." + str(d % 100))
Functions and Lists¶
You can check your answers by running the Python program functions-lists.py
in bac1-3
Warm-up exercise #1: What is the output of the following code?
def F1(i, j) :
print("F1({}, {})".format(i, j))
F2(j, i+j)
print("F1: i = {}, j = {}".format(i, j))
def F2(i, j) :
print(" F2({}, {})".format(i, j))
k = F3(i, j)
print(" F2: i = {}, j = {}, k = {}".format(i, j, k))
def F3(i, j) :
print(" F3({}, {})".format(i, j))
i = i+j
j = i+2*j
k = 2*i+3*j
print(" F3: i = {}, j = {}, k = {}".format(i, j, k))
return k
print("Warmup exercise 1:")
F1(1, 1)
print()
Warm-up exercise #2: What is the output of the following code?
def mystery1(l):
rv = []
for x in l:
rv = [x] + rv
return rv
print("Warmup exercise 2:")
l = [0, 1, 2, 3, 4]
nl = mystery1(l)
print("l: ", l)
print("nl: ", nl)
Warm-up exercise #3: What is the output of the following code?
def mystery2(l):
rv = []
for i in range(len(l)-1, -1, -1):
rv.append(l[i])
return rv
print("Warmup exercise 3:")
l = [0, 1, 2, 3, 4]
nl = mystery2(l)
print("l: ", l)
print("nl: ", nl)
Challenge exercise: What is the output of the following code?
def mystery3(l):
n = len(l)
for i in range(n // 2):
t = l[i]
l[i] = l[n-i-1]
l[n-i-1] = t
print("Challenge exercise:")
l = [0, 1, 2, 3, 4]
mystery3(l)
print("l: ", l)
Dictionaries¶
You can check your answers by running the Python program dictionaries.py
in bac1-3
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()
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()