diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-11 14:11:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-11 14:11:42 +0100 |
| commit | c1d0c58785500d4cc03257c2afa329ffb05575d5 (patch) | |
| tree | 57e7c9e4d9d9b3e4aea0edb8505a48718cba698b | |
| parent | 5a23d92684bafea4448fe1906ec8aa4ff4cf6f00 (diff) | |
| parent | 50431e4a3ce75d96370bc66fadae38e188999416 (diff) | |
| download | perlweeklychallenge-club-c1d0c58785500d4cc03257c2afa329ffb05575d5.tar.gz perlweeklychallenge-club-c1d0c58785500d4cc03257c2afa329ffb05575d5.tar.bz2 perlweeklychallenge-club-c1d0c58785500d4cc03257c2afa329ffb05575d5.zip | |
Merge pull request #10069 from packy/master
Challenge 267 Guest solutions in Elixir by Packy Anderson
| -rw-r--r-- | challenge-268/packy-anderson/README.md | 4 | ||||
| -rwxr-xr-x | challenge-268/packy-anderson/elixir/ch-1.exs | 53 | ||||
| -rwxr-xr-x | challenge-268/packy-anderson/elixir/ch-2.exs | 40 |
3 files changed, 97 insertions, 0 deletions
diff --git a/challenge-268/packy-anderson/README.md b/challenge-268/packy-anderson/README.md index 736b69a97f..ef082e98a3 100644 --- a/challenge-268/packy-anderson/README.md +++ b/challenge-268/packy-anderson/README.md @@ -14,6 +14,10 @@ * [Task 1](python/ch-1.py) * [Task 2](python/ch-2.py) +## Guest Language: Elixir +* [Task 1](elixir/ch-1.exs) +* [Task 2](elixir/ch-2.exs) + ## Blog Post [Let's do the Numbers!](https://packy.dardan.com/b/L6) 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..86099dac11 --- /dev/null +++ b/challenge-268/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,53 @@ +#!/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]) diff --git a/challenge-268/packy-anderson/elixir/ch-2.exs b/challenge-268/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..e0f0fae4d0 --- /dev/null +++ b/challenge-268/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,40 @@ +#!/usr/bin/env elixir + +defmodule PWC do + # list is empty, return results + defp numberGame([], results) do + results + end + + # grab the first elem off list, recurse + defp numberGame([x | rest], results) do + numberGame(x, rest, results) + end + + # grab second elem off list, swap them, recurse + defp numberGame(x, [y | rest], results) do + results = Enum.concat(results, [y, x]) + numberGame(rest, results) + end + + # sort the list and then recursively swap element pairs + def numberGame(ints) do + sortedInts = Enum.sort(ints) + numberGame(sortedInts, []) + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + result = PWC.numberGame(ints) + IO.puts("Output: (" <> Enum.join(result, ", ") <> ")") + end +end + +IO.puts("Example 1:") +PWC.solution([2, 5, 3, 4]) + +IO.puts("\nExample 2:") +PWC.solution([9, 4, 1, 3, 6, 4, 6, 1]) + +IO.puts("\nExample 3:") +PWC.solution([1, 2, 2, 3]) |
