Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

A small task

2023_12_05.livemd

A small task

Section

f = fn x -> x end

f.([{2, 1, 3}, {4, 5, 1}, {5, 2, 1}]) == {5, 5, 3}
f.([{2, 1, 3}, {4, 5, 1}]) == {4, 5, 3}
f.([{2, 1, 3}, {4, 5, 10}, {2, 10, 3}, {40, 5, 1}]) == {40, 10, 10}
Enum.max([10, 2, 3])
max_first =
  [{2, 1, 3}, {4, 5, 10}, {2, 10, 3}, {40, 5, 1}]
  |> Enum.map(fn {x, _, _} -> x end)
  |> Enum.max()
max_second =
  [{2, 1, 3}, {4, 5, 10}, {2, 10, 3}, {40, 5, 1}]
  |> Enum.map(fn {_, x, _} -> x end)
  |> Enum.max()
max_third =
  [{2, 1, 3}, {4, 5, 10}, {2, 10, 3}, {40, 5, 1}]
  |> Enum.map(fn {_, _, x} -> x end)
  |> Enum.max()
defmodule SimpleTask do
  def max_tuple(list) do
    {
      max_in(list, fn {x, _, _} -> x end),
      max_in(list, fn {_, x, _} -> x end),
      max_in(list, fn {_, _, x} -> x end)
    }
  end

  defp max_in(list, function) do
    list
    |> Enum.map(function)
    |> Enum.max()
  end
end
SimpleTask.max_tuple([{2, 1, 3}, {4, 5, 1}, {5, 2, 1}]) == {5, 5, 3}
SimpleTask.max_tuple([{2, 1, 3}, {4, 5, 10}, {2, 10, 3}, {40, 5, 1}]) == {40, 10, 10}
[{2, 1, 3}, {4, 5, 10}, {2, 10, 3}, {40, 5, 1}]
|> Enum.reduce(fn {x, y, z}, {maxx, maxy, maxz} ->
  {max(x, maxx), max(y, maxy), max(z, maxz)}
end)

Second sub-task

f = fn {x, y, z} -> x * y * z end
f.({4, 5, 10}) == 4 * 5 * 10