Run-length encoding: Difference between revisions

Content added Content deleted
(→‎{{header|PARI/GP}}: rewrote solution, previous one was incomplete and incorrect)
(coffeescript implementation)
Line 483: Line 483:
(mapcat (fn [[_ n ch]] (repeat (Integer/parseInt n) ch)))
(mapcat (fn [[_ n ch]] (repeat (Integer/parseInt n) ch)))
(apply str)))</lang>
(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}}==
=={{header|Common Lisp}}==