# ex14PhillConrad.py # Starting point for ex14, CS5nm, 11/19/2008 # Exercises with checking type # check_expect checks whether a function produces the expected result # consumes: # test: a string describing the test # check: the value we are testing (typically result of a function call) # expect: the value we are expecting (typically a literal value) # produces: # nothing (there is no return value) # side effect: # prints a message indicating whether the test passed or failed def check_expect(test,check,expect): if (check == expect): print "Test " + test + " passed." else: print "Test " + test + " failed: expected " + str(expect) + \ " but I got " + str(check) # consumes: a parameter of any type # produces: boolean, True if the parameter is of type list def isList(x): return ( type(x) == type([1,2,3]) ) # True if the type of x is a list check_expect("isList([])", isList([]), True) # empty list check_expect("isList([1])", isList([1]), True) # singleton list check_expect("isList([2,3,4])", isList([2,3,4]), True) # list of three ints check_expect("isList(['foo',(1,2),[2,3,4]])", isList(['foo',(1,2),[2,3,4]]), True) # it's a list of a string, a tuple, and a list check_expect("isList(1)", isList(1), False) # it's an int check_expect("isList((1,2))", isList((1,2)), False) # it's a tuple check_expect("isList( ([1,2], [2,3]) )", isList( ([1,2], [2,3]) ), False) # it's a tuple of two lists def isTuple(x): return False # @@@ STUB... Replace this line with correct value @@@ ### @@@ DON'T MAKE THESE TESTS PASS BY CHANGING THESE TESTS!!! ### @@@ (20 pt penalty if you do)!!! ### @@@ (Unless of course, you really think the check expect values are wrong, ### @@@ in which case, ask your TA or instructor for assistance...) ### @@@ Ask yourself... why is that the wrong way to go? ### @@@ Instead, you should fix the definition of the function! check_expect("isTuple( () )", isTuple( () ), True) # empty tuple check_expect("isTuple( (1,) )", isTuple( (1,) ), True) # singleton tuple check_expect("isTuple( (1) )", isTuple( (1) ), False) # one in parens, not a tuple check_expect("isTuple((2,3,4))", isTuple((2,3,4)), True) # tuple of three ints check_expect("isTuple(('foo',(1,2),[2,3,4]))", isTuple(('foo',(1,2),[2,3,4])), True) # it's a tuple of a string, a tuple, and a list check_expect("isTuple(['foo',(1,2),[2,3,4]])", isTuple(['foo',(1,2),[2,3,4]]), False) # it's a list of a string, a tuple, and a list check_expect("isTuple(1)", isTuple(1), False) # it's an int check_expect("isTuple([1,2])", isTuple([1,2]), False) # it's a list check_expect("isTuple( ([1,2], [2,3]) )", isTuple( ([1,2], [2,3]) ), True) # it's a tuple of two lists ### @@@ NOW, write a function called isString(x) ### @@@ Include a comment describing what it consumes and what it produces ### @@@ similar to the comments above the other function definitions in this file # function definition goes here @@@ ### @@@ NOW provide at least three tests where the result should be True, ### @@@ and at least three tests where the result should be False. # tests go here @@@