Created
October 25, 2018 17:50
-
-
Save adolfont/8ee956824edb86e11e622ebd5e280514 to your computer and use it in GitHub Desktop.
Elixir Coding Dojo at UTFPR - 24/October/2018
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 LookAndSay do | |
def lookandsay(numero) do | |
numero | |
|> Integer.digits() | |
|> lookandsay_inicia() | |
|> Integer.undigits() | |
end | |
defp lookandsay_inicia([cabeca | cauda]) do | |
lookandsayauxaux(cabeca, cauda, 1) | |
end | |
defp lookandsayauxaux(estado, [], i) do | |
[i, estado] | |
end | |
defp lookandsayauxaux(estado, [estado | cauda], i) do | |
lookandsayauxaux(estado, cauda, i + 1) | |
end | |
defp lookandsayauxaux(estado, [novoestado | cauda], i) do | |
[i, estado] ++ lookandsayauxaux(novoestado, cauda, 1) | |
end | |
end |
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 LookAndSayTest do | |
use ExUnit.Case | |
test "Look and Say de 1 é 11" do | |
assert LookAndSay.lookandsay(1) == 11 | |
end | |
test "Look and Say de 555 é 35" do | |
assert LookAndSay.lookandsay(555) == 35 | |
end | |
test "Look and Say de 55 é 25" do | |
assert LookAndSay.lookandsay(55) == 25 | |
end | |
test "Look and Say de 11 é 21" do | |
assert LookAndSay.lookandsay(11) == 21 | |
end | |
test "Look and Say de 5 é 15" do | |
assert LookAndSay.lookandsay(5) == 15 | |
end | |
test "Look and Say de 123 é 111213" do | |
assert LookAndSay.lookandsay(123) == 111_213 | |
end | |
test "Look and Say de 551 é 2511" do | |
assert LookAndSay.lookandsay(551) == 2511 | |
end | |
test "Look and Say de 555555 é 65" do | |
assert LookAndSay.lookandsay(555_555) == 65 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment