
- 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
Program to check whether list of points form a straight line or not in Python
Suppose we have a list coordinates in a Cartesian plane, we have to check whether the coordinates form a straight line segment or not.
So, if the input is like coordinates = [(5, 5),(8, 8),(9, 9)], then the output will be True, as these points are forming a line segment with a slope 1.
To solve this, we will follow these steps −
- (x0, y0) := coordinates[0]
- (x1, y1) := coordinates[1]
- for i in range 2 to size of coordinates list - 1, do
- (x, y) := coordinates[i]
- if (x0 - x1) * (y1 - y) is not same as (x1 - x) * (y0 - y1), then
- return False
- return True
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, coordinates): (x0, y0), (x1, y1) = coordinates[0], coordinates[1] for i in range(2, len(coordinates)): x, y = coordinates[i] if (x0 - x1) * (y1 - y) != (x1 - x) * (y0 - y1): return False return True ob = Solution() coordinates = [[5, 5],[8, 8],[9, 9]] print(ob.solve(coordinates))
Input
[[5, 5],[8, 8],[9, 9]]
Output
True
- Related Articles
- Python program to check whether a list is empty or not?
- Program to check whether given list of blocks are symmetric over x = y line or not in python
- C# program to check whether a list is empty or not
- Program to check whether given list is in valid state or not in Python
- Program to check whether list is alternating increase and decrease or not in Python
- Program to check if we reverse sublist of one list to form second list or not in Python
- Program to check whether parentheses are balanced or not in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Python program to check whether a given string is Heterogram or not
- Program to check whether a binary tree is complete or not in Python
- Program to check whether a binary tree is BST or not in Python
- Program to check whether all can get a seat or not in Python
- Program to check points are forming convex hull or not in Python
- Program to check points are forming concave polygon or not in Python
- Program to check whether every rotation of a number is prime or not in Python

Advertisements