Mathematically Impossible


Recommended Posts

I feel like I am going crazy. I do not see why this script is behaving the way it is.

def winner():

   pool = [1,2,5,6]

   winner = ( (1,2,3), (4,5,6), (7,8,9), (1,4,7), (2,5,8),
            (3,6,9), (1,5,9), (3,5,7) )


   for i in winner:
       if (i[0] and i[1] and i[2]) in pool:
           print i

winner()

here is what I think this script should be doing. it should check every element in the tuple called winner; if all three numbers that comprise the element are in the list called pool, it will print the element from the tuple winner.

here is where it is seeming to go wrong. it is printing the element (4,5,6). The numbers 4,5, and 6 are not in the list pool. How could this be possible.

edit added later//

my if statement is saying: if the numbers 4,5, and 6 are in the list pool then print 4,5,6

the numbers are not in the list pool, so how could it be printing them?

Edited by shanenin
Link to post
Share on other sites

I needed to do the line like this

if i[0] in pool and i[1] in pool and i[2] in pool:

the way I was doing it was like saying

if 5 in pool:     #i[2] = 5

it seems to only except the last elemnt in the argument

Link to post
Share on other sites

The description of the 'and' operator reads

"The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned."

if i[0] and i[1] and i[2] in pool:

is equivalent to

 if ((i[0] and i[1]) and i[2]) in pool:

every element of i is true, so this reduces like so

if ((i[0] and i[1]) and i[2]) in pool:
--> if (i[1] and i[2]) in pool:
--> if i[2] in pool:

Edited by jcl
Link to post
Share on other sites

You're far from the only person who's wanted that sort of operator chaining to work. There are a geat many people who really want

a < b < c

to mean

(a < b) and (b < c)

especially in numerical or otherwise math-oriented code, but there are very few languages in which it does. The only one that comes to mind is Perl 6. (The Lisp family supports something equivalent, but not with that syntax.)

It's a bit of a sensitive subject in programming language design.

[Edit: Really bad typo. Though 'operator changing' is accurate, since the meaning of the operators would change depending on the surround context. 'a < b' alone would produce in boolean result but 'a < b' in 'a < b < c' would produce... something odd. The oddness being part of the reason chaining isn't support.]

Edited by jcl
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...