crypto-square
This commit is contained in:
@@ -19,13 +19,17 @@ defmodule CryptoSquare do
|
||||
|> String.replace(~r/[^a-z0-9]/, "")
|
||||
end
|
||||
|
||||
defp square(""), do: ""
|
||||
|
||||
defp square(str) do
|
||||
length = String.length(str)
|
||||
row = :math.ceil(length ** 0.5)
|
||||
col = :math.ceil(length / row)
|
||||
row = round(ceil(length ** 0.5))
|
||||
|
||||
str
|
||||
|> String.split("", trim: true)
|
||||
|> Enum.chunk_every(col, col)
|
||||
|> Enum.chunk_every(row, row, List.duplicate(" ", row))
|
||||
|> Enum.zip()
|
||||
|> Enum.map(&Tuple.to_list(&1))
|
||||
|> Enum.map_join(" ", &Enum.join(&1))
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user