Provided digit sum solution is confusing


The solution given on the website is completely different and confusing

How can I break down these problems into easier parts?

yes, agree. That solution might be more “efficient” (only loop and math operations, no type casting) i can imaging it confuses new learners.


It wasn’t always the solution given, but somewhere along the line somebody thought it was the most clever one and adopted it for the exercise. Why? Who can know? Whomever made that change is probably long gone by now, and this situation will carry on unchanged going forward.

Use the niave solution that iterates over a string, character by character, adding the int() of that character to the total. Return the total.

for x in str(n):
    total += int(x)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.