Source Code: class Solution: def getLucky(self, s: str, k: int) -> int: ''' To solve this problem, I'm first going to convert all the characters in the string to its numerical equivalent, and call that resulting string "convert". Afterwards, with this string that we calculated, run a loop for k number of times, and for each iteration, we take the integer version of each character of the convert string,add them together, and set that as the new convert string, and we repeat this process :D ''' convert = "".join(str(ord(c)-ord('a')+1) for c in list(s)) res = 0 for i in range(k): res = sum([int(c) for c in convert]) convert = str(res) return int(convert)
Source Code:
class Solution:
def getLucky(self, s: str, k: int) -> int:
'''
To solve this problem, I'm first going to convert
all the characters in the string to its numerical equivalent,
and call that resulting string "convert". Afterwards, with this
string that we calculated, run a loop for k number of times,
and for each iteration, we take the integer version of each character
of the convert string,add them together, and set that as the new
convert string, and we repeat this process :D
'''
convert = "".join(str(ord(c)-ord('a')+1) for c in list(s))
res = 0
for i in range(k):
res = sum([int(c) for c in convert])
convert = str(res)
return int(convert)