Skip to content

Commit fc17569

Browse files
authored
Create Intersecting Lines
1 parent fb7fdb8 commit fc17569

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

interview_query/Intersecting Lines

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Say you are given a list of tuples where the first element is the slope of a line and the second element is the y-intercept of a line.
2+
3+
Write a function find_intersecting to find which lines, if any, intersect with any of the others in the given x_range.
4+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5+
6+
def find_intersecting(list_lines, range_x):
7+
output = []
8+
for i in range(len(list_lines)):
9+
for j in range(i+1, len(list_lines)):
10+
m1, c1 = list_lines[i]
11+
m2, c2 = list_lines[j]
12+
try:
13+
x = (-1*(c1 - c2))/(m1-m2)
14+
if x <= range_x[1] and x >= range_x[0]:
15+
if list_lines[i] not in output:
16+
output.append(list_lines[i])
17+
if list_lines[j] not in output:
18+
output.append(list_lines[j])
19+
except:
20+
continue
21+
return output
22+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
23+
24+
import numpy as np
25+
def find_intersecting(tuple_list,given_range):
26+
slopes=[tuple_list[i][0] for i in range(len(tuple_list))]
27+
intercepts=[tuple_list[i][1] for i in range(len(tuple_list))]
28+
inter_list=[]
29+
for i in range(len(tuple_list)):
30+
for j in range(i+1, len(tuple_list)):
31+
try:
32+
x=(intercepts[j]-intercepts[i])/(slopes[i]-slopes[j])
33+
if x>=given_range[0] and x<=given_range[1]:
34+
inter_list=inter_list+[tuple_list[i],tuple_list[j]]
35+
except:
36+
continue
37+
return list(set(tuple(sublist) for sublist in inter_list))

0 commit comments

Comments
 (0)