Solutions

Third Exam

See exam3Sol.pdf (only one version of exam, but others are similar)

Fourth Homework (due 11/11)

No solution presented for shapes.py, because there were so many different programs and most people got the basic idea pretty well, though it was unfortunate to see programs where the shapes were drawn using cut-and-paste with modifications, rather than using a function to draw and place each shape; also, the lack of comments hurt scores in some cases.

def hicode(A,wordcode):
R = []  # for accumulation
m = min(wordcode.values()) # less than or equal to max
for v in A:
m = max(wordcode[v],m)  # max of an actual word in A
for v in A:  # now accumulation will be simple
if wordcode[v]==m:
R.append(v)
return R

def findwhich(List,Value,Occur):
for i in range(len(List)):
if List[i]!=Value:
continue  # skip items not of interest
if Occur==1:
return i
Occur -= 1
return None # should not reach here

def arrave(X):
R = []
for i in range(len(X)):  # rows first
T = 0.0
for j in range(len(X[i])):  # sum of row i
T += X[i][j]    # only column changes for row i
R.append(T/float(len(X[i])))
for i in range(len(X)): # columns second
T = 0.0
for j in range(len(X)):   # sum of column i
T += X[j][i]    # only row changes for column i
R.append(T/float(len(X[i])))
return R

Third Homework (due 10/28)

def larges(R,v):
L = []
for x in R:
if x>v:
L.append(x)
return L
def doubat(A,B):
for e in B:
A[e] += A[e]
def doubodd(L):
for i in range(len(L)):
if L[i]%2==1:
L[i] *= 2
def foldsum(A):
O = []
while len(A)>0:
O.append(A + A[-1])
del A
del A[-1]
return O
def formax(T):
s = T
for v in T:
if v>s:
s = v
return s
def sumodd(R):
s = 0
for x in R:
if x%2==1:
s += x
return s

See quiz3Sol.pdf

Second Homework (due 10/8)

def afterX(s,c):
return s[s.index(c)+1:]

def column(A,i):
return [w[i] for w in A]

def places(A,k):
return [ i for i in range(len(A)) if A[i]==k ]

def prefixes(S):
return [ S[:i] for i in range(len(S)+1) ]

def evens(T):
return ''.join([T[i] for i in range(0,len(T),2)])
def alterchars(S):
return [evens(x) for x in S]

def rema(S,c):
return S[S.index(c)+1:]
def between(S,c):
return rema(S,c)[:rema(S,c).index(c)]
def between(S,c):
return S.split(c)

Extra Homework (due 9/23)

Here are function definitions that work for the extra homework problems.

def allz(x):
return 'z'*len(x)
def foo(s):
return (s+s[-1]) in s
def isplus(p,q,total):
def myisupper(s):
return s == s.upper()

For quadroot, the following defines some extra functions that help write the final answer:

def s(a,b,c):
return (b*b-4*a*c)**0.5
def root1(a,b,c):
return (-b+s(a,b,c))/(2*a)
def root2(a,b,c):
return (-b-s(a,b,c))/(2*a)