fork download
  1. import time
  2. import re
  3.  
  4. from memory_profiler import profile
  5.  
  6.  
  7. stepan_says = "aahahaaaaaaaaaaajkfldhahadofofahahlsaaadklsdhahaaa" * 2000
  8.  
  9.  
  10. #with open("stepan_says.txt", "w", encoding="utf-8") as f:
  11. #f.write(stepan_says)
  12.  
  13. # @profile
  14. def loop(inp):
  15. m, cm = 0, 0
  16.  
  17. pch = ''
  18.  
  19. for i, ch in enumerate(inp):
  20. if i == 0:
  21. pch = ch
  22. if ch in ('a', 'h'):
  23. cm += 1
  24. continue
  25.  
  26. if i == 1:
  27. if (pch + ch) in ('aa', 'hh'):
  28. cm = 0
  29. pch = ch
  30. continue
  31.  
  32. if (pch + ch) in ('ah', 'ha'):
  33. cm += 1
  34. pch = ch
  35. else:
  36. if cm > m:
  37. m = cm
  38. cm = 0
  39.  
  40. if cm > m:
  41. m = cm
  42.  
  43. return m
  44.  
  45. @profile
  46. def regex(inp):
  47. return len(
  48. sorted(
  49. filter(
  50. lambda x: abs(x.count("a") - x.count("h")) <= 1,
  51. re.findall('(?!aa|hh)([ah]{1,})(?<!aa|hh)', stepan_says)
  52. ), key=lambda x: len(x)
  53. )[-1]
  54. )
  55.  
  56.  
  57. start = time.monotonic()
  58.  
  59. regex(stepan_says)
  60.  
  61. finish = time.monotonic()
  62. # print(f'Loop: {finish-start:.5f}')
  63. # print(f'Regex: {finish-start:.5f}')
  64.  
Runtime error #stdin #stdout #stderr 0.11s 23768KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 4, in <module>
ModuleNotFoundError: No module named 'memory_profiler'