PDA

View Full Version : [Python] Shorten Conditions



regomodo
June 11th, 2008, 05:06 PM
`

nvteighen
June 11th, 2008, 05:34 PM
The condition, in a shorter notation, is:

if ((a == p) && (a == q))

So, you can reduce it to if(p == q), as it's the only way that "a" can meet both conditions simultaneously.

imdano
June 11th, 2008, 05:35 PM
What if (p == q) but (p != a)?

nvteighen
June 11th, 2008, 05:37 PM
What if (p == q) but (p != a)?
Then (a != q) also, and the if-block is not executed.

imdano
June 11th, 2008, 05:46 PM
I must not be understanding what you want him to do. If you change the if statement to just "if (p == q)", how are you determining if p/q are also equal to a? If a = 6, p = 7, and q = 7, the reduced if statement is true but the result wouldn't match original one.

edit: I think in python you can do
if a == b == c:Is that what you mean?

dmm1285
June 11th, 2008, 05:55 PM
try this:


if (abs(pos[0][0] - pos[1][0]) == (abs(pos[3][0] - pos[2][0])) and abs(pos[2][1] - pos[1][1]))):

imdano
June 11th, 2008, 06:04 PM
try this:


if (abs(pos[0][0] - pos[1][0]) == (abs(pos[3][0] - pos[2][0])) and abs(pos[2][1] - pos[1][1]))):That will evaluate either to False, or the value of abs(pos[2][1] - pos[1][1]). I think this will work though:

if abs(pos[0][0] - pos[1][0]) == abs(pos[3][0] - pos[2][0]) == abs(pos[2][1] - pos[1][1]):

mike_g
June 11th, 2008, 06:06 PM
The condition, in a shorter notation, is:

if ((a == p) && (a == q))

So, you can reduce it to if(p == q), as it's the only way that "a" can meet both conditions simultaneously.
But then how do you know that 'a' equals either 'p' or 'q'?
Edit: >_< just read the rest of the posts.

Personally I'd move some calculations out of the statement:

a = abs(pos[0][0] - pos[1][0])
p = abs(pos[3][0] - pos[2][0])
q = abs(pos[2][1] - pos[1][1])
if (a == p) and (a == q):

molotov00
June 11th, 2008, 06:06 PM
You have:
if a ==b and a==c

Python DOES support
if a==b==c

...which is logically identical to what you have now. It'll be faster because Python doesn't have to calcuate 'a' twice ;)

Although your code looks complex, the actual logic is simple (and can't be made simpler). It's the method calls that make it look longer.

nvteighen
June 11th, 2008, 06:13 PM
I must not be understanding what you want him to do. If you change the if statement to just "if (p == q)", how are you determining if p/q are also equal to a? If a = 6, p = 7, and q = 7, the reduced if statement is true but the result wouldn't match original one.


But then how do you know that 'a' equals either 'p' or 'q'?
Edit: >_< just read the rest of the posts.


Shame on me! Excuse today's horrible performance of mine :oops:

regomodo
June 11th, 2008, 08:16 PM
`

Can+~
June 11th, 2008, 08:56 PM
You're using something[n][a], and I'm guessing the [a] is either X or Y, right?

Why don't implement a coordinate class?


class Shape:
x = 0
y = 0

def something(self):
pass

class Rectangle(Shape):
pass

So now, every object holds their x and y. This approach also makes each object able to calculate something related as a method, or make all shapes have a certain property.

regomodo
June 11th, 2008, 09:05 PM
`

regomodo
June 13th, 2008, 04:20 PM
`

matja
June 13th, 2008, 11:29 PM
I've finished my masters!!!

Congratulations!