-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadd-digits.py
More file actions
48 lines (38 loc) · 1.32 KB
/
add-digits.py
File metadata and controls
48 lines (38 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# https://leetcode.com/problems/add-digits/
#
# Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
#
# For example:
#
# Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
# The use of python's inbuilt function map does the trick here
import unittest
class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
my_sum = 0
num = str(num)
while len(num) > 1:
num = map(int , num)
num = sum(num)
num = str(num)
return int(num)
class TestAddDigits(unittest.TestCase):
my_solution = Solution()
def test_for_zero(self):
self.assertEqual(self.my_solution.addDigits(0),0)
def test_single_digit_number(self):
self.assertEqual(self.my_solution.addDigits(7),7)
def test_two_digit_number(self):
self.assertEqual(self.my_solution.addDigits(15),6)
def test_three_digit_number(self):
self.assertEqual(self.my_solution.addDigits(100),1)
def test_three_digit_number(self):
self.assertEqual(self.my_solution.addDigits(111),3)
def test_large_number(self):
self.assertEqual(self.my_solution.addDigits(1111111111),1)
if __name__ == "__main__":
unittest.main()