I'm starting with Elixir and as exercise I wrote the Berlin Clock kata:
- The clock is made up of 5 rows.
- On the very top of the clock is a lamp that blinks to show the seconds. It turns on for one second, then off for one second, and so on.
- The next 2 rows represent the hours. The upper most of these rows represents 5 hour blocks and is made up of 4 red lamps. The second row represents 1 hour blocks and is also made up of 4 red lamps.
- The final two rows represent the minutes. The upper most row repesents 5 minute blocks and is made up of 11 lamps- every 3rd lamp red but the rest yellow. The second row represents 1 minute blocks and is made up of 4 yellow lamps.
Here's a picture:
The full code is available here.
import Integer, only: :macros defmodule BerlinClock do def parse (time) do [h, m, s] = String.split(time, ":") |> Enum.map fn n -> Integer.parse(n) |> elem(0) end seconds = get_seconds(s) single_minutes = get_single_minutes(m) fives_minutes = get_fives_minutes(m) single_hours = get_single_hours(h) fives_hours = get_fives_hours(h) [seconds, fives_hours, single_hours, fives_minutes, single_minutes] end def get_fives_hours(h) do number_of_r = div(h, 5) String.ljust(String.duplicate("R", number_of_r), 4, ?O) end def get_single_hours(h) do number_of_r = rem(h, 5) String.ljust(String.duplicate("R", number_of_r), 4, ?O) end def get_fives_minutes(m) do number_of_y = div(m, 5) String.ljust(create_fives_minutes(number_of_y), 11, ?O) end def create_fives_minutes(1) do "Y" end def create_fives_minutes(n) when n < 1 do "" end def create_fives_minutes(n) when n > 1 do if rem(n,3) == 0 do light = "R" else light = "Y" end [create_fives_minutes(n-1), light ] |> Enum.join end def get_single_minutes(m) do number_of_y = rem(m, 5) String.ljust(String.duplicate("Y", number_of_y), 4, ?O) end def get_seconds(s) do cond do s |> even? -> "Y" s |> odd? -> "O" end end end