Created
August 3, 2019 17:00
-
-
Save nyaray/133da6749416e26a0903116311fa8750 to your computer and use it in GitHub Desktop.
Run-length encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule RLE do | |
defp run_length_encode(terms, encoding) do | |
terms = Enum.map(terms, &encoding[&1]) | |
Enum.reduce(tl(terms), [{hd(terms), 1}], fn | |
c, [{c, count} | rest] -> [{c, count + 1} | rest] | |
c, [{c_prime, count} | rest] -> [{c, 1}, {c_prime, count} | rest] | |
end) | |
|> Enum.map(fn {c, count} -> "#{c}#{inspect(count)}" end) | |
|> Enum.reverse() | |
|> Enum.join() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment