Different approach to digit summing
I am quite proud of my solution to digit_sum(x)
def digit_sum(x):
numString = str(x)
sum=0;
for digit in numString:
if digit.isdigit():
sum += int(digit)
return sum
This turns the number into a string, and then you can iterate through the string and convert each letter back to a number and sum. It also works if there is a minus sign or decimal because it uses python’s built-in function .isdigit() to check if the character is in fact a digit
Answer 5097dfb45a49da0200000ad7
Have you seen this thread? There are some very neat solutions for this task. My favourite, because it’s so short and so “Pythony”:
return sum( map( int, list( str(x) ) ) )
This one doesn’t do checking, of course. Your solution has the benefit of accepting both numbers and strings (and the strings can even contain non-digits). Nice!
3 comments
I’m intrigued. I am willing to submit to the reality that this solution will make sense to me as I keep studying, but, if you could point me in the right direction, I’d appreciate it: what is this map()?
OMG! So smart! BTW, is your solution more efficient to run? I usually avoid this style if possible, because I bet I can’t read it in next morning (I’m a noob!). But I would force myself to use this approach if it is more efficient :)
Very clever and a kind of coding style I really enjoy. However: from my–admittedly limited–understanding, this couldn’t be any less “Pythony”. If you enter “import this” into the interpreter, you’ll see that this code violates several of the “Zen of Python” precepts, such as: “
- Flat is better than nested.
- Sparse is better than dense.
- Readability counts. “ …and arguably a few more. I like dense code that’s challenging to parse; I just don’t think the Python community, as a rule, does. Now, old-school C programmers….
Happy coding!
Answer 50a3ab5a76b0f32f25003fb0
I had the same approach, but didn’t think of using isdigit(), well done. I couldn’t think of an easy way to do it as an integer because without a len() how could the for loop be executed. Then I thought of using a while but it just seemed even more complex.
Answer 51c55ee49c4e9d4105009c2e
Answer 52d59375282ae3a99c000260
def digit_sum(n):
n = str(n)
total_count = 0
for i in range(len(n)):
total_count += int(n[i])
return total_count
Answer 54f0552e86f552e543001775
I came up with:
def digit_sum(n):
total = 0
for i in str(n):
total += int(i)
return total
Answer 509b5dc6da4f7d0200000513
I was gonna write about the same kind of solution, which also came natural to me.
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons