RosettaCodeData/Task/Run-length-encoding/Arturo/run-length-encoding.arturo
2026-02-01 16:33:20 -08:00

21 lines
538 B
Text

runlengthEncode: function [s][
join map chunk split s => [&] 'x ->
(to :string size x) ++ first x
]
runlengthDecode: function [s][
result: new ""
loop (chunk split s 'x -> match? x {/\d+/}) [a,b] ->
'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!"