mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
shortened code using abs() and inplace ops (#7191)
n = -n if n < 0 else n --> n = abs(n) n = n // 10 --> n //= 10
This commit is contained in:
parent
9278d0c6cd
commit
cafbbab125
|
@ -14,11 +14,11 @@ def sum_of_digits(n: int) -> int:
|
|||
>>> sum_of_digits(0)
|
||||
0
|
||||
"""
|
||||
n = -n if n < 0 else n
|
||||
n = abs(n)
|
||||
res = 0
|
||||
while n > 0:
|
||||
res += n % 10
|
||||
n = n // 10
|
||||
n //= 10
|
||||
return res
|
||||
|
||||
|
||||
|
@ -35,7 +35,7 @@ def sum_of_digits_recursion(n: int) -> int:
|
|||
>>> sum_of_digits_recursion(0)
|
||||
0
|
||||
"""
|
||||
n = -n if n < 0 else n
|
||||
n = abs(n)
|
||||
return n if n < 10 else n % 10 + sum_of_digits(n // 10)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user