Skip to content

Instantly share code, notes, and snippets.

@agnellvj
Last active August 9, 2017 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agnellvj/f7d3e85233aed7f7773b7f8379c2d7d3 to your computer and use it in GitHub Desktop.
Save agnellvj/f7d3e85233aed7f7773b7f8379c2d7d3 to your computer and use it in GitHub Desktop.
# A B C D
# E F G H
# I J K L
# M N O P
# a b c d h l p o n m i e f g k j"
# top, right, bottom, left
#
defmodule Spiralizer do
defp top(acc, []) do
acc
end
defp top(acc, [head | tail]) do
new_acc = Enum.concat(acc, head)
right(new_acc, tail)
end
defp right(acc, []) do
acc
end
defp right(acc, data) do
new_acc = acc
|> Enum.concat(data |> Enum.map(&(List.last(&1))))
new_arr = Enum.map(data, fn(row) ->
[head | tail] = Enum.reverse(row)
Enum.reverse(tail)
end)
bottom(new_acc, new_arr)
end
defp bottom(acc, []) do
acc
end
defp bottom(acc, data) do
new_acc = acc
|> Enum.concat(data |> Enum.reverse() |> List.first() |> Enum.reverse())
[head | tail] = Enum.reverse(data)
new_arr = Enum.reverse(tail)
left(new_acc, new_arr)
end
defp left(acc, []) do
acc
end
defp left(acc, data) do
new_acc = acc
|> Enum.concat(data |> Enum.map(&(List.first(&1))) |> Enum.reverse())
new_arr = Enum.map(data, fn([_head | tail]) -> tail end)
top(new_acc, new_arr)
end
def runner(data) do
top([], data) |> Enum.map(&(String.downcase(&1))) |> Enum.join(" ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment