function that counts...

superpollo utente at esempio.net
Tue May 25 07:28:22 EDT 2010


Jean-Michel Pichavant ha scritto:
> superpollo wrote:
>> Jean-Michel Pichavant ha scritto:
>>> Jerry Hill wrote:
>>>> On Wed, May 19, 2010 at 3:58 PM, superpollo <utente at 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.
>>>>
>>>>   
>>> my favorite solutio nso far.
>>>
>>> @ OP
>>>
>>> What means prttn ? 
>>
>> i already answered this downthreads...
>>
>>> something ... I don't find the word, something like un-intuitive. 
>>> Sounds like homework.
>>
>> it is not.
> My apologies then, for both statements.
> I still don't see "how many positive integers less than n have digits 
> that sum up to m" makes it a "partition" though if that what prttn 
> means. Surely because I miss the context.
> 
> JM

ok, this is the mistery. it was inspired by a question on e.c.m.:

http://groups.google.it/group/es.ciencia.matematicas/msg/f8f09672bd8a052a

the first question is (somewhat) an instance of:

http://en.wikipedia.org/wiki/Partition_(number_theory)

bye



More information about the Python-list mailing list