diff --git a/elixir/crypto-square/lib/crypto_square.ex b/elixir/crypto-square/lib/crypto_square.ex index c1bb6a2..f11f5f4 100644 --- a/elixir/crypto-square/lib/crypto_square.ex +++ b/elixir/crypto-square/lib/crypto_square.ex @@ -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 diff --git a/elixir/crypto-square/test/crypto_square_test.exs b/elixir/crypto-square/test/crypto_square_test.exs index 8d2fcfb..12bf87d 100644 --- a/elixir/crypto-square/test/crypto_square_test.exs +++ b/elixir/crypto-square/test/crypto_square_test.exs @@ -1,42 +1,34 @@ defmodule CryptoSquareTest do use ExUnit.Case - # @tag :pending test "empty plaintext results in an empty ciphertext" do assert CryptoSquare.encode("") == "" end - @tag :pending test "normalization results in empty plaintext" do assert CryptoSquare.encode("... --- ...") == "" end - @tag :pending test "lowercase" do assert CryptoSquare.encode("A") == "a" end - @tag :pending test "remove spaces" do assert CryptoSquare.encode(" b ") == "b" end - @tag :pending test "remove punctuation" do assert CryptoSquare.encode("@1,%!") == "1" end - @tag :pending test "9 character plaintext results in 3 chunks of 3 characters" do assert CryptoSquare.encode("This is fun!") == "tsf hiu isn" end - @tag :pending test "8 character plaintext results in 3 chunks, the last one with a trailing space" do assert CryptoSquare.encode("Chill out.") == "clu hlt io " end - @tag :pending test "54 character plaintext results in 7 chunks, the last two with trailing spaces" do msg = "If man was meant to stay on the ground, god would have given us roots." cipher = "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau "