[Tutor] cross sum toying

Gregor Lingl glingl at aon.at
Fri Oct 10 10:14:18 EDT 2003



Raymond Hettinger schrieb:

>Here is one more version to play with:
>
>  
>
>>>>from itertools import imap
>>>>def cross_sum(n, codezero=ord('0')):
>>>>        
>>>>
>	s = str(n)
>	return sum(imap(ord, s)) - len(s) * codezero
>  
>
Nice idea! I played with it, as you suggested and, interestingly, found
out that it is exactly as fast as:

def cross_sum7(n):
    s=str(n)
    return sum(map(ord, s)) - ord("0")*len(s)

So why use itertools in this case?

Regards, Gregor

>  
>




More information about the Tutor mailing list