diff options
| author | Packy Anderson <packy@cpan.org> | 2024-05-08 00:20:16 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-05-08 00:20:16 -0400 |
| commit | e62a9b316dac702fd890dbf349aa21ecc8089289 (patch) | |
| tree | f2643f6fcc2e5b8f7780f9678ac2484dceaca81c | |
| parent | 56ecb5995d71ed710da5e103eb89d35e6e450a30 (diff) | |
| download | perlweeklychallenge-club-e62a9b316dac702fd890dbf349aa21ecc8089289.tar.gz perlweeklychallenge-club-e62a9b316dac702fd890dbf349aa21ecc8089289.tar.bz2 perlweeklychallenge-club-e62a9b316dac702fd890dbf349aa21ecc8089289.zip | |
Challenge 268 Task 1 guest solution in Elixir by Packy Anderson
| -rwxr-xr-x | challenge-268/packy-anderson/elixir/ch-1.exs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-268/packy-anderson/elixir/ch-1.exs b/challenge-268/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..a1f791bcab --- /dev/null +++ b/challenge-268/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,49 @@ +#!/usr/bin/env elixir + +defmodule PWC do + defp checkNums(magic, [], []) do + { :ok, magic } + end + + defp checkNums(magic, [x | rX], [y | rY]) do + if y - x == magic do + checkNums(magic, rX, rY) + else + { :err, 0 } + end + end + + def magicNumber(x, y) do + xS = Enum.sort(x) + yS = Enum.sort(y) + magic = Enum.at(yS, 0) - Enum.at(xS, 0) + checkNums(magic, xS, yS) + end + + def solution(x, y) do + IO.puts("Input: @x = (" <> Enum.join(x, ", ") <> ")") + IO.puts(" @y = (" <> Enum.join(y, ", ") <> ")") + case PWC.magicNumber(x, y) do + {:ok, magic} -> + IO.puts("Output: #{to_string(magic)}") + IO.puts("\nThe magic number is #{to_string(magic)}.") + IO.puts("@x = (" <> Enum.join(x, ", ") <> ")") + IO.puts(" + " <> Enum.join(Enum.map(x, fn _ -> magic end), " ")) + IO.puts("@y = (" <> Enum.join(Enum.map(x, fn n -> n + magic end), ", ") <> ")") + {:err, _} -> + IO.puts('Output: no magic number') + end + end +end + +IO.puts("Example 1:") +PWC.solution([3, 7, 5], [9, 5, 7]) + +IO.puts("\nExample 2:") +PWC.solution([1, 2, 1], [5, 4, 4]) + +IO.puts("\nExample 3:") +PWC.solution([2], [5]) + +IO.puts("\nExample 4:") +PWC.solution([1, 2], [4, 2]) |
