Advertisement
gruntfutuk

Count last substring occurences

Mar 31st, 2018
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 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.  
  5. def Stuart_Moore_count2(source):
  6.     count = 0
  7.     if source[3:]:
  8.         search = source[-2:]
  9.         prev = source[0]
  10.         for ch in source[1:-2]:
  11.             if prev + ch == search:
  12.                 count += 1
  13.             prev = ch
  14.     return count
  15.  
  16.  
  17. def Mark_Geyzer_count2(target):
  18.     count = 0
  19.     tail = target[-2:]
  20.     while target != tail:
  21.         count += target[:2] == tail
  22.         target = target[1:]
  23.     return count
  24.  
  25.  
  26. def Srinivasa_Reddy_Varimadugu_count2(target):
  27.     count = 0
  28.     flag = False
  29.     osubstr = target[-2::]
  30.     tail = target[-2::-1]
  31.     otail = tail[::-1]
  32.     for i in otail:
  33.         if i == osubstr[1] and flag:
  34.             count += 1
  35.             flag = False
  36.         flag = osubstr[0] == i
  37.     return count
  38.  
  39.  
  40. def Kevin_Stout_count2(xs):
  41.  
  42.     def bigrams(xs):
  43.         a, b = None, None
  44.  
  45.         for x in xs:
  46.             a, b = b, x
  47.             if a:
  48.                 yield a, b
  49.  
  50.     freq = {}
  51.     xy = None
  52.  
  53.     for xy in bigrams(xs):
  54.         freq[xy] = freq.get(xy, 0) + 1
  55.  
  56.     return freq.get(xy, 1) - 1
  57.  
  58.  
  59. def Zarren_Donovan_Spry_count2(pattern):
  60.     #lenString = stringLen(string)
  61.  
  62.     def stringLen(theString):
  63.         result = 0
  64.         for i in theString:
  65.             result = result + 1
  66.         return result
  67.  
  68.     lenString = 2
  69.     lenPattern = stringLen(pattern)
  70.     i = 0
  71.     matchCount = 0
  72.     string = pattern[-lenString:]
  73.     while i < lenPattern:
  74.         searchPattern = pattern[i:i + lenString]
  75.         if stringLen(searchPattern) == lenString:
  76.             if searchPattern == string and i < (lenPattern - lenString):
  77.                 matchCount = matchCount + 1
  78.         i = i + 1
  79.     if matchCount == 1:
  80.         if lenPattern <= lenString + 1:
  81.             return 0
  82.         else:
  83.             return matchCount
  84.     else:
  85.         return matchCount
  86.  
  87.  
  88. def Pierre_Chiggles_count2(Haystack):
  89.     count = 0
  90.     while True:
  91.         try:
  92.             needle = Haystack[-2] + Haystack[-1]
  93.             pos = -3
  94.             while True:
  95.                 try:
  96.                     needleTest = Haystack[pos] + Haystack[pos + 1]
  97.                     if needle == needleTest:
  98.                         count += 1
  99.                     pos -= 1
  100.                 except:
  101.                     return count
  102.         except:
  103.             return count
  104.  
  105. ''' test harness for all of above solutions '''
  106.  
  107.  
  108. def test_funcs(*funcs):
  109.     ''' test of supplied function to count occurences of last 2 char substring in source'''
  110.     tests = [('abab', 1),
  111.              ('abcdefxy', 0),
  112.              ('abcdabcdab', 2),
  113.              ('abbcdabbbcbb', 3),
  114.              ('abbbcdabbcbb', 3),
  115.              ('', 0), ('ab', 0), ('abc', 0),
  116.              ('failedtest', 999)
  117.              ]
  118.  
  119.     for func in funcs:
  120.         print(f'\nTesting function {func.__name__}\n')
  121.         for test, assertion in tests:
  122.             count = func(test)
  123.             try:
  124.                 assert count == assertion
  125.             except AssertionError:
  126.                 print(
  127.                     f'*** ERROR **>> test data {test} returned a count of {count} instead of {assertion}')
  128.             else:
  129.                 print(
  130.                     f'{test[-2:]} found in {test} {count} times.')
  131.  
  132.  
  133. ''' testing functions '''
  134. if __name__ == '__main__':
  135.     func_list = [func for name, func in globals().items()
  136.                  if callable(func) and name[0:2] not in ('__')
  137.                     and name[0:4] not in ('exit', 'quit', 'get_', 'test')]
  138.     test_funcs(*func_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement