Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' beginners exercise to count and return occurrences of last 2 char
- substring in preceding part of a string argument '''
- def Your_Name_count2(stringtosearch):
- ''' write your solution here '''
- return count
- ''' test harness for all of above solutions '''
- def test_funcs(*funcs):
- ''' test of supplied function to count occurences of last 2 char substring in source'''
- tests = [('abab', 1),
- ('abcdefxy', 0),
- ('abcdabcdab', 2),
- ('abbcdabbbcbb', 3),
- ('abbbcdabbcbb', 3),
- ('', 0), ('ab', 0), ('abc', 0),
- ('failedtest', 999)
- ]
- for func in funcs:
- print(f'\nTesting function {func.__name__}\n')
- for test, assertion in tests:
- count = func(test)
- try:
- assert count == assertion
- except AssertionError:
- print(
- f'*** ERROR **>> test data {test} returned a count of {count} instead of {assertion}')
- else:
- print(
- f'{test[-2:]} found in {test} {count} times.')
- ''' testing functions '''
- if __name__ == '__main__':
- func_list = [func for name, func in globals().items()
- if callable(func) and name[0:2] not in ('__')
- and name[0:4] not in ('exit', 'quit', 'get_', 'test')]
- test_funcs(*func_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement