Clarity vs. code reuse/generality

Pablo Torres N. tn.pablo at gmail.com
Sat Jul 4 01:40:03 CEST 2009


On Fri, Jul 3, 2009 at 09:05, kj<no.email at please.post> wrote:
> The context is the concept of a binary search.  In one of their
> homeworks, my students will have two occasions to use a binary
> search.  This seemed like a perfect opportunity to illustrate the
> idea of abstracting commonalities of code into a re-usable function.
> So I thought that I'd code a helper function, called _binary_search,
> that took five parameters: a lower limit, an upper limit, a
> one-parameter function, a target value, and a tolerance (epsilon).
> It returns the value of the parameter for which the value of the
> passed function is within the tolerance of the target value.

Five params for a novice function is overkill.  I'd drop the epsilon
thing and, assuming you are searching in a list, tuple or something
similar, make the lower limit default to the first index and the upper
default to the last one.

In fact, I'd let them to realize that a function is convenient, and
base some of the grading in whether they wrote it or not.  Just a
thought.

-- 
Pablo Torres N.



More information about the Python-list mailing list