[Tutor] Converting integers into digit sum (Python 3.3.0)

spir denis.spir at gmail.com
Mon Dec 9 17:00:49 CET 2013


On 12/09/2013 03:49 PM, Alan Gauld wrote:
> On 09/12/13 13:48, spir wrote:
>> On 12/09/2013 02:29 PM, Wolfgang Maier wrote:
>>> spir <denis.spir <at> gmail.com> writes:
>
>>>> Tu sum it up (aha!): you algorithm is the right and only one
>>>
>>> No, it's not the only one. ...
>>> also the pure numbers approach pointed out by me and Alan.
>>
>> You are right in a sense, but this is what int() does, isn't it?
>
> No. int() can be done in several ways but usually it's based on taking the
> character code and adding/subtracting some base value. (or uses a lookup table).
> But to get the individual characters for conversion
> it will loop over a string so in that respect its similar to the string based
> approaches. But the OP didn't start with a string, he started with an int.
>
>> another sense I thought it is still the same algo,
>
> The string based algorithms are effectively all the same whether
> using loops, comprehensions or map.
>
> But the numerical solution is fundamentally different since it never looks at
> the characters it relies on math to generate the digits.
>
> Neither is better than the other; they both work reasonably efficiently.
> But they are fundamentally different algorithms.

All right; I thought int numeral decoding was always more or less the same algo 
(for having studied and implemented several versions), using integer division 
remainders, but obviously ws wrong. Thank you for the correction, Alan.

Denis


More information about the Tutor mailing list