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

Number Finder

exercises/number_finder.livemd

Number Finder

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue ReduceCustom Enum With Reduce

Number Finder

Create a NumberFinder module which can find the smallest/1 number in a list, and the largest/1 number in a list.

NumberFinder.smallest([2, 3, 1])
1

NumberFinder.largest([2, 3, 1])
3

Example Solution

defmodule NumberFinder do
  def smallest(number_list) do
    Enum.reduce(number_list, fn each, acc ->
      min(each, acc)
    end)
  end

  def largest(number_list) do
    Enum.reduce(number_list, fn each, acc ->
      max(each, acc)
    end)
  end
end
defmodule NumberFinder do
  @doc """
  Find the smallest number in a list

  ## Examples

    iex> NumberFinder.smallest([2, 3, 1])
    1
    iex> NumberFinder.smallest([2, 2, 3, 4])
    2
    iex> NumberFinder.smallest([2, 2, 3, 4, 10, 20, -3])
    -3
  """
  def smallest(number_list) do
    Enum.reduce(number_list, fn n, acc -> 
      min(n, acc)
    end)
  end

  @doc """
  Find the largest number in a list

  ## Examples

    iex> NumberFinder.largest([2, 3, 1])
    3
    iex> NumberFinder.largest([2, 2, 3, 4, 4])
    4
    iex> NumberFinder.largest([2, 2, 3, 4, 10, 20, -3])
    20
  """
  def largest(number_list) do
    Enum.reduce(number_list, fn n, acc -> 
      max(n, acc)
    end)
  end
end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Number Finder exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue ReduceCustom Enum With Reduce