Boolean function on variable-length lists
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Boolean function on variable-length lists
On Wednesday, September 12, 2012 3:02:44 PM UTC+2, Jussi Piitulainen wrote:
> So you would associate each constraint with an index. You could
> maintain a list of constraints and apply it to the values as follows:
Yes, even though there could be more constraints for each value in the list (at
least 1 constraint for each value)
>
> >>> cs = [ lambda x : x >= 1, lambda x : x <= 3, lambda x : x == 2,
>
> ...lambda x : x >= 3 ]
>
> >>> { f(x) for f, x in zip(cs, [1,2,3,4]) }
Just to understand, with f(x) you are defining a function f with argument x,
right? I didn't know it was possible to define functions in this way. Is this a
case of anonymous function?
> {False, True}
Actually, I don't understand the output. Why it is both False and True?
> >>> { f(x) for f, x in zip(cs, [1,2,2,4]) }
> {True}
Ok.
Thank you very much
--
http://mail.python.org/mailman/listinfo/python-list
Re: Boolean function on variable-length lists
On Wednesday, September 12, 2012 3:11:42 PM UTC+2, Steven D'Aprano wrote: > On Wed, 12 Sep 2012 05:48:09 -0700, Libra wrote: > > 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. > > What are the restrictions on the constraints themselves? > Could they be arbitrarily complicated? > "Item 2 must be an even number divisible by 17 and 39 with at least eight > digits but no greater than four million, unless today is Tuesday, in > which case it must be equal to six exactly." Generally the constraints are quite simple, like the one in my example. But I can also have 2 or more constraints for each value: L[0] >= 1 L[0] <= 5 To complicate a little, what about constraints like: L[0] + L[2] >= 3 Thanks > -- > > Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Boolean function on variable-length lists
On Wednesday, September 12, 2012 3:19:28 PM UTC+2, Libra wrote:
> > {False, True}
> Actually, I don't understand the output. Why it is both False and True?
Ok, I have understood now, I didn't noticed it was a set and not a list.
Regards
--
http://mail.python.org/mailman/listinfo/python-list
Graph editor
Hi, I would like to build a simple graph editor, allowing me to add nodes and edges via the mouse, along with the possibility to edit it (delete/ move nodes and edges, double click on object to pop-up a form where I can insert object related info, and so forth) and bind edges to nodes (that is, when I move a node, the attached edges move accordingly). I should also put an image (a bitmap of png) in the background of the graph. I've seen many GUI libraries (from TkInter to wxPython) but I was wondering if there is a specific graphic library (with good documentation and maybe examples) simplifying the rapid development of such an application. Thanks Libra -- http://mail.python.org/mailman/listinfo/python-list
