Am 19.05.2010 21:58, schrieb superpollo: > ... 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
An idea would be: >>> def prttn(m, n): ... return sum(1 for x in range(n) if sum(map(int, str(x))) == m) A small oneliner :) (I first had "return len([x for x in ...])" but the above avoids creating an intermediate list) - René
signature.asc
Description: OpenPGP digital signature
-- http://mail.python.org/mailman/listinfo/python-list