Using python to find the missing coordinate of a rectangle
Question:
Problem: Given integer coordinates of three vertices of a rectangle whose sides are parallel to coordinate axes, find the coordinates of the fourth vertex of the rectangle.
I have written the code to answer the problem as follows (but it’s not correct):
coord_1_x = int(input())
coord_1_y = int(input())
coord_2_x = int(input())
coord_2_y = int(input())
coord_3_x = int(input())
coord_3_y = int(input())
coord_4_x = 0
coord_4_y = 0
if coord_1_x == coord_2_x:
coord_4_x = coord_3_x
if coord_2_y > coord_1_y:
coord_4_y = coord_2_y
else:
coord_4_y = coord_1_y
else:
if coord_3_x == coord_1_x:
coord_4_x = coord_2_x
coord_4_y = coord_3_y
print(coord_4_x)
print(coord_4_y)
Here’s some example inputs/outputs that the code should display:
Example input #1 – Three vertices given are (1, 5), (7, 5), (1, 10)
1
5
7
5
1
10
Example output #1
7
10
Example input #2 – Three vertices given are (1, 5), (7, 10), (1, 10)
1
5
7
10
1
10
Example output #2
7
5
Please can someone help me determine the correct code to answer this problem? (I’ve tried Googling/ reading previous Stack posts but can’t find an answer)
Note: The code should only use if/else statements, not arrays or loops.
Answers:
if coord1_x == coord2_x or coord1_x==coord3_x:
if coord1_x == coord2_x:
coord4_x=coord3_x
else:
coord4_x=coord2_x
else:
coord4_x=coord1_x
if coord1_y == coord2_y or coord1_y==coord3_y:
if coord1_y == coord2_y:
coord4_y=coord3_y
else:
coord4_y=coord2_y
else:
coord4_y=coord1_y
Since a rectangle is symmetric, the X and Y coordinates need to appear 2 times in all Points of your rectangle. So you would just need to find the X and Y values that appear once in your given points:
def getMissingRectanglePoint(A, B, C):
rectangle = [A, B, C]
xValues = [p[0] for p in rectangle]
yValues = [p[1] for p in rectangle]
missingX = [mp for mp in xValues if xValues.count(mp) == 1][0]
missingY = [mp for mp in yValues if yValues.count(mp) == 1][0]
print missingX
print missingY
getMissingRectanglePoint( (1, 5), (7, 5), (1, 10) )
getMissingRectanglePoint( (1, 5), (7, 10), (1, 10) )
Prints:
7
10
7
5
mylist = []
for i in range(6):
num = input('num: ')
mylist.append(num)
for i in range(2):
if mylist.count(mylist[i]) == 2:
number = ''.join(mylist[i])
mylist.remove(number)
mylist.remove(number)
print(mylist)
Problem: Given integer coordinates of three vertices of a rectangle whose sides are parallel to coordinate axes, find the coordinates of the fourth vertex of the rectangle.
I have written the code to answer the problem as follows (but it’s not correct):
coord_1_x = int(input())
coord_1_y = int(input())
coord_2_x = int(input())
coord_2_y = int(input())
coord_3_x = int(input())
coord_3_y = int(input())
coord_4_x = 0
coord_4_y = 0
if coord_1_x == coord_2_x:
coord_4_x = coord_3_x
if coord_2_y > coord_1_y:
coord_4_y = coord_2_y
else:
coord_4_y = coord_1_y
else:
if coord_3_x == coord_1_x:
coord_4_x = coord_2_x
coord_4_y = coord_3_y
print(coord_4_x)
print(coord_4_y)
Here’s some example inputs/outputs that the code should display:
Example input #1 – Three vertices given are (1, 5), (7, 5), (1, 10)
1
5
7
5
1
10
Example output #1
7
10
Example input #2 – Three vertices given are (1, 5), (7, 10), (1, 10)
1
5
7
10
1
10
Example output #2
7
5
Please can someone help me determine the correct code to answer this problem? (I’ve tried Googling/ reading previous Stack posts but can’t find an answer)
Note: The code should only use if/else statements, not arrays or loops.
if coord1_x == coord2_x or coord1_x==coord3_x:
if coord1_x == coord2_x:
coord4_x=coord3_x
else:
coord4_x=coord2_x
else:
coord4_x=coord1_x
if coord1_y == coord2_y or coord1_y==coord3_y:
if coord1_y == coord2_y:
coord4_y=coord3_y
else:
coord4_y=coord2_y
else:
coord4_y=coord1_y
Since a rectangle is symmetric, the X and Y coordinates need to appear 2 times in all Points of your rectangle. So you would just need to find the X and Y values that appear once in your given points:
def getMissingRectanglePoint(A, B, C):
rectangle = [A, B, C]
xValues = [p[0] for p in rectangle]
yValues = [p[1] for p in rectangle]
missingX = [mp for mp in xValues if xValues.count(mp) == 1][0]
missingY = [mp for mp in yValues if yValues.count(mp) == 1][0]
print missingX
print missingY
getMissingRectanglePoint( (1, 5), (7, 5), (1, 10) )
getMissingRectanglePoint( (1, 5), (7, 10), (1, 10) )
Prints:
7
10
7
5
mylist = []
for i in range(6):
num = input('num: ')
mylist.append(num)
for i in range(2):
if mylist.count(mylist[i]) == 2:
number = ''.join(mylist[i])
mylist.remove(number)
mylist.remove(number)
print(mylist)