473,396 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

conditional expression sought

If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True,
then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without
evaluating any other B_i. This is such a useful mode of expression that I would
like to be able to use something similar even when there is an i with
bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and [B_1])
or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious
reasons. Does anybody know a good way to express this? Any help will be mucho
appreciado.

Peace
Jul 18 '05 #1
11 1495
since True and False
can also evaluate as 1 and 0
you can use the binary operators
| and &
for or and and respectively
class F: .... a = 5
.... def b(self):
.... self.a = 4
.... return True
.... def a(self):
.... self.a = 1
.... return False
.... g = F()
g.a() & g.b() False g.a
4
"Elaine Jackson" <el***************@home.com> wrote in message
news:pRbSb.330334$ts4.37644@pd7tw3no... If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True, then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without evaluating any other B_i. This is such a useful mode of expression that I would like to be able to use something similar even when there is an i with
bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and [B_1]) or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious reasons. Does anybody know a good way to express this? Any help will be mucho appreciado.

Peace

Jul 18 '05 #2
Elaine Jackson wrote:
If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True,
then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without
evaluating any other B_i. This is such a useful mode of expression that I would
like to be able to use something similar even when there is an i with
bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and [B_1])
or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious
reasons. Does anybody know a good way to express this? Any help will be mucho
appreciado.

Why not write a unit test that demonstrates the behavior you want?
It'll then likely be obvious to someone both what your problem is and
what a likely solution is.

Cheers,

// m

Jul 18 '05 #3
Preferably, a program that runs or a screen dump.

Mark McEahern wrote:
Elaine Jackson wrote:
If bool(B_i)==True for 1<=i<=n and j is the smallest i with
bool(A_j)==True,
then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns
B_j without
evaluating any other B_i. This is such a useful mode of expression
that I would
like to be able to use something similar even when there is an i with
bool(B_i)==False. The only thing I can think of by myself is ( (A_1
and [B_1])
or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for
obvious
reasons. Does anybody know a good way to express this? Any help will
be mucho
appreciado.

Why not write a unit test that demonstrates the behavior you want?
It'll then likely be obvious to someone both what your problem is and
what a likely solution is.

Cheers,

// m


Jul 18 '05 #4
"Elaine Jackson" <el***************@home.com> wrote in message news:<pRbSb.330334$ts4.37644@pd7tw3no>...
If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True,
then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without
evaluating any other B_i. This is such a useful mode of expression that I would
like to be able to use something similar even when there is an i with
bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and [B_1])
or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious
reasons. Does anybody know a good way to express this? Any help will be mucho
appreciado.

Peace


Oh, this must be part of your truth table script. Interesting,
looking for something like a fast SOP evaluator, or more like a
function evaluation mechanism? It would probably be most useful to
share your containers for A and B. Are you really going to have
variables named A_1 to A_n, or will you just have a vector A[0:n]?
The vector would probably be easier to deal with. Using integer
representations for your boolean vectors is a good idea, and will
probably buy you enough speed that you won't need a more serious form
of short circuit evaluation, I imagine. Unless your vectors are very
large indeed. Hmm...
Jul 18 '05 #5
On Thu, 29 Jan 2004 17:58:45 GMT in comp.lang.python, "Elaine Jackson"
<el***************@home.com> wrote:
If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True,
then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without
evaluating any other B_i. This is such a useful mode of expression that I would
like to be able to use something similar even when there is an i with
bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and [B_1])
or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious
reasons. Does anybody know a good way to express this? Any help will be mucho
appreciado.

Peace


I'm not sure if this is what you're looking for, but for i > a very
small number, the zip function seems more appropriate:
def get_B_i(A, B): for a, b in zip(A, B):
if a: return b
return A[-1] print get_B_i([False, False, True, False], [0, 1, 2, 3]) 2 print get_B_i([False, False, False, False], [0, 1, 2, 3])

False

This has exactly the same effect provided that A and B are the same
length, otherwise the return value by failure should be adjusted to
whatever suits your purpose.

If you really want to write a conditional expression out in full, I
can't think of anything non-clumsy that would work for all possible
values of B_i, so unfortunately can't help you there.

Dave
Jul 18 '05 #6
Sorry to take so long but I wasn't sure what a "unit test" was (the other guy's
post clarified it). Tell me if this isn't what you're looking for:

falsies=[0,0.0,[],(),{},'',None]
truies=[49,3.14,[1,2,3],(4,5,6),{7:8,9:10},'nonempty']

def demo(A,B):
print "If A is ",A
print "and B is ",B
print "then (A[0] and B[0]) or (A[1] and B[1]) or (A[2] and B[2]) = ",
print (A[0] and B[0]) or (A[1] and B[1]) or (A[2] and B[2])

A=[]
from random import randint
for i in range(3):
A.append(bool(randint(0,1)))
B=truies[0:3]
demo(A,B)

A=[False,False,False]
B=falsies[0:3]
demo(A,B)
print "I would have liked this to be B[2] = ",B[2]
"Mark McEahern" <ma**@mceahern.com> wrote in message
news:ma***************************************@pyt hon.org...
| Elaine Jackson wrote:
|
| >If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True,
| >then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j
without
| >evaluating any other B_i. This is such a useful mode of expression that I
would
| >like to be able to use something similar even when there is an i with
| >bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and
[B_1])
| >or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for
obvious
| >reasons. Does anybody know a good way to express this? Any help will be mucho
| >appreciado.
| >
| Why not write a unit test that demonstrates the behavior you want?
| It'll then likely be obvious to someone both what your problem is and
| what a likely solution is.
|
| Cheers,
|
| // m
|
Jul 18 '05 #7
Elaine,
The last code line:

print "I would have liked this to be B[2] = ",B[2]

prints the value of B[2]; the value you don't want.
I think what you meant was that you want B[2] to be
0.0 not false. bool(0.0) does equal false.
---------------------------------------------------
The code:

A.append(bool(randint(0,1)))

will always yield an A of [true,true,true]
---------------------------------------------------
I didn't know this about python, and I'm not sure I
like it:

wes@linux:~/amy> python
print 1.1 and 2.2 2.2 print 2.2 and 1.1 1.1 print (1.1 and 2.2)

2.2

The order is important. To me, it should be printing true
and not either number or the last number.

Jul 18 '05 #8
This is a good idea. Thanks for pointing it out.

"Dave K" <dk*********@REMOVEhotmail.com> wrote in message
news:86********************************@4ax.com...
| On Thu, 29 Jan 2004 17:58:45 GMT in comp.lang.python, "Elaine Jackson"
| <el***************@home.com> wrote:
|
| >If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True,
| >then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j
without
| >evaluating any other B_i. This is such a useful mode of expression that I
would
| >like to be able to use something similar even when there is an i with
| >bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and
[B_1])
| >or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for
obvious
| >reasons. Does anybody know a good way to express this? Any help will be mucho
| >appreciado.
| >
| >Peace
| >
|
| I'm not sure if this is what you're looking for, but for i > a very
| small number, the zip function seems more appropriate:
|
| >>> def get_B_i(A, B):
| for a, b in zip(A, B):
| if a: return b
| return A[-1]
| >>> print get_B_i([False, False, True, False], [0, 1, 2, 3])
| 2
| >>> print get_B_i([False, False, False, False], [0, 1, 2, 3])
| False
|
| This has exactly the same effect provided that A and B are the same
| length, otherwise the return value by failure should be adjusted to
| whatever suits your purpose.
|
| If you really want to write a conditional expression out in full, I
| can't think of anything non-clumsy that would work for all possible
| values of B_i, so unfortunately can't help you there.
|
| Dave
|
|
Jul 18 '05 #9
This is just for theoretical interest. The A_i's and B_i's were meant as
metavariables.

"Corey Coughlin" <co************@attbi.com> wrote in message
news:a8**************************@posting.google.c om...
| "Elaine Jackson" <el***************@home.com> wrote in message
news:<pRbSb.330334$ts4.37644@pd7tw3no>...
| > If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True,
| > then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j
without
| > evaluating any other B_i. This is such a useful mode of expression that I
would
| > like to be able to use something similar even when there is an i with
| > bool(B_i)==False. The only thing I can think of by myself is ( (A_1 and
[B_1])
| > or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for
obvious
| > reasons. Does anybody know a good way to express this? Any help will be
mucho
| > appreciado.
| >
| > Peace
|
| Oh, this must be part of your truth table script. Interesting,
| looking for something like a fast SOP evaluator, or more like a
| function evaluation mechanism? It would probably be most useful to
| share your containers for A and B. Are you really going to have
| variables named A_1 to A_n, or will you just have a vector A[0:n]?
| The vector would probably be easier to deal with. Using integer
| representations for your boolean vectors is a good idea, and will
| probably buy you enough speed that you won't need a more serious form
| of short circuit evaluation, I imagine. Unless your vectors are very
| large indeed. Hmm...
Jul 18 '05 #10
Thanks for your help. I just looked at some values of random.randint(0,1) in my
interpreter, and one of them was 0. Getting nonboolean values from conjunction
and disjunction allows conditional evaluation of expressions; for example:
reciprocal = lambda x: (x!=0 and 1.0/x) or (x==0 and "undefined").

"wes weston" <ww*****@att.net> wrote in message
news:6P**********************@bgtnsc05-news.ops.worldnet.att.net...
| Elaine,
| The last code line:
|
| print "I would have liked this to be B[2] = ",B[2]
|
| prints the value of B[2]; the value you don't want.
| I think what you meant was that you want B[2] to be
| 0.0 not false. bool(0.0) does equal false.
| ---------------------------------------------------
| The code:
|
| A.append(bool(randint(0,1)))
|
| will always yield an A of [true,true,true]
| ---------------------------------------------------
| I didn't know this about python, and I'm not sure I
| like it:
|
| wes@linux:~/amy> python
|
| >>> print 1.1 and 2.2
| 2.2
| >>> print 2.2 and 1.1
| 1.1
| >>> print (1.1 and 2.2)
| 2.2
|
| The order is important. To me, it should be printing true
| and not either number or the last number.
|
Jul 18 '05 #11
oops;

The code:

A.append(bool(randint(0,1)))

will always yield an A of [true,true,true]
WRONG; was thinking it was a rand float;.
Thanks for replying to all who tried to help.

wes

wes weston wrote:
Elaine,
The last code line:

print "I would have liked this to be B[2] = ",B[2]

prints the value of B[2]; the value you don't want.
I think what you meant was that you want B[2] to be
0.0 not false. bool(0.0) does equal false.
---------------------------------------------------
The code:

A.append(bool(randint(0,1)))

will always yield an A of [true,true,true]
---------------------------------------------------
I didn't know this about python, and I'm not sure I
like it:

wes@linux:~/amy> python
>>> print 1.1 and 2.2 2.2 >>> print 2.2 and 1.1 1.1 >>> print (1.1 and 2.2)

2.2

The order is important. To me, it should be printing true
and not either number or the last number.


Jul 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
23
by: Charles Law | last post by:
Does anyone have a regex pattern to parse HTML from a stream? I have a well structured file, where each line is of the form <sometag someattribute='attr'>text</sometag> for example <SPAN...
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.