fork download
  1. import re
  2. import time
  3.  
  4. SS = str(13**100500)
  5. print(len(SS))
  6. SUB = '637036977498347229246'
  7. TEMPLATE = "63@036@7749@347@29@46"
  8.  
  9. time_start = time.monotonic()
  10.  
  11. result = SS.find(SUB)
  12. time_end = time.monotonic()
  13. print(f"result: {result}, str search worktime: {1000 * (time_end - time_start):.3f}ms")
  14.  
  15. def check_pattern(s, template):
  16. # first, split strings to lists of symbols
  17. m = [*s]
  18. t = [*template]
  19. at = [index for index, symbol in enumerate(t) if symbol == '@']
  20. for shift in range(len(m) - len(t) + 1):
  21. sub_array = m[shift: shift + len(t)]
  22. for at_index in at:
  23. sub_array[at_index] = '@'
  24. if sub_array == t:
  25. return shift
  26. return -1
  27.  
  28. def check_pattern_2(s, template):
  29. # first, split strings to lists of symbols
  30. m = [*s]
  31. t = [*template]
  32. MM = [(index, symbol) for index, symbol in enumerate(t) if symbol != '@']
  33. for shift in range(len(m) - len(t) + 1):
  34. t_pass = True
  35. for kk, symbol in MM:
  36. if m[shift + kk] != symbol:
  37. t_pass = False
  38. break
  39. if t_pass:
  40. return shift
  41.  
  42. return -1
  43.  
  44. def check_pattern_re(s, template):
  45. res = re.findall(template.replace('@', '.'), s)
  46. return s.find(res[0]) if res else -1
  47.  
  48. time_start = time.monotonic()
  49. result = check_pattern_2(SS, TEMPLATE)
  50. time_end = time.monotonic()
  51.  
  52. worktime = 1000 * (time_end - time_start)
  53. print(f"result: {result}, worktime: {worktime:.3f}ms")
  54.  
Success #stdin #stdout 0.24s 10300KB
stdin
Standard input is empty
stdout
111952
result: 110890, str search worktime: 0.076ms
result: 110890, worktime: 18.100ms