tree functions daily exercise: Table

Duncan Booth duncan.booth at invalid.invalid
Tue Jun 21 09:39:54 EDT 2005


Xah Lee wrote:

> '''Table(f,[iStart,iEnd,iStep]) returns a list of f applied to the
> range range(iStart,iEnd,iStep).  Example: Table(f,[3,10,2]) returns
> [f(3),f(5),f(7),f(9)] Table(f,[iStart,iEnd,iStep],
> [jStart,jEnd,jStep], ...) returns a nested list of f(i,j,...) applied
> thru the iterators.  Example: Table(f,[1,3,1],[2,6,2]) returns
> [[f(1,2),f(1,4),f(1,6)],[f(2,2),f(2,4),f(2,6)]]'''

How does it know when to skip the end value (i.e. act as for the rest of 
Python) and when to include it? Or did you mean:

Table(f,[1,3,1],[2,7,2]) returns 
[[f(1,2),f(1,4),f(1,6)],[f(2,2),f(2,4),f(2,6)]]

Wouldn't it be more sensible just to take the iterators directly as 
arguments, so for this example you would do:

Table(f, range(1,3), range(2,7,2))

That way you have more flexibility (e.g. to do out-of-order ranges such as 
[1,3,2,0,4]), and the code for Table is much simpler since it just has to 
manipulate the lists it was given.



More information about the Python-list mailing list