aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-268/packy-anderson/elixir/ch-1.exs8
-rwxr-xr-xchallenge-268/packy-anderson/elixir/ch-2.exs40
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])