Skip to content

Commit 46ea830

Browse files
Merge pull request #3071 from vedikaagarwal28/fixing-sum_of_digits_of_a_number.py
updated sum_of_digits_of_a_number to include negative numbers
2 parents 8221f73 + be7fa3e commit 46ea830

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

sum_of_digits_of_a_number.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
0
1414
>>> sum_of_digits(999)
1515
27
16+
>>> sum_of_digits(-123)
17+
6
1618
"""
1719

1820
import sys
@@ -49,8 +51,8 @@ def sum_of_digits(n: int) -> int:
4951
Compute the sum of the digits of an integer.
5052
5153
Args:
52-
n:Non-negative integer
53-
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.
5456
5557
Returns:
5658
Sum of digits of the number.
@@ -60,8 +62,10 @@ def sum_of_digits(n: int) -> int:
6062
6
6163
>>> sum_of_digits(405)
6264
9
65+
>>> sum_of_digits(-789)
66+
24
6367
"""
64-
n=abs(n)
68+
n = abs(n) # FIX: handle negative numbers
6569
total = 0
6670
while n > 0:
6771
# Add last digit and remove it from n

0 commit comments

Comments
 (0)