#CS 0008: Spring 2015 Lab 3 #YOUR NAME: # #YOUR PARTNER'S NAME: # #To get credit on this lab, attend recitation, work with your partner to answer the questions, and give "reasonable" (if not all correct) answers. Each of you should hand in a hardcopy of your own solution, due at the beginning of next week's recitation. #For this lab, you will write 4 string functions. The last two do the #same thing. You should write two different versions of the same function. # #Recall in stringFuns.py, a program covered in class on 1/27, function "grade" was included, and the functions are tested by calling them like this: # grade(numVowels('hello there fellow'), 6) #"grade" is included below, along with a function "main" with test # cases already defined for you. In the future, we'll ask you to define your own test functions. def count(c,s): '''c is a character and s is a string. Return the number of times (an int) that c appears in s. Eg: count('a','Anna') returns 1''' def replace(old,new,s): '''old and new are characters and s is a string. Return a copy of s with all occurrences of old replaced by new. Eg: replace('i','o','Million') returns 'Molloon' ''' # Hint for the following functions: a return inside the loop and/or a boolean # variable can be helpful. If you can only get one version, # that's ok - you will pass the lab if you only come up with one. # def contains1(c,s): '''c is a char and s is a str. Return True if s contains c and false otherwise. ''' def contains2(c,s): '''c is a char and s is a str. Return True if s contains c and false otherwise. ''' def grade(myAns,correctAnswer): if myAns == correctAnswer: print("Correct, the answer is",myAns) else: print("My answer is",myAns,"but the correct answer is",correctAnswer) def main(): grade(count('n','Anna'),2) grade(count('n','computer'),0) grade(count('a','a'),1) grade(count('a',''),0) grade(count('a','baaaaaaa'),7) grade(replace('i','o','Million'),'Molloon') grade(replace('z','o','Million'),'Million') grade(replace('i','o','i'),'o') grade(replace('i','o',''),'') grade(contains1('c','hello'),False) grade(contains1('e','hello'),True) grade(contains1('h','hello'),True) grade(contains1('o','hello'),True) grade(contains2('c','hello'),False) grade(contains2('e','hello'),True) grade(contains2('h','hello'),True) grade(contains2('o','hello'),True) main()