RosettaCodeData/Task/Run-length-encoding/CoffeeScript/run-length-encoding-1.coffeescript
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

11 lines
303 B
Text

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