RosettaCodeData/Task/Run-length-encoding/CoffeeScript/run-length-encoding-1.coffee

12 lines
303 B
CoffeeScript
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
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