Sha256: 288ce707178ee42b36e33efbcfd54260cb0b04052b81e8bcdfc5fa81b83a567e

Contents?: true

Size: 1.2 KB

Versions: 32

Compression:

Stored size: 1.2 KB

Contents

defmodule Series do
  @spec digits(String.t()) :: [non_neg_integer]
  defp digits("") do
    []
  end

  defp digits(number_string) do
    String.split(number_string, "", trim: true)
    |> Enum.reduce([], fn char, acc -> [String.to_integer(char) | acc] end)
    |> Enum.reverse()
  end

  @spec slices(String.t(), non_neg_integer) :: [list(non_neg_integer)]
  defp slices(number_string, size) do
    digits = digits(number_string)
    chunk(digits, size, 1)
  end

  @doc """
  Finds the largest product of a given number of consecutive numbers in a given string of numbers.
  """
  @spec largest_product(String.t(), non_neg_integer) :: non_neg_integer
  def largest_product(_, 0) do
    1
  end

  def largest_product(number_string, size) do
    unless Enum.member?(Range.new(0, String.length(number_string)), size) do
      raise ArgumentError
    end

    slices = slices(number_string, size)

    Enum.map(slices, &Enum.reduce(&1, fn x, acc -> x * acc end))
    |> Enum.max()
  end

  # Handle Enum.chunks -> Enum.chunk renaming.
  if {:chunk, 3} not in Enum.__info__(:functions) do
    defp chunk(coll, n, step), do: Enum.chunks(coll, n, step)
  else
    defp chunk(coll, n, step), do: Enum.chunk(coll, n, step)
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/largest-series-product/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/largest-series-product/example.exs