absolute value | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

absolute value

situation: my programm gets 4 int(input()) the first 2 inputs are the x coordinate and the second 2 inputs are the y coordinate on a chess board. Now the program should know if the bishop can go or not. my code is simple: # bishop move x1 = int(input()) y1 = int(input()) x2 = int(input()) y2 = int(input()) if abs(x1 - y1) == abs(y2 - x2): print("YES") else: print("NO") if I do the evaluation then this code can run some examples and others not. can anybody of you help me to find out why? for example if the bishop goes from 4,4 (4D) to 5,3 (5C) then i get a wrong result. I have now idea why because the absolute value is both time same big. now my question: can I not say abs(xy) == abs(xy)? do i have to use other ways? thanks for help

28th May 2020, 3:37 PM
Sur Tur
Sur Tur - avatar
7 Answers
+ 3
Well by the angle I meant the direction of movement. 45° means a bishop going toward top right corner 225° means a bishop going toward bottom left corner. Think In polar coordinates system. Hope it answer you
28th May 2020, 4:41 PM
Hacker Badshah
Hacker Badshah - avatar
+ 2
Well we know Bishop can only move diagonally So the slope of its movement would be 1(45°,225°) or -1(135°,315°) So you have to check if any of the two equation is true or not (x2 - x1 == y2 - y1) or (-x2 + x1 == y2 - y1) Thank You ☺️
28th May 2020, 4:19 PM
Hacker Badshah
Hacker Badshah - avatar
+ 2
I have a smaller one than this ; x1 = int(input()) y1 = int(input()) x2 = int(input()) y2 = int(input()) if (x2-x1 == y2-y1) or (-x2 + x1==y2-y1): print("YES") else: print("NO") Thank You ☺️
28th May 2020, 4:36 PM
Hacker Badshah
Hacker Badshah - avatar
+ 1
cool thanks. finaly i fount a way for my code: # bishop move x1 = int(input()) y1 = int(input()) x2 = int(input()) y2 = int(input()) if (x1 + y1) % 2 == 0: if (x2 + y2) % 2 == 0: if abs(x2 - x1) == abs(y2 - y1): print("YES") else: print("NO") else: print("NO") elif (x1 + y1) % 2 == 1: if (x2 + y2) % 2 == 1: if abs(x2 - x1) == abs(y2 - y1): print("YES") else: print("NO") else: print("NO") else: print("NO")
28th May 2020, 4:30 PM
Sur Tur
Sur Tur - avatar
+ 1
Hacker Badshah you said: "So the slope of its movement would be 1(45°,225°) or -1(135°,315°)" one more question: what means 45° ? you put the chess board in a circle?
28th May 2020, 4:34 PM
Sur Tur
Sur Tur - avatar
+ 1
yeah helps thanks :D
28th May 2020, 4:43 PM
Sur Tur
Sur Tur - avatar
0
Shouldn't it be x1-x2 and y1-y2, respectively? Or switch the sequence of the input to make it more intuitive..
28th May 2020, 4:21 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar