🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Largest Series Product in the Scala Track

Published at Aug 28 2019 · 0 comments
Instructions
Test suite
Solution

Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.

For example, for the input '1027839564', the largest product for a series of 3 digits is 270 (9 * 5 * 6), and the largest product for a series of 5 digits is 7560 (7 * 8 * 3 * 9 * 5).

Note that these series are only required to occupy adjacent positions in the input; the digits need not be numerically consecutive.

For the input '73167176531330624919225119674426574742355349194934', the largest product for a series of 6 digits is 23520.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

A variation on Problem 8 at Project Euler http://projecteuler.net/problem=8

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

SeriesTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class LargestSeriesProductTest extends FunSuite with Matchers {

  test("finds the largest product if span equals length") {
    Series.largestProduct(2, "29") should be(Some(18))
  }

  test("can find the largest product of 2 with numbers in order") {
    pending
    Series.largestProduct(2, "0123456789") should be(Some(72))
  }

  test("can find the largest product of 2") {
    pending
    Series.largestProduct(2, "576802143") should be(Some(48))
  }

  test("can find the largest product of 3 with numbers in order") {
    pending
    Series.largestProduct(3, "0123456789") should be(Some(504))
  }

  test("can find the largest product of 3") {
    pending
    Series.largestProduct(3, "1027839564") should be(Some(270))
  }

  test("can find the largest product of 5 with numbers in order") {
    pending
    Series.largestProduct(5, "0123456789") should be(Some(15120))
  }

  test("can get the largest product of a big number") {
    pending
    Series.largestProduct(
      6,
      "73167176531330624919225119674426574742355349194934") should be(
      Some(23520))
  }

  test("reports zero if the only digits are zero") {
    pending
    Series.largestProduct(2, "0000") should be(Some(0))
  }

  test("reports zero if all spans include zero") {
    pending
    Series.largestProduct(3, "99099") should be(Some(0))
  }

  test("rejects span longer than string length") {
    pending
    Series.largestProduct(4, "123") should be(None)
  }

  test("reports 1 for empty string and empty product (0 span)") {
    pending
    Series.largestProduct(0, "") should be(Some(1))
  }

  test("reports 1 for nonempty string and empty product (0 span)") {
    pending
    Series.largestProduct(0, "123") should be(Some(1))
  }

  test("rejects empty string and nonzero span") {
    pending
    Series.largestProduct(1, "") should be(None)
  }

  test("rejects invalid character in digits") {
    pending
    Series.largestProduct(2, "1234a5") should be(None)
  }

  test("rejects negative span") {
    pending
    Series.largestProduct(-1, "12345") should be(None)
  }
}
import scala.util.Try

object Series {
  def largestProduct(product: Int, digits: String): Option[Int] = product match {
    case 0 => Some(1)
    case _ if Try[Boolean](isValid(product, digits)) isFailure => None
    case _ => Some(digits.map(_.asDigit).sliding(product).map(_.product).max)
  }

  private def isValid(product: Int, digits: String):Boolean = {
    require(product >= 0)
    require(digits.length >= product)
    require(digits.forall(_.isDigit))
    true
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?