aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Krňávek <Jan.Krnavek@gmail.com>2024-01-14 15:26:53 +0100
committerJan Krňávek <Jan.Krnavek@gmail.com>2024-01-14 15:26:53 +0100
commitedbba80c7b75a8db46bc830caff263d1dc3d701e (patch)
treea1869b7fa102cad62dd1a535da9bc9cd4130cb7a
parentdd4895f90e2ad2036d1ebe266acf9aa598b18d5b (diff)
downloadperlweeklychallenge-club-edbba80c7b75a8db46bc830caff263d1dc3d701e.tar.gz
perlweeklychallenge-club-edbba80c7b75a8db46bc830caff263d1dc3d701e.tar.bz2
perlweeklychallenge-club-edbba80c7b75a8db46bc830caff263d1dc3d701e.zip
solution week 251-1 -- Elixir
-rw-r--r--challenge-251/wambash/elixir/ch-1.exs45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-251/wambash/elixir/ch-1.exs b/challenge-251/wambash/elixir/ch-1.exs
new file mode 100644
index 0000000000..1153cee416
--- /dev/null
+++ b/challenge-251/wambash/elixir/ch-1.exs
@@ -0,0 +1,45 @@
+defmodule Concatenation do
+ def iter([]) do
+ nil
+ end
+
+ def iter([o]) do
+ {o, []}
+ end
+
+ def iter([f | t]) do
+ with [l | t] <- Enum.reverse(t) do
+ {String.to_integer("#{f}#{l}"), Enum.reverse(t)}
+ end
+ end
+
+ def value(ints) do
+ ints
+ |> Stream.unfold(&iter/1)
+ |> Enum.sum()
+ end
+end
+
+ExUnit.start()
+
+defmodule ConcatenationTest do
+ use ExUnit.Case
+
+ test "concatenation value" do
+ assert Concatenation.iter([6, 12, 25, 1]) == {61, [12, 25]}
+ assert Concatenation.iter([12, 25]) == {1225, []}
+ assert Concatenation.iter([]) == nil
+ assert Concatenation.value([6, 12, 25, 1]) == 1286
+ end
+
+ test "concatenation value more elems" do
+ assert Concatenation.iter([10, 7, 31, 5, 2, 2]) == {102, [7, 31, 5, 2]}
+ assert Concatenation.value([10, 7, 31, 5, 2, 2]) == 489
+ end
+
+ test "concatenation value odd elems" do
+ assert Concatenation.iter([1, 2, 10]) == {110, [2]}
+ assert Concatenation.iter([2]) == {2, []}
+ assert Concatenation.value([1, 2, 10]) == 112
+ end
+end