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

Version Path
trackler-2.0.8.31 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.30 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.29 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.28 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.27 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.26 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.24 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.23 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.22 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.21 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.20 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.19 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.18 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.17 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.16 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.15 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.14 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.13 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.12 tracks/elixir/exercises/run-length-encoding/example.exs
trackler-2.0.8.11 tracks/elixir/exercises/run-length-encoding/example.exs