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.

   1 def hicode(A,wordcode):
   2   R = []  # for accumulation
   3   m = min(wordcode.values()) # less than or equal to max 
   4   for v in A:
   5      m = max(wordcode[v],m)  # max of an actual word in A
   6   for v in A:  # now accumulation will be simple
   7      if wordcode[v]==m:
   8         R.append(v)
   9   return R
  10 
  11 def findwhich(List,Value,Occur):
  12   for i in range(len(List)):
  13      if List[i]!=Value:
  14         continue  # skip items not of interest
  15      if Occur==1:
  16         return i        
  17      Occur -= 1
  18   return None # should not reach here
  19 
  20 def arrave(X):
  21   R = []
  22   for i in range(len(X)):  # rows first
  23     T = 0.0
  24     for j in range(len(X[i])):  # sum of row i
  25         T += X[i][j]    # only column changes for row i
  26     R.append(T/float(len(X[i])))
  27   for i in range(len(X[0])): # columns second
  28     T = 0.0
  29     for j in range(len(X)):   # sum of column i
  30         T += X[j][i]    # only row changes for column i
  31     R.append(T/float(len(X[i])))
  32   return R

Third Homework (due 10/28)

   1 def larges(R,v):
   2    L = []
   3    for x in R:
   4      if x>v:
   5        L.append(x)
   6    return L
   7 def doubat(A,B):
   8    for e in B:
   9      A[e] += A[e]
  10 def doubodd(L):
  11    for i in range(len(L)):
  12      if L[i]%2==1:
  13         L[i] *= 2
  14 def foldsum(A):
  15    O = []
  16    while len(A)>0:
  17      O.append(A[0] + A[-1])
  18      del A[0]
  19      del A[-1]
  20    return O
  21 def formax(T):
  22    s = T[0]  
  23    for v in T:
  24      if v>s:
  25        s = v
  26    return s
  27 def sumodd(R):
  28    s = 0  
  29    for x in R:
  30      if x%2==1:
  31        s += x 
  32    return s

Third Quiz (10/20)

See quiz3Sol.pdf

Second Exam (midterm)

See exam2Sols.pdf

Second Homework (due 10/8)

   1 def afterX(s,c):
   2    return s[s.index(c)+1:]
   3 
   4 def column(A,i):
   5   return [w[i] for w in A]
   6 
   7 def places(A,k):
   8   return [ i for i in range(len(A)) if A[i]==k ]
   9 
  10 def prefixes(S):
  11   return [ S[:i] for i in range(len(S)+1) ]
  12  
  13 def evens(T):
  14   return ''.join([T[i] for i in range(0,len(T),2)]) 
  15 def alterchars(S):
  16   return [evens(x) for x in S]
  17 
  18 def rema(S,c):
  19   return S[S.index(c)+1:]
  20 def between(S,c): 
  21   return rema(S,c)[:rema(S,c).index(c)]
  22 def between(S,c):
  23   return S.split(c)[1]

Quiz Two (given 10/4)

See quiz2Sols.pdf

Quiz One (given 9/24)

See quiz1Sols.pdf

Extra Homework (due 9/23)

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

   1 def allz(x):
   2   return 'z'*len(x)
   3 def foo(s): 
   4   return (s[0]+s[-1]) in s
   5 def isplus(p,q,total): 
   6   return total == p+q 
   7 def myisupper(s): 
   8   return s == s.upper()

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

   1 def s(a,b,c):
   2   return (b*b-4*a*c)**0.5
   3 def root1(a,b,c):
   4   return (-b+s(a,b,c))/(2*a)
   5 def root2(a,b,c):
   6   return (-b-s(a,b,c))/(2*a)
   7 def quadroot(a,b,c):
   8   return root1(a,b,c), root2(a,b,c)

First Exam

See Exam1Sols.pdf

Solutions (last edited 2014-05-25 18:33:05 by localhost)