Posted to tcl by mjanssen at Fri Feb 26 07:32:25 GMT 2021view raw

  1. ## Tcl
  2.  
  3. #%timeit
  4. proc encode {m c} {return "[string length $m]$c"}
  5.  
  6. proc say n {
  7. regsub -all -command {(.)\1*} $n encode
  8. }
  9. set s 1321131112
  10. for {set i 0} {$i < 16} {incr i} {
  11. set s [say $s]
  12. }
  13. puts $s
  14.  
  15. 860
  16. 36676996 microseconds per iteration
  17.  
  18.  
  19.  
  20. # Python
  21.  
  22. %time
  23. import re
  24. re_d = re.compile(r'((\d)\2*)')
  25.  
  26. def replace(match_obj):
  27. s = match_obj.group(1)
  28. return str(len(s)) + s[0]
  29.  
  30. s = '1321131112'
  31. for i in range(16):
  32. s = re_d.sub(replace,s)
  33. print(len(s))
  34.  
  35. CPU times: user 4 µs, sys: 1 µs, total: 5 µs
  36. Wall time: 8.82 µs
  37. 860
  38.  
  39.  
  40.