looping through possible combinations of McNuggets packs of 6, 9 and 20

John Posner jjposner at optimum.net
Thu Aug 12 22:26:20 EDT 2010


On 8/12/2010 6:31 PM, News123 wrote:

>>      candidate_box_counts = product(
>>          xrange(target/box_sizes[0] + 1),
>>          xrange(target/box_sizes[1] + 1),
>>          xrange(target/box_sizes[2] + 1),
>>      )
>
> Couldn't this be rewritten as:
>      candidate_box_counts = product(
> 	 * [ xrange(target/sizes + 1) for size in box_sizes ]
>       )
>

Sure (except for the typo: "sizes" should be "size").

You could even use a generator expression instead of a list comprehension:

       candidate_box_counts = product(
  	 * ( xrange(target/size + 1) for size in box_sizes )
        )

-John



More information about the Python-list mailing list