From e62a9b316dac702fd890dbf349aa21ecc8089289 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Wed, 8 May 2024 00:20:16 -0400 Subject: Challenge 268 Task 1 guest solution in Elixir by Packy Anderson --- challenge-268/packy-anderson/elixir/ch-1.exs | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 challenge-268/packy-anderson/elixir/ch-1.exs 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]) -- cgit From 868bef33df5d0b4608d69195af39dded2ae5d3df Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Thu, 9 May 2024 00:02:57 -0400 Subject: Challenge 268 Task 2 guest solution in Elixir by Packy Anderson --- challenge-268/packy-anderson/elixir/ch-1.exs | 8 ++++-- challenge-268/packy-anderson/elixir/ch-2.exs | 40 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100755 challenge-268/packy-anderson/elixir/ch-2.exs diff --git a/challenge-268/packy-anderson/elixir/ch-1.exs b/challenge-268/packy-anderson/elixir/ch-1.exs index a1f791bcab..86099dac11 100755 --- a/challenge-268/packy-anderson/elixir/ch-1.exs +++ b/challenge-268/packy-anderson/elixir/ch-1.exs @@ -28,8 +28,12 @@ defmodule PWC do 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), ", ") <> ")") + 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 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]) -- cgit From 50431e4a3ce75d96370bc66fadae38e188999416 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Thu, 9 May 2024 02:28:22 -0400 Subject: Add Elixir to README --- challenge-268/packy-anderson/README.md | 4 ++++ 1 file changed, 4 insertions(+) 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) -- cgit