diff options
| author | Packy Anderson <packy@cpan.org> | 2024-05-09 00:02:57 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-05-09 00:02:57 -0400 |
| commit | 868bef33df5d0b4608d69195af39dded2ae5d3df (patch) | |
| tree | 583dc8977a02074def651d7877377bf49c47d592 | |
| parent | e62a9b316dac702fd890dbf349aa21ecc8089289 (diff) | |
| download | perlweeklychallenge-club-868bef33df5d0b4608d69195af39dded2ae5d3df.tar.gz perlweeklychallenge-club-868bef33df5d0b4608d69195af39dded2ae5d3df.tar.bz2 perlweeklychallenge-club-868bef33df5d0b4608d69195af39dded2ae5d3df.zip | |
Challenge 268 Task 2 guest solution in Elixir by Packy Anderson
| -rwxr-xr-x | challenge-268/packy-anderson/elixir/ch-1.exs | 8 | ||||
| -rwxr-xr-x | challenge-268/packy-anderson/elixir/ch-2.exs | 40 |
2 files changed, 46 insertions, 2 deletions
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]) |
