# THIS CODE IS SUPPOSED TO: # Function get_occurrence takes a string and a substring # and returns the number of times the substring appears in the string # without using any built-in functions/methods besides len() # The substring will be at most three characters long. # (i.e. NOT use .index()). # # Write test cases to find an unknown bug in the function. # # Do not look at the code. # You only need a specification to create test cases. # import re def get_occurrence(string, substring): count = 0 for i in range( 1 , len(string) ): if string[i : i + len( substring )] == substring : count += 1 return count actualResults = [] expectedResults = [] #WRITE YOUR TESTS BELOW FOLLOWING THE FORMAT actualResults.append(get_occurrence("introduction to programming", "d")) expectedResults.append(1) actualResults.append(get_occurrence("introduction to programming", "o")) expectedResults.append(4) actualResults.append(get_occurrence("introduction to programming", "g")) expectedResults.append(1) actualResults.append(get_occurrence("introduction to programming", "ro")) expectedResults.append(2) actualResults.append(get_occurrence("introduction to programming", "in")) # find bug expectedResults.append(2) actualResults.append(get_occurrence("introduction to programming", "i")) # find bug expectedResults.append(3) #===================================# ctr = 0 found = False while ctr < len(actualResults): actual = actualResults[ctr] expected = expectedResults[ctr] print(str(expected) + " : " + str(actual)) if (actual != expected): found = True ctr = ctr + 1 if (found == True): print("Your test suite found the bug!!") else: print("Try writing more tests, you didn't find the bug yet.") ''' correct code: for i in range( 0 , len(string) ): '''