Hej there,

> I don't know if everyone would consider this more elegant but it's
> certainly shorter:

Thanks!

>>>> def DigitSum(YourNumber):
> ...     return sum(map(int, YourNumber))
> ...
>>>> DigitSum('55')
> 10

I don't understand yet what the "map" function does - can you explain?
I read the Python 3.3.0 documentation on that topic but I frankly
didn't really understand it
http://docs.python.org/3/library/functions.html#map. My search for
simple code examples using "map" wasn't really fruitful either.

Other than that, I went through each of the 7 alternatives to my
"Integer to Digit Sum" program you guys sent me, I understand what
each of those programs does. My favorite one is:

def DigSum (integer):
    s = 0
    while integer != 0:
        integer, remainder = divmod(integer, 10)
        s += remainder
    print(s)

DigSum(537)

It's really a beautiful, elegant solution IMHO and way better than my
original code (convert int to string, store each digit in an empty
list, then convert them back to int and sum them).

Again, thank you all!

Raf
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to