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:
Lukas Esc 2022-10-30 11:56:54 +01:00 committed by GitHub
parent 9278d0c6cd
commit cafbbab125
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)