[Tutor] Setting thresholds in a compact way

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jan 1 20:07:42 EST 2020


On 01/01/2020 22:27, boB Stepp wrote:

> Is there any sensible/better thing to do if one is stuck with a long
> sequence of "if" conditions?  

Richard already suggested a sorted list of tuples, which is
usually the simplest option, although that carries the overhead
of a loop to test the list entries.

The other option is to use a sparse array where the values
are the functions to be called and the test values are the
indices into the array. (multiple indices potentially mapping
to the same code.)
However that requires that you can reduce your test values
to some kind of quantum value (possibly by multiplying
the float by 10,100 etc and then using rounding)

You can use a statically created array or build a class
that generates the required array from a set of input values.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list