Skip to content

Latest commit

 

History

History
81 lines (57 loc) · 1.51 KB

034-perdidos.livemd

File metadata and controls

81 lines (57 loc) · 1.51 KB

34 - Números Perdidos

LOS NÚMEROS PERDIDOS

Enunciado

Dado un array de enteros ordenado y sin repetidos, crea una función que calcule y retorne todos los que faltan entre el mayor y el menor.

  • Lanza un error si el array de entrada no es correcto.

Solución

defmodule Solution do
  def all(input) do
    first = List.first(input)
    last = List.last(input)
    Enum.to_list(first..last)
  end

  def run(input) do
    sorted =
      Enum.sort(input)
      |> Enum.uniq()

    cond do
      sorted != input -> {:error, "Input is not acceptable"}
      true -> all(sorted)
    end
  end
end
{:module, Solution, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:run, 1}}
Solution.run([4, 5, 8, 10, 20, 48])
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48]
Solution.run([4, 1, 3])
{:error, "Input is not acceptable"}
Solution.run([2, 2, 2, 4])
{:error, "Input is not acceptable"}