RosettaCodeData/Task/Run-length-encoding/Arturo/run-length-encoding.arturo

22 lines
538 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
runlengthEncode: function [s][
join map chunk split s => [&] 'x ->
(to :string size x) ++ first x
]
runlengthDecode: function [s][
result: new ""
2026-02-01 16:33:20 -08:00
loop (chunk split s 'x -> match? x {/\d+/}) [a,b] ->
2023-07-01 11:58:00 -04:00
'result ++ repeat first b to :integer join to [:string] a
return result
]
str: "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
encoded: runlengthEncode str
print ["encoded:" encoded]
decoded: runlengthDecode encoded
print ["decoded:" decoded]
if decoded=str -> print "\nSuccess!"