quinta-feira, 25 de outubro de 2018

Elixir Coding Dojo at UTFPR

We have had an Elixir Coding Dojo at UTFPR yesterday.

I was the Sensei and there was approximately 8 participants.

Some links I have suggested to the participants who want to learn more about Elixir are on this Twitter thread.

The Code Kata (problem) we chose to solve was Look and Say.

Code was posted as a gist but it is also below:

look_and_say.ex

defmodule LookAndSay do
  def lookandsay(numerodo
    numero
    |> Integer.digits()
    |> lookandsay_inicia()
    |> Integer.undigits()
  end

  defp lookandsay_inicia([cabeca | cauda]) do
    lookandsayauxaux(cabecacauda, 1)
  end

  defp lookandsayauxaux(estado, [], ido
    [iestado]
  end

  defp lookandsayauxaux(estado, [estado | cauda], ido
    lookandsayauxaux(estadocaudai + 1)
  end

  defp lookandsayauxaux(estado, [novoestado | cauda], ido
    [iestado] ++ lookandsayauxaux(novoestadocauda, 1)
  end
end


look_and_say_test.exs

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

Code above was created using ExToHtml:



The Coding Dojo was part of a Free Software event:


Nenhum comentário:

Postar um comentário