<div class="gmail_quote">On 12 September 2012 14:25, Libra <span dir="ltr"><<a href="mailto:librarama@gmail.com" target="_blank">librarama@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Wednesday, September 12, 2012 3:11:42 PM UTC+2, Steven D'Aprano wrote:<br>
> On Wed, 12 Sep 2012 05:48:09 -0700, Libra wrote:<br>
<br>
</div><div class="im">> > I need to implement a function that returns 1 only if all the values in<br>
> > a list satisfy given constraints (at least one constraint for each<br>
> > element in the list), and zero otherwise.<br>
><br>
> What are the restrictions on the constraints themselves?<br>
> Could they be arbitrarily complicated?<br>
> "Item 2 must be an even number divisible by 17 and 39 with at least eight<br>
> digits but no greater than four million, unless today is Tuesday, in<br>
> which case it must be equal to six exactly."<br>
<br>
</div>Generally the constraints are quite simple, like the one in my example. But I can also have 2 or more constraints for each value:<br>
L[0] >= 1<br>
L[0] <= 5<br></blockquote><div><br></div><div>You can use:</div><div> lambda x: 1 <= x and x <= 5</div><div>or</div><div> lambda x: 1 <= x <= 5</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
To complicate a little, what about constraints like:<br>
L[0] + L[2] >= 3</blockquote><div><br></div><div>You could rewrite all your constraints as functions on the sequence of values:</div><div> lambda y: 1 <= y[0] <= 5</div><div> lambda y: y[0] + y[2] >= 3</div>
<div><br></div><div>If all of your constraints are linear (like all of the ones you have shown) then you can represent each one as a set of coefficients for a linear projection of the list combined with a threshold value (if this last point doesn't make sense then just ignore it).</div>
<div><br></div><div>Oscar</div></div>