A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Transpose Matrix in Elixir page! Here, you'll find the source code for this program as well as a description of how the program works.
defmodule TransposeMatrix do
@doc """
Transposes a matrix represented as a list of lists.
"""
@spec transpose_matrix(matrix :: list(list(integer()))) :: list(list(integer()))
def transpose_matrix(matrix), do: Enum.zip(matrix) |> Enum.map(&Tuple.to_list/1)
@spec parse_list(str :: String.t()) :: {:ok, list(integer())} | :error
def parse_list(str) do
res = str
|> String.split(",")
|> Enum.map(&String.trim/1)
|> Enum.reduce_while({:ok, []}, fn n, {:ok, acc} ->
case Integer.parse(n) do
{n, ""} -> {:cont, {:ok, [n | acc]}}
_ -> {:halt, :error}
end
end)
case res do
{:ok, list} -> {:ok, Enum.reverse(list)}
:error -> :error
end
end
end
with [cols, rows, matrix] <- System.argv(),
{cols, ""} when cols > 0 <- Integer.parse(cols),
{rows, ""} when rows > 0 <- Integer.parse(rows),
{:ok, matrix} when rows * cols == length(matrix) <- TransposeMatrix.parse_list(matrix) do
Enum.chunk_every(matrix, cols)
|> TransposeMatrix.transpose_matrix()
|> Enum.map(&Enum.join(&1, ", "))
|> Enum.join(", ")
|> IO.puts()
else
_ -> IO.puts("Usage: please enter the dimension of the matrix and the serialized matrix")
end
Transpose Matrix in Elixir was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.