Sha256: c84dc78dfbd948af66f505d83de56352899b9f9f8af5694b0958d975065d0716

Contents?: true

Size: 1.49 KB

Versions: 154

Compression:

Stored size: 1.49 KB

Contents

defmodule Matrix do
  @doc """
  Parses a string representation of a matrix
  to a list of rows
  """
  @spec rows(String.t()) :: [[integer]]
  def rows(str) do
    str
    |> String.split("\n")
    |> Enum.map(&parse_row/1)
  end

  defp parse_row(str) do
    str
    |> String.split(" ")
    |> Enum.map(&String.to_integer/1)
  end

  @doc """
  Parses a string representation of a matrix
  to a list of columns
  """
  @spec columns(String.t()) :: [[integer]]
  def columns(str) do
    str
    |> rows
    |> List.zip
    |> Enum.map(&Tuple.to_list/1)
  end

  @doc """
  Calculates all the saddle points from a string
  representation of a matrix
  """
  @spec saddle_points(String.t()) :: [{integer, integer}]
  def saddle_points(str) do
    rows = rows(str)
    columns = columns(str)
    rows
    |> generate_coordinates
    |> Enum.filter(&is_saddle_point?(&1, rows, columns))
  end

  defp is_saddle_point?(point, rows, columns) do
    max_in_row?(point, rows) && min_in_column?(point, columns)
  end

  defp max_in_row?({x, y}, rows) do
    row = Enum.at(rows, x)
    Enum.at(row, y) == Enum.max(row)
  end

  defp min_in_column?({x, y}, columns) do
    column = Enum.at(columns, y)
    Enum.at(column, x) == Enum.min(column)
  end

  defp generate_coordinates(rows) do
    rows
    |> Enum.with_index
    |> Enum.flat_map(&generate_coordinates_row/1)
  end

  defp generate_coordinates_row({row, row_index}) do
    row
    |> Enum.with_index
    |> Enum.map(fn {_, col_index} -> {row_index, col_index} end)
  end
end

Version data entries

154 entries across 154 versions & 1 rubygems

Version Path
trackler-2.0.8.54 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.53 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.52 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.51 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.50 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.49 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.48 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.47 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.46 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.45 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.44 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.43 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.42 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.41 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.40 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.39 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.38 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.37 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.36 tracks/elixir/exercises/saddle-points/example.exs
trackler-2.0.8.35 tracks/elixir/exercises/saddle-points/example.exs