Skip to content

Commit 77bab35

Browse files
archaengelstokhos
authored andcommitted
Add style improvements to Project Euler problem 8 (TheAlgorithms#3001)
1 parent 8a324e5 commit 77bab35

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

Diff for: project_euler/problem_08/sol1.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 8: https://projecteuler.net/problem=8
3+
24
The four adjacent digits in the 1000-digit number that have the greatest
35
product are 9 × 9 × 8 × 9 = 5832.
46
@@ -50,21 +52,21 @@
5052
71636269561882670428252483600823257530420752963450"""
5153

5254

53-
def solution(n):
55+
def solution(n: str = N) -> int:
5456
"""Find the thirteen adjacent digits in the 1000-digit number n that have
5557
the greatest product and returns it.
5658
5759
>>> solution(N)
5860
23514624000
5961
"""
60-
LargestProduct = -sys.maxsize - 1
62+
largest_product = -sys.maxsize - 1
6163
for i in range(len(n) - 12):
6264
product = 1
6365
for j in range(13):
6466
product *= int(n[i + j])
65-
if product > LargestProduct:
66-
LargestProduct = product
67-
return LargestProduct
67+
if product > largest_product:
68+
largest_product = product
69+
return largest_product
6870

6971

7072
if __name__ == "__main__":

Diff for: project_euler/problem_08/sol2.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 8: https://projecteuler.net/problem=8
3+
24
The four adjacent digits in the 1000-digit number that have the greatest
35
product are 9 × 9 × 8 × 9 = 5832.
46
@@ -53,7 +55,7 @@
5355
)
5456

5557

56-
def solution(n):
58+
def solution(n: str = N) -> int:
5759
"""Find the thirteen adjacent digits in the 1000-digit number n that have
5860
the greatest product and returns it.
5961

Diff for: project_euler/problem_08/sol3.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 8: https://projecteuler.net/problem=8
3+
24
The four adjacent digits in the 1000-digit number that have the greatest
35
product are 9 × 9 × 8 × 9 = 5832.
46
@@ -50,32 +52,39 @@
5052
71636269561882670428252483600823257530420752963450"""
5153

5254

53-
def streval(s: str) -> int:
54-
ret = 1
55-
for it in s:
56-
ret *= int(it)
57-
return ret
55+
def str_eval(s: str) -> int:
56+
"""Returns product of digits in given string n
57+
58+
>>> str_eval("987654321")
59+
362880
60+
>>> str_eval("22222222")
61+
256
62+
"""
63+
product = 1
64+
for digit in s:
65+
product *= int(digit)
66+
return product
5867

5968

60-
def solution(n: str) -> int:
69+
def solution(n: str = N) -> int:
6170
"""Find the thirteen adjacent digits in the 1000-digit number n that have
6271
the greatest product and returns it.
6372
6473
>>> solution(N)
6574
23514624000
6675
"""
67-
LargestProduct = -sys.maxsize - 1
76+
largest_product = -sys.maxsize - 1
6877
substr = n[:13]
6978
cur_index = 13
7079
while cur_index < len(n) - 13:
7180
if int(n[cur_index]) >= int(substr[0]):
7281
substr = substr[1:] + n[cur_index]
7382
cur_index += 1
7483
else:
75-
LargestProduct = max(LargestProduct, streval(substr))
84+
largest_product = max(largest_product, str_eval(substr))
7685
substr = n[cur_index : cur_index + 13]
7786
cur_index += 13
78-
return LargestProduct
87+
return largest_product
7988

8089

8190
if __name__ == "__main__":

0 commit comments

Comments
 (0)