Removing Recursion

Ignacio Vazquez-Abrams ignacio at openservices.net
Sun Sep 23 22:39:45 EDT 2001


On Mon, 24 Sep 2001, Duncan Smith wrote:

>          I need to make the following code more efficient, but I am
> struggling to find a decent way of removing the recursive calls.  So, anyone
> any suggestions (on any ways of speeding this up)?  'bins(self)' constructs
> a list containing the numbers of ways of packing 0 to n = k*(N-k) items into
> k bins where each bin has maximum capacity N-k.  So I'd also be grateful if
> anyone knows of a better approach to this general problem.  Thanks in
> advance.

The following code is untested, but it's faster. It works by going to every
k'th item starting at 0 and putting the items in that set of k in each bin[].

---
bins=[[]]*k
filter(lambda x, bins=bins, binitems=binitems: map(lambda y, x=x, bins=bins, binitems=binitems: bins[y].append(binitems[x*(N-k)+y]), xrange(0, k)), xrange(0, k*(N-k), k)
---

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>





More information about the Python-list mailing list