Skip to content

Commit

Permalink
saddle-points: sync tests expected results with problem-specification…
Browse files Browse the repository at this point in the history
…s and update example solution.
  • Loading branch information
BethanyG committed May 22, 2019
1 parent 596fc02 commit b4e3777
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
18 changes: 9 additions & 9 deletions exercises/saddle-points/example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
def saddle_points(m):
if not m:
return set()
if any(len(r) != len(m[0]) for r in m):
def saddle_points(matrix):
if not matrix:
return [{}]
if any(len(row) != len(matrix[0]) for row in matrix):
raise ValueError('irregular matrix')
mmax = [max(r) for r in m]
mmin = [min(c) for c in zip(*m)]
points = [(i+1, j+1) for i in range(len(m))
for j in range(len(m[0])) if mmax[i] == mmin[j]]
mmax = [max(row) for row in matrix]
mmin = [min(col) for col in zip(*matrix)]
points = [{"row": index + 1, "column": col_index + 1} for index in range(len(matrix))
for col_index in range(len(matrix[0])) if mmax[index] == mmin[col_index]]

return set(points)
return points or [{}]
22 changes: 11 additions & 11 deletions exercises/saddle-points/saddle_points_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,50 @@
from saddle_points import saddle_points


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.5.0
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.6.0

class SaddlePointsTest(unittest.TestCase):
def test_identify_single_saddle_point(self):
matrix = [[9, 8, 7], [5, 3, 2], [6, 6, 7]]
self.assertEqual(saddle_points(matrix), set([(2, 1)]))
self.assertEqual(saddle_points(matrix), [{"row": 2, "column": 1}])

def test_empty_matrix_has_no_saddle_points(self):
self.assertEqual(saddle_points([]), set())
self.assertEqual(saddle_points([]), [dict()])

def test_matrix_with_one_elem_has_single_saddle_point(self):
matrix = [[1]]
self.assertEqual(saddle_points(matrix), set([(1, 1)]))
self.assertEqual(saddle_points(matrix), [{"row": 1, "column": 1}])

def test_identify_lack_of_saddle_points_when_there_are_none(self):
matrix = [[1, 2, 3], [3, 1, 2], [2, 3, 1]]
self.assertEqual(saddle_points(matrix), set())
self.assertEqual(saddle_points(matrix), [dict()])

def test_identify_multiple_saddle_points_in_column(self):
matrix = [[4, 5, 4], [3, 5, 5], [1, 5, 4]]
expected = set([(1, 2), (2, 2), (3, 2)])
expected = [{"row": 1, "column": 2}, {"row": 2, "column": 2}, {"row": 3, "column": 2}]
self.assertEqual(saddle_points(matrix), expected)

def test_identify_multiple_saddle_points_in_row(self):
matrix = [[6, 7, 8], [5, 5, 5], [7, 5, 6]]
expected = set([(2, 1), (2, 2), (2, 3)])
expected = [{"row": 2, "column": 1}, {"row": 2, "column": 2}, {"row": 2, "column": 3}]
self.assertEqual(saddle_points(matrix), expected)

def test_identify_saddle_point_in_bottom_right_corner(self):
matrix = [[8, 7, 9], [6, 7, 6], [3, 2, 5]]
expected = set([(3, 3)])
expected = [{"row": 3, "column": 3}]
self.assertEqual(saddle_points(matrix), expected)

def test_non_square_matrix_with_2_saddle_points(self):
matrix = [[3, 1, 3], [3, 2, 4]]
self.assertEqual(saddle_points(matrix), set([(1, 3), (1, 1)]))
self.assertEqual(saddle_points(matrix), [{"row": 1, "column": 1}, {"row": 1, "column": 3}])

def test_single_column_matrix_has_saddle_point_min_value(self):
matrix = [[2], [1], [4], [1]]
self.assertEqual(saddle_points(matrix), set([(2, 1), (4, 1)]))
self.assertEqual(saddle_points(matrix), [{"row": 2, "column": 1}, {"row": 4, "column": 1}])

def test_single_row_matrix_has_saddle_point_in_max_value(self):
matrix = [[2, 5, 3, 5]]
self.assertEqual(saddle_points(matrix), set([(1, 2), (1, 4)]))
self.assertEqual(saddle_points(matrix), [{"row": 1, "column": 2}, {"row": 1, "column": 4}])

# Additional tests for this track

Expand Down

0 comments on commit b4e3777

Please sign in to comment.