Skip to content

Commit 750278b

Browse files
committed
Updated and functional unit test and function for 2d_line exercise
1 parent 9e9df05 commit 750278b

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

2d_line.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
def find_eqn_2d_line(input_coords):
3+
# INPUT: [(x1, y1), (x2, y2)]
4+
# OUTPUT: m = slope, b = y-intercept
5+
m = (input_coords[1][[1] - input_coords[0][1]) / (input_coords[1][0] - input_coords[0][0])
6+
b = input_coords[0][1] - m * input_coords[0][0]
7+
return (m, b)
8+
9+
def find_y_from_x(m, b, x):
10+
# INPUT: m, b, x
11+
# OUTPUT: y = m*x + b
12+
y = m*x + b
13+
return y

line_2d.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
def find_eqn_2d_line(input_coords):
3+
# INPUT: [(x1, y1), (x2, y2)]
4+
# OUTPUT: m = slope, b = y-intercept
5+
m = (input_coords[1][1] - input_coords[0][1]) / (input_coords[1][0] - input_coords[0][0])
6+
b = input_coords[0][1] - m * input_coords[0][0]
7+
return (m, b)
8+
9+
def find_y_from_x(input_line_x):
10+
# INPUT: m, b, x
11+
# OUTPUT: y = m*x + b
12+
y = input_line_x[0]*input_line_x[2] + input_line_x[1]
13+
return y

test_line_2d.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
def test_find_eqn_2d_line():
3+
from line_2d import find_eqn_2d_line
4+
5+
input_coords = [(1, 8), (4, 14)]
6+
(m, b) = find_eqn_2d_line(input_coords)
7+
8+
assert m == 2
9+
assert b == 6
10+
11+
def test_find_y_from_x():
12+
from line_2d import find_y_from_x
13+
14+
input_line_x = (2, 6, 8)
15+
y_result = find_y_from_x(input_line_x)
16+
17+
assert y_result == 22

0 commit comments

Comments
 (0)