Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import random
  3. import string
  4.  
  5. CMP_COUNT = 0
  6.  
  7. def matches_at(t: str, pos: int, w: str) -> bool:
  8.     # 0 <= pos <= len(t) - len(w)
  9.     global CMP_COUNT
  10.     for i in range(len(w)):
  11.         CMP_COUNT += 1
  12.         if t[pos+i] != w[i]:
  13.             return False
  14.     return True
  15.     # return all(t[pos+i]==w[i] for i in range(len(w)))
  16.  
  17. def naive_search(t: str, w: str) -> list:
  18.     result = []
  19.     for pos in range(len(t) - len(w) + 1):
  20.         if matches_at(t, pos, w):
  21.             result.append(pos)
  22.     return result
  23.     # return [pos for pos in range(len(t) + len(w) + 1) if matches_at(t, pos, w)]
  24.  
  25. def sunday_search(t: str, w: str) -> list:
  26.     result = []
  27.     p = 0
  28.     lastp = create_pattern_cache(w)
  29.     while p <= (len(t) - len(w)):
  30.         if matches_at(t, p, w):
  31.             result.append(p)
  32.         p += len(w)
  33.         if p < len(t): p -= lastp.get(t[p], -1)
  34.     return result
  35.    
  36. def create_pattern_cache(pattern):
  37.     return { sign: index for index, sign in enumerate(pattern) }
  38.    
  39. def benchmark(alg, t, w):
  40.     global CMP_COUNT
  41.     CMP_COUNT = 0
  42.     alg(t, w)
  43.     return CMP_COUNT
  44.  
  45. def rand_str(str_size, alphabet_size):
  46.     alphabet = string.ascii_letters[:alphabet_size]
  47.     result = ''
  48.     for _ in range(str_size):
  49.         result += random.choice(alphabet)
  50.     return result
  51.  
  52. def create_text_len_simulation(plt, t_max_len, p_len, a_len, methods):
  53.     w = rand_str(p_len, a_len)
  54.     for method in methods:
  55.         result = []
  56.         for t_len in range(t_max_len):
  57.             t = rand_str(t_len, a_len)
  58.             result.append(benchmark(method[0], t, w))
  59.            
  60.         plt.plot(range(t_max_len), result, label=method[1])
  61.        
  62.     plt.set_xlabel('Text length')
  63.     plt.set_ylabel('Number of count operation')
  64.     plt.legend()
  65.  
  66. def create_pattern_len_simulation(plt, t_len, p_max_len, a_len, methods):  
  67.     t = rand_str(t_len, a_len)
  68.     for method in methods:
  69.         result = []
  70.         for p_len in range(1, p_max_len):
  71.             w = rand_str(p_len, a_len)
  72.             result.append(benchmark(method[0], t, w))
  73.            
  74.         plt.plot(range(1, p_max_len), result, label=method[1])
  75.        
  76.     plt.set_xlabel('Pattern length')
  77.     plt.set_ylabel('Number of count operation')
  78.     plt.legend()
  79.  
  80. def create_alphabet_len_simulation(plt, t_len, p_len, a_max_len, methods):  
  81.     for method in methods:
  82.         result = []
  83.         for a_len in range(1, a_max_len):
  84.             t = rand_str(t_len, a_len)
  85.             w = rand_str(p_len, a_len)
  86.             result.append(benchmark(method[0], t, w))
  87.            
  88.         plt.plot(range(1, a_max_len), result, label=method[1])
  89.        
  90.     plt.set_xlabel('Pattern length')
  91.     plt.set_ylabel('Number of count operation')
  92.     plt.legend()
  93.  
  94.  
  95. if __name__ == "__main__":
  96.     methods = [(naive_search, 'Naive'), (sunday_search, 'Sunday')]    
  97.     _, axs = plt.subplots(3, 1)
  98.     text_len_plt, pattern_len_plt, alphabet_len_plt = axs.flat
  99.      
  100.     create_text_len_simulation(text_len_plt, 1000, 6, 4, methods)
  101.     create_pattern_len_simulation(pattern_len_plt, 1000, 1000, 4, methods)
  102.     create_alphabet_len_simulation(alphabet_len_plt, 100, 6, 200, methods)
  103.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement