fork download
  1. import time
  2.  
  3.  
  4. def check_pattern(s, template):
  5. # first, split strings to lists of symbols
  6. m = [*s]
  7. t = [*template]
  8. at = [index for index, symbol in enumerate(t) if symbol == '@']
  9. for shift in range(len(m) - len(t) + 1):
  10. sub_array = m[shift: shift + len(t)]
  11. for at_index in at:
  12. sub_array[at_index] = '@'
  13. if sub_array == t:
  14. return shift
  15. return -1
  16.  
  17. time_start = time.monotonic()
  18. result = check_pattern("obosralsya v proshlom primere", "sr@l")
  19. time_end = time.monotonic()
  20.  
  21. worktime = 1_000_000 * (time_end - time_start)
  22. print(f"result: {result}, worktime: {worktime:.1f}us")
  23.  
Success #stdin #stdout 0.02s 9264KB
stdin
Standard input is empty
stdout
result: 3, worktime: 8.7us