From: superpollo on 19 May 2010 15:58 .... 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
From: Jerry Hill on 19 May 2010 16:14 On Wed, May 19, 2010 at 3:58 PM, superpollo <utente(a)esempio.net> wrote: > ... how many positive integers less than n have digits that sum up to m: .... > any suggestion for pythonizin' it? This is how I would do it: def prttn(m, n): """How many positive integers less than n have digits that sum up to m""" total = 0 for testval in range(n): sumofdigits = sum(int(char) for char in str(testval)) if sumofdigits == m: total += 1 return total I added a docstring to the function, saying what it does, and what the arguments are supposed to represent. I also moved the convert-to-string-and-sum-the-digits logic into a single generator expression that's passed to the builtin sum function. Oh, and I tried to use slightly more expressive variable names. -- Jerry
From: René 'Necoro' Neumann on 19 May 2010 16:24 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é
From: superpollo on 19 May 2010 16:25 Jerry Hill ha scritto: > On Wed, May 19, 2010 at 3:58 PM, superpollo <utente(a)esempio.net> wrote: >> ... how many positive integers less than n have digits that sum up to m: > ... >> any suggestion for pythonizin' it? > > This is how I would do it: > > def prttn(m, n): > """How many positive integers less than n have digits that sum up to m""" > total = 0 > for testval in range(n): > sumofdigits = sum(int(char) for char in str(testval)) this line gives me this: TypeError: 'int' object is not callable is it some new feature in >2.5 ? > if sumofdigits == m: > total += 1 > return total > > I added a docstring to the function, saying what it does, and what the > arguments are supposed to represent. I also moved the > convert-to-string-and-sum-the-digits logic into a single generator > expression that's passed to the builtin sum function. Oh, and I tried > to use slightly more expressive variable names. >
From: superpollo on 19 May 2010 16:30
René 'Necoro' Neumann ha scritto: > 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) TypeError: 'int' object is not callable on 2.5.4 > > A small oneliner :) > > (I first had "return len([x for x in ...])" but the above avoids > creating an intermediate list) > > - René > |