# more1_sol.py # cs 1110 profs, Spring 2016 # March 2016 # solutions to the "more problems" # Read this file; you can run it to verify that the code is "correct" ### print 'Running Q1a, should get no warning messages' def Q1v1(s): """ Returns True if the characters at the start and end of s are the same and occur nowhere else in s PreCondition: s is a string with length equal to 3 or greater.""" n = len(s) t = s[1:n-1] return s[0]==s[n-1] and t.count(s[0])==0 # alternate answer def Q1v2(s): """ Same as Q1v1""" first = s[0] last = s[len(s)-1] return s.count(first)==2 and first == last # alternate, very compact answer def Q1v3(s): """ Same as Q1v1""" return (s.count(s[0])==2 and s[0] == s[len(s)-1]) for answer in [['abc', False], ['aba', True], ['aaa', False]]: for fn in [Q1v1, Q1v2, Q1v3]: response = fn(answer[0]) right = answer[1] if response != right: print '\tWarning:', fn, 'wrong on', answer[0], answer[1] ## print 'Running Q1b' def Q1b(s): t = s.replace(s[0],'x') # new string: every occurrence of 1st letter to 'x' u = t.replace('x',s[0]) # newer string: every 'x' turned into s[0] return u # so, if first letter is "x", o.w., no "x"'s in s for s in ['xbc', 'xxbc', 'abac', 'axbc']: u = Q1b(s) #print '\t\t', 's and u are', s, u, 'so are they the same?', s==u be = s[0] == 'x' or s.count('x') == 0 #print '\t\t Value of "s[0] == \'x\' or s.count(\'x\') == 0" is', be print '\t"s[0] == \'x\' or s.count(\'x\') == 0" is right?', be == (s==u), '\n' ### #Q2a: (abs(x)==n and y==0) or (abs(y)==n and x==0) ## print 'Running Q2b, should get no warnings' # something that will NOT work, because "count" counts NON-OVERLAPPING strings! def nPredictablewrong(s): """ Returns an int that is the number of predictable hops in s. Precondition: s is a travel string. """ ### note that this doesn't work! return s.count('NN') + s.count('SS') + s.count('WW') + s.count('EE') #loop solution def nPredictable1(s): """Same spec as above.""" count = 0 for k in range(1,len(s)): #k = position to check if previous is the same if s[k] == s[k-1]: count = count + 1 return count def nPredictable2(s): """Same spec as above.""" count = 0 for k in range(len(s)-1): #k = position to check if next is the same if s[k] == s[k+1]: count += 1 return count for answer in [['EWNNNWWN', 3], ['EWNNWWN', 2], ['NENWNWNWN', 0]]: for fn in [nPredictable1,nPredictable2]: response = fn(answer[0]) right = answer[1] if response != right: print '\tWarning:', fn, 'wrong on', answer[0], answer[1] ### print 'running Q3a, should get no warnings' #Q3a: any even number whose remainder when divided by 3 is 1 will work for x in [10,4]: if not (x%2==0 and x%3==1): print '\tWarning: wrong about Q3a' ## print 'running Q3b, should get no warnings' def q3b(x,y): """Thinking about these constraints geometrically can help.""" if not ((0<=x<=3) and (0<=y<=3)): return 'A' elif y<=1 or y>=2: return 'B' elif x<=1 or x>=2: return 'C' else: return 'D' x = 1.5; y = 1.5 if not (q3b(x,y) == 'D'): print '\tQ3b Warning: wrong about this x and y:', x, y ## print 'running Q3c, should get 2.0' x = float(10/4) print '\t', x print '\n' ## # Q3d: you could import more than you bargained for ## print 'Running Q3e' def F(x,y): x = y y = x z = x+2*y print x,y,z return z x = 1 y = 2 print x,y x = F(y,x) print x,y if x prints 1 2 # Call to F # For F, x gets AS's y=2; For F, y gets AS's x=1 # So, for F, x: 2, y: 1 # For F: x gets F's y, so # x: 1 y:2 # For F: y gets F's x, so # x: 1 y:1 # For F: z gets F's x + 2 times F's y # so: x: 1 y:1 z= 3 # -------------> prints 1 1 3 # F returns 3 # global x gets returned value # x: 3 y:2 # --------> prints 3 2 # ----------> prints 'B' ### # Q4a print 'Running Q4a, should see no warnings' def q4a(s): """version allowing testing""" t = 'x' for c in s: t = t+c+t return t s='ba' soln='xbxaxbx' if q4a(s) != soln: print '\tQ4a: Warning: wrong answer' # showing our work # t: 'x', s: 'ba' # c: 'b' makes t: 'xbx' # c: 'a' makes t: 'xbxaxbx' ## def q4b(s): """supposed to be equivalent to above.""" t = 'x' i = 0 # index in s to consider while (i < len(s)): c = s[i] t = t + c + t i+= 1 return t for s in ['ba', 'aba', 'bbba']: if q4a(s) != q4b(s): print '\tWarning: q4b while not the same as q4a on', s ### print 'Running Q6, should see two windows, both with pyramids, one with cyan background' import SimpleGraphics def DrawRow(x0,y0,s,n): """ Draws a horizontal row of n squares that are each s-by-s. The center of the leftmost square is (x0,y0). PreC: x0, y0, and s are floats, n is a positive integer""" x = x0 for i in range(n): # need to draw square i SimpleGraphics.DrawRect(x, y0, s, s) x+=s # for loop solution def DrawPyramid(x0,y0,s,n): """ Draws a pyramid of s-by-s squares. The bottom row consists of n squares and the lower left corner of the leftmost square is at (x0,y0). There are n rows of squares and each row has one less square than the row beneath it. Precondition: x0, y0, and s are floats, n is a positive integer""" xc = x0+s/2 yc = y0+s/2 for k in range(n): DrawRow(xc,yc,s,n-k) yc = yc+s xc = xc+s/2 #while-loop solution def DrawPyramid2(x0,y0,s,n): """Same spec as above.""" i = 0 # next row to draw is row i; start at row 0 ni = n # number of squares in next row xc = x0 + s/2.0 # x-coord of center of 1st square of row to draw yc = y0 + s/2.0 # y-coord of center of 1st square of row to draw while i < n: # still a row to draw DrawRow(xc, yc, s, ni) #set up for next row xc = xc + s/2.0 yc = yc + s i +=1 ni -= 1 SimpleGraphics.MakeWindow(6) DrawPyramid(-5.,-5.,2, 5) # for-loop solution SimpleGraphics.MakeWindow(6,bgcolor=SimpleGraphics.CYAN) DrawPyramid2(-5., -5., 2, 5) # while-loop solution SimpleGraphics.ShowWindow() print 'all done, hurrah!!'