File tree 3 files changed +28
-15
lines changed
3 files changed +28
-15
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
+ Problem 8: https://projecteuler.net/problem=8
3
+
2
4
The four adjacent digits in the 1000-digit number that have the greatest
3
5
product are 9 × 9 × 8 × 9 = 5832.
4
6
50
52
71636269561882670428252483600823257530420752963450"""
51
53
52
54
53
- def solution (n ) :
55
+ def solution (n : str = N ) -> int :
54
56
"""Find the thirteen adjacent digits in the 1000-digit number n that have
55
57
the greatest product and returns it.
56
58
57
59
>>> solution(N)
58
60
23514624000
59
61
"""
60
- LargestProduct = - sys .maxsize - 1
62
+ largest_product = - sys .maxsize - 1
61
63
for i in range (len (n ) - 12 ):
62
64
product = 1
63
65
for j in range (13 ):
64
66
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
68
70
69
71
70
72
if __name__ == "__main__" :
Original file line number Diff line number Diff line change 1
1
"""
2
+ Problem 8: https://projecteuler.net/problem=8
3
+
2
4
The four adjacent digits in the 1000-digit number that have the greatest
3
5
product are 9 × 9 × 8 × 9 = 5832.
4
6
53
55
)
54
56
55
57
56
- def solution (n ) :
58
+ def solution (n : str = N ) -> int :
57
59
"""Find the thirteen adjacent digits in the 1000-digit number n that have
58
60
the greatest product and returns it.
59
61
Original file line number Diff line number Diff line change 1
1
"""
2
+ Problem 8: https://projecteuler.net/problem=8
3
+
2
4
The four adjacent digits in the 1000-digit number that have the greatest
3
5
product are 9 × 9 × 8 × 9 = 5832.
4
6
50
52
71636269561882670428252483600823257530420752963450"""
51
53
52
54
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
58
67
59
68
60
- def solution (n : str ) -> int :
69
+ def solution (n : str = N ) -> int :
61
70
"""Find the thirteen adjacent digits in the 1000-digit number n that have
62
71
the greatest product and returns it.
63
72
64
73
>>> solution(N)
65
74
23514624000
66
75
"""
67
- LargestProduct = - sys .maxsize - 1
76
+ largest_product = - sys .maxsize - 1
68
77
substr = n [:13 ]
69
78
cur_index = 13
70
79
while cur_index < len (n ) - 13 :
71
80
if int (n [cur_index ]) >= int (substr [0 ]):
72
81
substr = substr [1 :] + n [cur_index ]
73
82
cur_index += 1
74
83
else :
75
- LargestProduct = max (LargestProduct , streval (substr ))
84
+ largest_product = max (largest_product , str_eval (substr ))
76
85
substr = n [cur_index : cur_index + 13 ]
77
86
cur_index += 13
78
- return LargestProduct
87
+ return largest_product
79
88
80
89
81
90
if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments