Tracks
/
Python
Python
/
Exercises
/
Largest Series Product
Largest Series Product

Largest Series Product

Medium

Introduction

You work for a government agency that has intercepted a series of encrypted communication signals from a group of bank robbers. The signals contain a long sequence of digits. Your team needs to use various digital signal processing techniques to analyze the signals and identify any patterns that may indicate the planning of a heist.

Instructions

Your task is to look for patterns in the long sequence of digits in the encrypted signal.

The technique you're going to use here is called the largest series product.

Let's define a few terms, first.

  • input: the sequence of digits that you need to analyze
  • series: a sequence of adjacent digits (those that are next to each other) that is contained within the input
  • span: how many digits long each series is
  • product: what you get when you multiply numbers together

Let's work through an example, with the input "63915".

  • To form a series, take adjacent digits in the original input.
  • If you are working with a span of 3, there will be three possible series:
    • "639"
    • "391"
    • "915"
  • Then we need to calculate the product of each series:
    • The product of the series "639" is 162 (6 Γ— 3 Γ— 9 = 162)
    • The product of the series "391" is 27 (3 Γ— 9 Γ— 1 = 27)
    • The product of the series "915" is 45 (9 Γ— 1 Γ— 5 = 45)
  • 162 is bigger than both 27 and 45, so the largest series product of "63915" is from the series "639". So the answer is 162.

Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should always include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the built in error types, but should still include a meaningful message.

This particular exercise requires that you use the raise statement to "throw" a ValueError when your largest_product() function receives invalid input. The tests will only pass if you both raise the exception and include a message with it. Feel free to reuse your code from the series exercise!

To raise a ValueError with a message, write the message as an argument to the exception type:

# span of numbers is longer than number series
raise ValueError("span must be smaller than string length")

# span of number is negative
raise ValueError("span must not be negative")

# series includes non-number input
raise ValueError("digits input must only contain digits")
Edit via GitHub The link opens in a new window or tab
Python Exercism

Ready to start Largest Series Product?

Sign up to Exercism to learn and master Python with 17 concepts, 140 exercises, and real human mentoring, all for free.