Given the coordinates of four points in 2D space p1, p2, p3 and p4, return true if the four points construct a square. The coordinate of a point pi is represented as [xi, yi]. The input is not given in any order.
A valid square has four equal sides with positive length and four equal angles (90-degree angles).
Example 1:
Input: p1 = [2,6], p2 = [5,1], p3 = [0,-2], p4 = [-3,3]
Output: true
Example 2:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Output: false
import math A=list(map(int,input('Point p1: ').split(','))) B=list(map(int,input('Point p2: ').split(','))) C=list(map(int,input('Point p3: ').split(','))) D=list(map(int,input('Point p4: ').split(','))) LIST=[A,B,C,D] M=sorted(LIST , key=lambda X: [X[1], X[0]]) p1,p2,p3,p4 = M[0],M[1],M[2],M[3] def length(x,y): return (((y[0]-x[0])**2) + ((y[1]-x[1])**2)) flag=0 if length(p1,p2)==length(p2,p4): if length(p2,p4) == length(p4,p3): if length(p4,p3) == length(p3,p1): if length(p2,p3) == length(p1,p4): flag=1 else: flag=0 else: flag=0 else: flag=0 else: flag=0 print(True) if flag==1 else print(False)
YOU CAN INPUT THE COORDINATES(POINTS) IN ANY ORDER.
PRACTICAL SAMPLE IMAGE BELOW
Input_1:
Point p1: 2,6
Point p2: 5,1
Point p3: 0,-2
Point p4: -3,3
Output:
True
Input_2:
Point p1: 0,0
Point p2: 1,1
Point p3: 1,0
Point p4: 0,12
Output:
False
Input_3:
Point p1: 0,0
Point p2: 1,1
Point p3: 1,0
Point p4: 0,1
Output:
True
Input_4:
Point p1: 1,0
Point p2: -1,0
Point p3: 0,1
Point p4: 0,-1
Output:
True
Input_5:
Point p1: 10,20
Point p2: 20,20
Point p3: 10,10
Point p4: 20,10
Output:
True
Sample Image of Output and the Execution of the Code.
More Q
- Find the majority element appearing more than n/2 times
- Return an array of the squares of each number.
- Highest factor in addition.
- Find the empty packets(0) of chocolate and push it to the end of the conveyor belt(array).
- Every element in array appears twice.Find that single one.
- Find the duplicate letters in string. Same to do in dictionary way.
- Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’.
- Return the maximum number you can get by changing at most one digit.
- Sed Commands – Ranges of lines.
- Find the kth smallest element in the given 2D array.
- Find all elements that appear more than [n/3] times.
- Write a function to return true if s2 contains the permutation of s1.
- Sort the array into a wave like array.
- Re-order array, such that
nums[0] < nums[1] > nums[2] < nums[3]...
. - Given the coordinates, return true if the four points construct a square.
- Minimum Distance Between Words of a String.
- Return the shortest distance between these two words.
- Write a program to find the n-th ugly number.
- Check if array could become Non – Decreasing array.
- Find the minimum area of a rectangle that can be formed from given points.