Skip to content

Largest Series Product

Find the largest product of a given number of adjacent digits in a string of digits.

Adapted from Exercism’s problem specification (MIT). Listed under Numbers & Math.

// FILE: largest_series_product.nomi
//# The largest product of a run of adjacent digits in a string of digits.

/// Why a string and span have no largest product.
pub enum Error
    SpanTooLong
    NotADigit
    NegativeSpan
end

/// The largest product of `span` adjacent digits in `digits`. A span of zero
/// has the empty product, 1.
pub fn largest_product(digits: String, span: Int) -> Result<Int, Error>
    if span < 0 then return Err(.NegativeSpan)

    Some(numbers) =
        digits
        |> String.to_codepoints()
        |> Iter.map(Codepoint.to_digit)
        |> Maybe.collect()
    else
        return Err(.NotADigit)
    end

    if span > Iter.count(numbers) then return Err(.SpanTooLong)

    if span == 0 then return Ok(1)

    numbers
    |> Iter.windows(span)
    |> Iter.map(Iter.product)
    |> Iter.max()!
    |> Ok()
end

// FILE: largest_series_product_test.nomi
import
    largest_series_product
    largest_series_product.Error
end

test "largest product"
    [
        ("finds the largest product if span equals length",         "29",         2, 18),
        ("can find the largest product of 2 with numbers in order", "0123456789", 2, 72),
        ("can find the largest product of 2",                       "576802143",  2, 48),
        ("can find the largest product of 3 with numbers in order", "0123456789", 3, 504),
        ("can find the largest product of 3",                       "1027839564", 3, 270),
        ("can find the largest product of 5 with numbers in order", "0123456789", 5, 15120),
        (
            "can get the largest product of a big number",
            "73167176531330624919225119674426574742355349194934",
            6,
            23520,
        ),
        ("reports zero if the only digits are zero",                 "0000",  2, 0),
        ("reports zero if all spans include zero",                   "99099", 3, 0),
        ("reports 1 for empty string and empty product (0 span)",    "",      0, 1),
        ("reports 1 for nonempty string and empty product (0 span)", "123",   0, 1),
    ]
    |> subtests (description, digits, span, expected), description
        assert largest_series_product.largest_product(digits, span) == Ok(expected)
    end
end

test "rejects"
    [
        ("rejects span longer than string length", "123",    4,  Error.SpanTooLong),
        ("rejects empty string and nonzero span",  "",       1,  .SpanTooLong),
        ("rejects invalid character in digits",    "1234a5", 2,  .NotADigit),
        ("rejects negative span",                  "12345",  -1, .NegativeSpan),
    ]
    |> subtests (description, digits, span, expected), description
        assert largest_series_product.largest_product(digits, span) == Err(expected)
    end
end
  • Errors are an Error enum, one variant per canonical message. The checks run in the order the canonical errors imply: a negative span, then a character that is not a digit, then a span longer than the digits.
  • The digits become a List<Int> in one pipe: String.to_codepoints, Codepoint.to_digit on each and Maybe.collect, whose None is the not-a-digit error through a binding else.
  • The span check compares the span with the digit count, before any series is built, since Iter.windows yields no series for a span longer than the digits and the error must be told apart from a product.
  • Each series is a window from Iter.windows(span), and its product is Iter.product, passed to Iter.map as a function value.
  • The largest product is Iter.max()!. The span is checked against the digit count first, so there is always one series and the None would be a bug (style §29). An Iter.reduce from 0 also works, since no product is negative, but it spells out what Iter.max names.
  • A span of 0 returns 1, the empty product, before the windows: Iter.windows(0) yields no window, as a non-positive size does in Iter.chunks, where the exercise wants one empty series.
  • The canonical cases are two subtests tables, the products and the rejections, since the rows of each share an assertion.