
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the cordinates of the fourth vertex of a rectangle with given 3 vertices in Python
Suppose we have a grid of size Q * P, this grid contains exactly three asterisk '*' and every other cell there is dot '.', where '*' is for a vertex of a rectangle. We have to find the coordinates of the missing vertex. Here we will consider 1-based indexing.
So, if the input is like grid = [ ".*.", "...", "*.*" ], then the output will be [1, 3], this is the missing coordinate.
To solve this, we will follow these steps −
p := number of rows
q := number of columns
row := make a map for all row number, and associated value is 0
col := make a map for all column number, and associated value is 0
for i in range 0 to p, do
for j in range 0 to q, do
if grid[i, j] is same as '*', then
row[i] := row[i] + 1
col[j] := col[j] + 1
for each k,v in row, do
if v is same as 1, then
x_coord := k;
for each k,v in col, do
if v is same as 1, then
y_coord := k;
return(x_coord + 1, y_coord + 1)
Example
Let us see the following implementation to get better understanding −
def get_missing_vertex(grid) : p = len(grid) q = len(grid[0]) row = dict.fromkeys(range(p), 0) col = dict.fromkeys(range(q), 0) for i in range(p) : for j in range(q) : if (grid[i][j] == '*') : row[i] += 1 col[j] += 1 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) grid = [".*.", "...", "*.*"] print(get_missing_vertex(grid))
Input
[".*.", "...", "*.*"]
Output
(1, 3)
- Related Articles
- The three vertices of a parallelogram are $(3, 4), (3, 8)$ and $(9, 8)$. Find the fourth vertex.
- Three consecutive vertices of a parallelogram are $(-2, -1), (1, 0)$ and $(4, 3)$. Find the fourth vertex.
- Three vertices of a rectangle ABCD are A(3,1),B(-3,1)andC(-3,3). Plot these points on a graph paper and find the coordinates of the fourth vertex D . Also , find the area of rectangle ABCD.
- If three consecutive vertices of a parallelogram are $(1, -2), (3, 6)$ and $(5, 10)$, find its fourth vertex.
- The points $(3, -4)$ and $(-6, 2)$ are the extremities of a diagonal of a parallelogram. If the third vertex is $(-1, -3)$. Find the coordinates of the fourth vertex.
- Three vertices of a parallelogram are $(a + b, a – b), (2 a + b, 2a – b), (a – b, a + b)$. Find the fourth vertex.
- The area of a triangle is 5. Two of its vertices are $(2, 1)$ and $(3, -2)$. The third vertex lies on $y = x + 3$. Find the third vertex.
- If $(-4, 3)$ and $(4, 3)$ are two vertices of an equilateral triangle, find the coordinates of the third vertex, given that the origin lies in the interior of the triangle.
- If $(-4, 3)$ and $(4, 3)$ are two vertices of an equilateral triangle, find the coordinates of the third vertex, given that the origin lies in the exterior of the triangle.
- If $(0, -3)$ and $(0, 3)$ are the two vertices of an equilateral triangle, find the coordinates of its third vertex.
- An equilateral triangle has two vertices at the points $(3, 4)$, and $(-2, 3)$, find the coordinates of the third vertex.
- $ABCD$ is a parallelogram with vertices $A (x_1, y_1), B (x_2, y_2)$ and $C (x_3, y_3)$. Find the coordinates of the fourth vertex $D$ in terms of $x_1, x_2, x_3, y_1, y_2$ and $y_3$.
- If $A( 1,2) ,B( 4,3) $ and $C( 6,\ 6)$ are the three vertices of a parallelogram $ABCD$, find the coordinates of fourth vertices D.
- Two vertices of a triangle are $(1, 2), (3, 5)$ and its centroid is at the origin. Find the coordinates of the third vertex.
- Find the third vertex of a triangle, if two of its vertices are at $(-3, 1)$ and $(0, -2)$ and the centroid is at the origin.
