ex15KangYooPark - ex15KangYooPark.py based on...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
# ex15KangYooPark.py 12/5/08 # based on: ex15PhillConrad.py Examples to use for ex15, CS5NM # Code for "new_check_expect", "check_exception", and # examples of various kinds of functions. import sys # new_check_expect checks whether a fruitful (non-void) # function produces the expected result # # consumes: # check: a string containing the function call we want to test # expect: the value we are expecting (typically a literal value) # msg: True if we should print output, False if we should not # default is True) # produces: # True if test passes, False otherwise # side effect: # prints a message indicating whether the test passed or failed def new_check_expect(check,expect,msg=True): if msg: print "Testing " + check + " :", try: actual = eval(check) result = (actual == expect) if msg: if result: print " Passed!" else: print " Failed! Expected: " + str(expect) + " Got: " + str(actual) return result except: if msg: print " ERROR: Unable to evaluate test: " + str(sys.exc_info()[0]) # check_exception is used when we want to test whether a function # returns the correct "exception" under certain circumstances # # consumes: # check: an expression to test, as a string # expectedException: the exception you should get in response # msg: whether or not to print results as a side effect # produces: # boolean: True is the test comes out as expected, i.e. you get the # exception that you think you are going to get # side effect: # if msg is True, then print messages about what happened to the console def check_exception(check,expectedException,msg=True): if msg: print "Testing " + check + " :", try: evalResult = eval(check) gotException = False
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
except: actualException = sys.exc_info()[1] gotException = True gotExpectedException = (str(actualException) == expectedException) if gotException and gotExpectedException: if msg: print " Passed!" return True elif not gotException: if msg: print " Failed! I expected an exception, but instead I got the result: " + \ str(evalResult) return False else: # must be true that (not gotExpectedException) if msg: print " Failed! I expected this Exception: " + \ expectedException + " but instead I got this exception: " + \ str(actualException) return False # noDups is a function that takes a list of items, and # returns a copy of that list with no consecutive duplicates # consumes: a list of items # produces: the same list with any consecutive duplicates removed # example: noDups([2,3,3,3,4,4,5,6,6,2,2,1,5,3,3,2] # produces: [2,3,4,5,6,2,1,5,3,2] def noDups(lst): # return -1 stub if (len(lst)==0): return lst # not [], because then it doesn't work on tuples or strings elif len(lst)==1: return lst else: # now we know that the list has at least two elements if lst[0:1] == lst[1:2]: return noDups(lst[1:]) # remove first element if its a duplicate else: return lst[0:1] + noDups(lst[1:]) # we could use: # return [lst[0]] + noDups(lst[1:]) # but that only works on lists, not strings or tuples # we cannot use: # lst[0] + noDups(lst[1:]) because + isn't defined for adding
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/28/2009 for the course CS 50 taught by Professor Staff during the Spring '08 term at UCSB.

Page1 / 11

ex15KangYooPark - ex15KangYooPark.py based on...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online