Anonymous user
Run-length encoding: Difference between revisions
coffeescript implementation
(→{{header|PARI/GP}}: rewrote solution, previous one was incomplete and incorrect) |
(coffeescript implementation) |
||
Line 483:
(mapcat (fn [[_ n ch]] (repeat (Integer/parseInt n) ch)))
(apply str)))</lang>
=={{header|Coffeescript}}==
<lang coffeescript>encode = (str) ->
str.replace /(.)\1*/g, (w) ->
w[0] + w.length
decode = (str) ->
str.replace /(.)(\d+)/g, (m,w,n) ->
new Array(+n+1).join(w)
console.log s = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
console.log encode s
console.log decode encode s</lang>
<pre>
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
W12B1W12B3W24B1W14
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
</pre>
=={{header|Common Lisp}}==
|