We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 8221f73 + be7fa3e commit 46ea830Copy full SHA for 46ea830
1 file changed
sum_of_digits_of_a_number.py
@@ -13,6 +13,8 @@
13
0
14
>>> sum_of_digits(999)
15
27
16
+ >>> sum_of_digits(-123)
17
+ 6
18
"""
19
20
import sys
@@ -49,8 +51,8 @@ def sum_of_digits(n: int) -> int:
49
51
Compute the sum of the digits of an integer.
50
52
53
Args:
- n:Non-negative integer
- If the integer is negative , it is converted to postive interger and assigned to same number
54
+ n: Non-negative integer.
55
+ If the integer is negative, it is converted to positive before computing the sum.
56
57
Returns:
58
Sum of digits of the number.
@@ -60,8 +62,10 @@ def sum_of_digits(n: int) -> int:
60
62
6
61
63
>>> sum_of_digits(405)
64
9
65
+ >>> sum_of_digits(-789)
66
+ 24
67
- n=abs(n)
68
+ n = abs(n) # FIX: handle negative numbers
69
total = 0
70
while n > 0:
71
# Add last digit and remove it from n
0 commit comments