Boolean function on variable-length lists
Libra
librarama at gmail.com
Wed Sep 12 08:48:09 EDT 2012
Hello,
I need to implement a function that returns 1 only if all the values in a list satisfy given constraints (at least one constraint for each element in the list), and zero otherwise.
For example, I may have a list L = [1, 2, 3, 4] and the following constraints:
L[0] >= 1
L[1] <= 3
L[2] == 2
L[3] >= 3
In this case, the function returns 0 because the third constraint is not satisfied.
With fixed-length lists, I can sometimes use a very naive approach and hard-code the constraints combined with AND. Nonetheless, the problems are:
1) even with fixed-length lists, the hard-code approach requires a lot of effort (especially with long lists) and is prone to error;
2) the constraints may change, so with a hard-code approach the effort grows exponentially;
3) I need to work on variable-length lists (generally, lists of numbers).
I can't figure out anything useful.
Could you please suggest me a suitable ways?
Thanks
Libra
More information about the Python-list
mailing list