Abbreviating the Product of a Range LeetCode solution in Python

1
class Solution:
2
def abbreviateProduct(self, left: int, right: int) -> str:
3
c2 = c5 = 0
4
top12 = tail5 = 1
5

6
for i in range(left, right + 1):
7
# count and remove all 2 and 5
8
while i % 2 == 0:
9
i //= 2
10
c2 += 1
11
while i % 5 == 0:
12
i //= 5
13
c5 += 1
14

15
# track top 12 and last 5
16
top12 = int(str(top12 * i)[:12])
17
tail5 = tail5 * i % 100000
18

19
# multiply the remained 2 or 5
20
if c2 > c5:
21
for _ in range(c2 - c5):
22
top12 = int(str(top12 * 2)[:12])
23
tail5 = tail5 * 2 % 100000
24
elif c2 < c5:
25
for _ in range(c5 - c2):
26
top12 = int(str(top12 * 5)[:12])
27
tail5 = tail5 * 5 % 100000
28

29
zero = min(c2, c5)
30

31
# as is included in top 12, it's easy to tell when d<=10
32
if len(str(top12)) <= 10:
33
return str(top12) + "e" + str(zero)
34

35
return (
36
str(top12)[:5]
37
+ "." * 3
38
+ "0" * (5 - len(str(tail5)))
39
+ str(tail5)
40
+ "e"
41
+ str(zero)
42
)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0