Skip to content

Instantly share code, notes, and snippets.

@adamrobbie
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save adamrobbie/4e14dba97e59adf2f2c9 to your computer and use it in GitHub Desktop.

Select an option

Save adamrobbie/4e14dba97e59adf2f2c9 to your computer and use it in GitHub Desktop.

Revisions

  1. adamrobbie revised this gist Sep 29, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions run_length.ex
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@
    defmodule RunLength do
    def run(string) do
    compress(String.slice(string, 1..-1), String.slice(string, 0, 1), 1, [])
    compress(String.slice(string, 1..-1), String.slice(string, 0, 1), 1, "")
    end

    defp compress("", char, count, result) do
    result ++ [Integer.to_string(count) <> char]
    result <> Integer.to_string(count) <> char
    end

    defp compress(string, char, count, result) do
    @@ -16,7 +16,7 @@ defmodule RunLength do
    String.slice(string, 1..-1),
    next,
    1,
    result ++ [Integer.to_string(count) <> char]
    result <> Integer.to_string(count) <> char
    )
    end
    end
  2. adamrobbie revised this gist Sep 29, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions run_length.ex
    Original file line number Diff line number Diff line change
    @@ -3,12 +3,12 @@ defmodule RunLength do
    compress(String.slice(string, 1..-1), String.slice(string, 0, 1), 1, [])
    end

    defp compress([], char, count, result) do
    defp compress("", char, count, result) do
    result ++ [Integer.to_string(count) <> char]
    end

    defp compress(string, char, count, result) do
    next = String.slice(string, 1, 1)
    next = String.slice(string, 0, 1)
    if next == char do
    compress(String.slice(string, 1..-1), char, count+1, result)
    else
  3. adamrobbie created this gist Sep 29, 2014.
    23 changes: 23 additions & 0 deletions run_length.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    defmodule RunLength do
    def run(string) do
    compress(String.slice(string, 1..-1), String.slice(string, 0, 1), 1, [])
    end

    defp compress([], char, count, result) do
    result ++ [Integer.to_string(count) <> char]
    end

    defp compress(string, char, count, result) do
    next = String.slice(string, 1, 1)
    if next == char do
    compress(String.slice(string, 1..-1), char, count+1, result)
    else
    compress(
    String.slice(string, 1..-1),
    next,
    1,
    result ++ [Integer.to_string(count) <> char]
    )
    end
    end
    end