aboutsummaryrefslogtreecommitdiff
path: root/sum_digit_str_convert.py
diff options
context:
space:
mode:
Diffstat (limited to 'sum_digit_str_convert.py')
-rw-r--r--sum_digit_str_convert.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/sum_digit_str_convert.py b/sum_digit_str_convert.py
new file mode 100644
index 0000000..3980972
--- /dev/null
+++ b/sum_digit_str_convert.py
@@ -0,0 +1,30 @@
+# 1945. Sum of Digits of String After Convert
+
+"""
+you are given a string 's' consisting of lowercase english letters, and an
+integer 'k'. first, convert 's' into an integer by replacing each letter with
+its position in the alphabet (ie. replace 'a' with 1, 'b' with 2, and so on).
+then, transform the integer by replacing it with the sum of its digits.
+repeat the transform operation 'k' times in total. return the resulting
+integer after performing the operations described above
+"""
+
+
+class Solution(object):
+ def getLucky(self, s, k):
+ """
+ :type s: str
+ :type k: int
+ :rtype: int
+ """
+ s = "".join(str(ord(i) - ord("a") + 1) for i in s)
+ for _ in range(k):
+ s = str(sum(int(i) for i in s))
+ return int(s)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.getLucky(s="iiii", k=1))
+ print(obj.getLucky(s="leetcode", k=2))
+ print(obj.getLucky(s="zbax", k=2))