Advertisement
gruntfutuk

test count last substring occurrences

Apr 27th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. ''' beginners exercise to count and return occurrences of last 2 char
  2.    substring in preceding part of a string argument '''
  3.  
  4. def Your_Name_count2(stringtosearch):
  5.     ''' write your solution here '''
  6.     return count
  7.  
  8. ''' test harness for all of above solutions '''
  9.  
  10.  
  11. def test_funcs(*funcs):
  12.     ''' test of supplied function to count occurences of last 2 char substring in source'''
  13.     tests = [('abab', 1),
  14.              ('abcdefxy', 0),
  15.              ('abcdabcdab', 2),
  16.              ('abbcdabbbcbb', 3),
  17.              ('abbbcdabbcbb', 3),
  18.              ('', 0), ('ab', 0), ('abc', 0),
  19.              ('failedtest', 999)
  20.              ]
  21.  
  22.     for func in funcs:
  23.         print(f'\nTesting function {func.__name__}\n')
  24.         for test, assertion in tests:
  25.             count = func(test)
  26.             try:
  27.                 assert count == assertion
  28.             except AssertionError:
  29.                 print(
  30.                     f'*** ERROR **>> test data {test} returned a count of {count} instead of {assertion}')
  31.             else:
  32.                 print(
  33.                     f'{test[-2:]} found in {test} {count} times.')
  34.  
  35.  
  36. ''' testing functions '''
  37. if __name__ == '__main__':
  38.     func_list = [func for name, func in globals().items()
  39.                  if callable(func) and name[0:2] not in ('__')
  40.                     and name[0:4] not in ('exit', 'quit', 'get_', 'test')]
  41.     test_funcs(*func_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement