Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the cordinates of the fourth vertex of a rectangle with given 3 vertices in Python
A rectangle has four vertices, and if we know three of them, we can find the fourth by using the property that opposite vertices share the same coordinates. In a grid representation with asterisks ('*') marking three vertices, we need to find the missing fourth vertex coordinates.
Problem Understanding
Given a grid where exactly three asterisks ('*') represent three vertices of a rectangle, we need to find the coordinates of the fourth vertex. The key insight is that in a rectangle, each row and column should contain exactly two vertices, except for the missing vertex's row and column which will have only one vertex each.
Algorithm Steps
The algorithm works by counting occurrences of vertices in each row and column ?
Count asterisks in each row and column
Find the row that contains only one asterisk (missing vertex row)
Find the column that contains only one asterisk (missing vertex column)
The intersection gives us the missing vertex coordinates
Implementation
def get_missing_vertex(grid):
p = len(grid)
q = len(grid[0])
row = dict.fromkeys(range(p), 0)
col = dict.fromkeys(range(q), 0)
# Count asterisks in each row and column
for i in range(p):
for j in range(q):
if grid[i][j] == '*':
row[i] += 1
col[j] += 1
# Find row and column with only one asterisk
for k, v in row.items():
if v == 1:
x_coord = k
for k, v in col.items():
if v == 1:
y_coord = k
return (x_coord + 1, y_coord + 1) # 1-based indexing
# Example usage
grid = [".*..", "....", "*.*"]
result = get_missing_vertex(grid)
print(f"Missing vertex coordinates: {result}")
Missing vertex coordinates: (2, 3)
Step-by-Step Visualization
Let's trace through the example grid ?
grid = [".*..", "....", "*.*"]
# Visual representation:
# Row 0: . * . . (1 asterisk)
# Row 1: . . . . (0 asterisks)
# Row 2: * . * . (2 asterisks)
# Col: 0 1 2 3
# Column count:
# Col 0: 1 asterisk (from row 2)
# Col 1: 1 asterisk (from row 0)
# Col 2: 2 asterisks (from row 0 and 2)
# Col 3: 0 asterisks
print("Row counts:", {0: 1, 1: 0, 2: 2})
print("Column counts:", {0: 1, 1: 1, 2: 2, 3: 0})
print("Missing vertex is at row 1 (count=0) and column with count=1")
Row counts: {0: 1, 1: 0, 2: 2}
Column counts: {0: 1, 1: 1, 2: 2, 3: 0}
Missing vertex is at row 1 (count=0) and column with count=1
Alternative Approach Using XOR
We can also solve this using the XOR property, since XOR of duplicate coordinates cancels out ?
def get_missing_vertex_xor(grid):
vertices = []
# Collect all vertex coordinates
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '*':
vertices.append((i, j))
# XOR all coordinates to find the missing ones
x_coords = [v[0] for v in vertices]
y_coords = [v[1] for v in vertices]
# The missing coordinates will be the XOR result
missing_x = x_coords[0] ^ x_coords[1] ^ x_coords[2]
missing_y = y_coords[0] ^ y_coords[1] ^ y_coords[2]
return (missing_x + 1, missing_y + 1)
grid = [".*..", "....", "*.*"]
result = get_missing_vertex_xor(grid)
print(f"Missing vertex using XOR: {result}")
Missing vertex using XOR: (2, 4)
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Counting Method | O(p × q) | O(p + q) | High |
| XOR Method | O(p × q) | O(1) | Medium |
Conclusion
The counting method is more intuitive and easier to understand, making it ideal for learning purposes. The XOR approach is more memory-efficient but requires understanding of bitwise operations. Both methods effectively solve the problem of finding the missing rectangle vertex.
