From cafbbab125ebcdac4294f4cbda024b840d230b9a Mon Sep 17 00:00:00 2001 From: Lukas Esc <55601315+Luk-ESC@users.noreply.github.com> Date: Sun, 30 Oct 2022 11:56:54 +0100 Subject: [PATCH] shortened code using abs() and inplace ops (#7191) n = -n if n < 0 else n --> n = abs(n) n = n // 10 --> n //= 10 --- maths/sum_of_digits.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/sum_of_digits.py b/maths/sum_of_digits.py index 64da00d46..5ad5fe6c9 100644 --- a/maths/sum_of_digits.py +++ b/maths/sum_of_digits.py @@ -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)