MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / sum_of_digits_compact

Function sum_of_digits_compact

maths/sum_of_digits.py:37–49  ·  view source on GitHub ↗

Find the sum of digits of a number >>> sum_of_digits_compact(12345) 15 >>> sum_of_digits_compact(123) 6 >>> sum_of_digits_compact(-123) 6 >>> sum_of_digits_compact(0) 0

(n: int)

Source from the content-addressed store, hash-verified

35
36
37def sum_of_digits_compact(n: int) -> int:
38 """
39 Find the sum of digits of a number
40 >>> sum_of_digits_compact(12345)
41 15
42 >>> sum_of_digits_compact(123)
43 6
44 >>> sum_of_digits_compact(-123)
45 6
46 >>> sum_of_digits_compact(0)
47 0
48 """
49 return sum(int(c) for c in str(abs(n)))
50
51
52def benchmark() -> None:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected