function that counts...
superpollo
utente at esempio.net
Thu May 20 10:38:55 EDT 2010
Steven D'Aprano ha scritto:
> On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote:
>
>> ... how many positive integers less than n have digits that sum up to m:
>>
>> In [197]: def prttn(m, n):
>
> Does the name "prttn" mean anything? I'm afraid I keep reading it as a
> mispelling of "print n".
pArtItIOn
>
>
> [...]
>> s = str(i)
>> sum = 0
>> for j in range(len(s)):
>> sum += int(s[j])
>
> Rather than iterating over an index j = 0, 1, 2, ... and then fetching
> the jth character of the string, you can iterate over the characters
> directly. So the inner loop is better written:
>
> for c in s:
> sum += int(c)
d'oh! some day i will learn to speak pythonese!
thanks
bye
More information about the Python-list
mailing list