-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#1232 checkStraightLine.py
49 lines (42 loc) · 1.35 KB
/
#1232 checkStraightLine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution:
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
x1, y1 = coordinates[0]
x2, y2 = coordinates[1]
# Vertical slope case
if x1 == x2:
for i in range(2, len(coordinates)):
if coordinates[i][0] != x1:
return False
return True
slope = (y2 - y1) / (x2 - x1)
b = y2 - slope * x2
for i in range(2, len(coordinates)):
x, y = coordinates[i]
if y != slope * x + b:
return False
return True
# # This is always valid because
# # the question states that we must have at least 2 coordinates
# x1, y1 = coordinates[0]
# x2, y2 = coordinates[1]
#
# # Case for infinite slope
# if x1 == x2:
# for i in range(2, len(coordinates)):
# if coordinates[i][0] != x1:
# return False
#
# return True
#
# # General case
# slope = (y2 - y1) / (x2 - x1)
#
# # y = mx + b
# # b = y - mx
# intercept = y1 - slope * x1
#
# for i in range(2, len(coordinates)):
# if int(slope * coordinates[i][0] + intercept) != coordinates[i][1]:
# return False
#
# return True