Skip to content

Commit 3e6280d

Browse files
authored
Create sum-of-digits-of-string-after-convert.py
1 parent ace95be commit 3e6280d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n + logn + log(logn) + ...) = O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def getLucky(self, s, k):
6+
"""
7+
:type s: str
8+
:type k: int
9+
:rtype: int
10+
"""
11+
total = reduce(lambda total, x: total+sum(divmod((ord(x)-ord('a')+1), 10)), s, 0)
12+
while k > 1 and total > 9:
13+
new_total = 0
14+
while total:
15+
total, x = divmod(total, 10)
16+
new_total += x
17+
total = new_total
18+
k -= 1
19+
return total

0 commit comments

Comments
 (0)