function that counts...

superpollo utente at esempio.net
Wed May 19 15:58:04 EDT 2010


... how many positive integers less than n have digits that sum up to m:

In [197]: def prttn(m, n):
     tot = 0
     for i in range(n):
         s = str(i)
         sum = 0
         for j in range(len(s)):
             sum += int(s[j])
         if sum == m:
             tot += 1
     return tot
    .....:

In [207]: prttn(25, 10000)
Out[207]: 348

any suggestion for pythonizin' it?

bye



More information about the Python-list mailing list