Skip to content

Latest commit

 

History

History
50 lines (32 loc) · 833 Bytes

Largest_product_in_a_series.md

File metadata and controls

50 lines (32 loc) · 833 Bytes

CodeWars Python Solutions


Largest product in a series

Complete the greatestProduct method so that it'll find the greatest product of five consecutive digits in the given string of digits.

For example:

greatestProduct("123834539327238239583") // should return 3240

The input string always has more than five digits.

Adapted from Project Euler.


Given Code

def greatest_product(n):
    pass

Solution

def greatest_product(n):
    mx = 0
    for i in range(len(n)+1):
        pr = 1
        for x in n[i:i+5]:
            if len(n[i:i+5]) < 5 or "0" in n[i:i+5]:
                break
            pr = pr * int(x)
        if pr > mx and pr != 1: mx = pr
    return mx

See on CodeWars.com