Sha256: 8b383f6c93e8fa685a77d75bb4fb326dd464a109dc5b26539e775d206c5ebd35
Contents?: true
Size: 619 Bytes
Versions: 41
Compression:
Stored size: 619 Bytes
Contents
defmodule RunLengthEncoder do @spec encode(String.t) :: String.t def encode(string) do Regex.scan(~r/([a-zA-Z ])\1*/, string) |> Enum.map_join(fn([run, c]) -> if String.match?(run, ~r/\s+/) do run else times = String.length(run) number = if times == 1 do "" else times end "#{number}#{c}" end end) end @spec decode(String.t) :: String.t def decode(string) do Regex.scan(~r/(\d*)(.)/, string) |> Enum.map_join(fn [_,n,c] -> times = if n == "" do 1 else String.to_integer(n) end String.duplicate(c, times) end) end end
Version data entries
41 entries across 41 versions & 1 rubygems