mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
Add style improvements to Project Euler problem 8 (#3001)
This commit is contained in:
parent
21581eae3b
commit
f3fe29cea1
|
@ -1,4 +1,6 @@
|
||||||
"""
|
"""
|
||||||
|
Problem 8: https://projecteuler.net/problem=8
|
||||||
|
|
||||||
The four adjacent digits in the 1000-digit number that have the greatest
|
The four adjacent digits in the 1000-digit number that have the greatest
|
||||||
product are 9 × 9 × 8 × 9 = 5832.
|
product are 9 × 9 × 8 × 9 = 5832.
|
||||||
|
|
||||||
|
@ -50,21 +52,21 @@ N = """73167176531330624919225119674426574742355349194934\
|
||||||
71636269561882670428252483600823257530420752963450"""
|
71636269561882670428252483600823257530420752963450"""
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n: str = N) -> int:
|
||||||
"""Find the thirteen adjacent digits in the 1000-digit number n that have
|
"""Find the thirteen adjacent digits in the 1000-digit number n that have
|
||||||
the greatest product and returns it.
|
the greatest product and returns it.
|
||||||
|
|
||||||
>>> solution(N)
|
>>> solution(N)
|
||||||
23514624000
|
23514624000
|
||||||
"""
|
"""
|
||||||
LargestProduct = -sys.maxsize - 1
|
largest_product = -sys.maxsize - 1
|
||||||
for i in range(len(n) - 12):
|
for i in range(len(n) - 12):
|
||||||
product = 1
|
product = 1
|
||||||
for j in range(13):
|
for j in range(13):
|
||||||
product *= int(n[i + j])
|
product *= int(n[i + j])
|
||||||
if product > LargestProduct:
|
if product > largest_product:
|
||||||
LargestProduct = product
|
largest_product = product
|
||||||
return LargestProduct
|
return largest_product
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
"""
|
"""
|
||||||
|
Problem 8: https://projecteuler.net/problem=8
|
||||||
|
|
||||||
The four adjacent digits in the 1000-digit number that have the greatest
|
The four adjacent digits in the 1000-digit number that have the greatest
|
||||||
product are 9 × 9 × 8 × 9 = 5832.
|
product are 9 × 9 × 8 × 9 = 5832.
|
||||||
|
|
||||||
|
@ -53,7 +55,7 @@ N = (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n: str = N) -> int:
|
||||||
"""Find the thirteen adjacent digits in the 1000-digit number n that have
|
"""Find the thirteen adjacent digits in the 1000-digit number n that have
|
||||||
the greatest product and returns it.
|
the greatest product and returns it.
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
"""
|
"""
|
||||||
|
Problem 8: https://projecteuler.net/problem=8
|
||||||
|
|
||||||
The four adjacent digits in the 1000-digit number that have the greatest
|
The four adjacent digits in the 1000-digit number that have the greatest
|
||||||
product are 9 × 9 × 8 × 9 = 5832.
|
product are 9 × 9 × 8 × 9 = 5832.
|
||||||
|
|
||||||
|
@ -50,21 +52,28 @@ N = """73167176531330624919225119674426574742355349194934\
|
||||||
71636269561882670428252483600823257530420752963450"""
|
71636269561882670428252483600823257530420752963450"""
|
||||||
|
|
||||||
|
|
||||||
def streval(s: str) -> int:
|
def str_eval(s: str) -> int:
|
||||||
ret = 1
|
"""Returns product of digits in given string n
|
||||||
for it in s:
|
|
||||||
ret *= int(it)
|
>>> str_eval("987654321")
|
||||||
return ret
|
362880
|
||||||
|
>>> str_eval("22222222")
|
||||||
|
256
|
||||||
|
"""
|
||||||
|
product = 1
|
||||||
|
for digit in s:
|
||||||
|
product *= int(digit)
|
||||||
|
return product
|
||||||
|
|
||||||
|
|
||||||
def solution(n: str) -> int:
|
def solution(n: str = N) -> int:
|
||||||
"""Find the thirteen adjacent digits in the 1000-digit number n that have
|
"""Find the thirteen adjacent digits in the 1000-digit number n that have
|
||||||
the greatest product and returns it.
|
the greatest product and returns it.
|
||||||
|
|
||||||
>>> solution(N)
|
>>> solution(N)
|
||||||
23514624000
|
23514624000
|
||||||
"""
|
"""
|
||||||
LargestProduct = -sys.maxsize - 1
|
largest_product = -sys.maxsize - 1
|
||||||
substr = n[:13]
|
substr = n[:13]
|
||||||
cur_index = 13
|
cur_index = 13
|
||||||
while cur_index < len(n) - 13:
|
while cur_index < len(n) - 13:
|
||||||
|
@ -72,10 +81,10 @@ def solution(n: str) -> int:
|
||||||
substr = substr[1:] + n[cur_index]
|
substr = substr[1:] + n[cur_index]
|
||||||
cur_index += 1
|
cur_index += 1
|
||||||
else:
|
else:
|
||||||
LargestProduct = max(LargestProduct, streval(substr))
|
largest_product = max(largest_product, str_eval(substr))
|
||||||
substr = n[cur_index : cur_index + 13]
|
substr = n[cur_index : cur_index + 13]
|
||||||
cur_index += 13
|
cur_index += 13
|
||||||
return LargestProduct
|
return largest_product
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user