Last active
June 10, 2017 06:36
-
-
Save VikingMew/9283117b5f8a7e59e1f014244f5de8e5 to your computer and use it in GitHub Desktop.
runlength with codepoints.
This file contains hidden or 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 Runlength do | |
| def encode(string), do: c2s encode_2 String.codepoints string | |
| def encode_2([h]), do: [1,h] | |
| def encode_2([h, h2 | t]) when h == h2, do: plusplus(encode_2([h2 |t])) | |
| def encode_2([h, h2 | t]) , do: [1,h] ++ encode_2([h2 |t]) | |
| def c2s([]), do: "" | |
| def c2s([h,h2|t]), do: Integer.to_string(h) <> h2 <> c2s(t) | |
| def plusplus([h|t]), do: [h+1|t] | |
| def decode(string), do: decode_2 String.codepoints string | |
| def decode_2([]), do: "" | |
| def decode_2([h,h2|t]), do: String.duplicate(h2,String.to_integer h) <> decode_2(t) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment