From b4e3777ce91bba8b91c40e4086aaa3a39ecff0b4 Mon Sep 17 00:00:00 2001 From: Bethany Garcia Date: Tue, 21 May 2019 18:54:24 -0700 Subject: [PATCH 1/3] saddle-points: sync tests expected results with problem-specifications and update example solution. --- exercises/saddle-points/example.py | 18 +++++++-------- exercises/saddle-points/saddle_points_test.py | 22 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/exercises/saddle-points/example.py b/exercises/saddle-points/example.py index 76cbaedb160..6d2af0cb46a 100644 --- a/exercises/saddle-points/example.py +++ b/exercises/saddle-points/example.py @@ -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 [{}] \ No newline at end of file diff --git a/exercises/saddle-points/saddle_points_test.py b/exercises/saddle-points/saddle_points_test.py index 636c5ec3883..71d1fa74afc 100644 --- a/exercises/saddle-points/saddle_points_test.py +++ b/exercises/saddle-points/saddle_points_test.py @@ -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 From bcdd67f723efb01867f4d5681beb0f51f8e322b7 Mon Sep 17 00:00:00 2001 From: Bethany Garcia Date: Tue, 21 May 2019 19:09:15 -0700 Subject: [PATCH 2/3] Revert "saddle-points: sync tests expected results with problem-specifications and update example solution." This reverts commit b4e3777ce91bba8b91c40e4086aaa3a39ecff0b4. Wanted this in it's own branch. --- exercises/saddle-points/example.py | 18 +++++++-------- exercises/saddle-points/saddle_points_test.py | 22 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/exercises/saddle-points/example.py b/exercises/saddle-points/example.py index 6d2af0cb46a..76cbaedb160 100644 --- a/exercises/saddle-points/example.py +++ b/exercises/saddle-points/example.py @@ -1,11 +1,11 @@ -def saddle_points(matrix): - if not matrix: - return [{}] - if any(len(row) != len(matrix[0]) for row in matrix): +def saddle_points(m): + if not m: + return set() + if any(len(r) != len(m[0]) for r in m): raise ValueError('irregular matrix') - 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]] + 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]] - return points or [{}] \ No newline at end of file + return set(points) diff --git a/exercises/saddle-points/saddle_points_test.py b/exercises/saddle-points/saddle_points_test.py index 71d1fa74afc..636c5ec3883 100644 --- a/exercises/saddle-points/saddle_points_test.py +++ b/exercises/saddle-points/saddle_points_test.py @@ -10,50 +10,50 @@ from saddle_points import saddle_points -# Tests adapted from `problem-specifications//canonical-data.json` @ v1.6.0 +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.5.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), [{"row": 2, "column": 1}]) + self.assertEqual(saddle_points(matrix), set([(2, 1)])) def test_empty_matrix_has_no_saddle_points(self): - self.assertEqual(saddle_points([]), [dict()]) + self.assertEqual(saddle_points([]), set()) def test_matrix_with_one_elem_has_single_saddle_point(self): matrix = [[1]] - self.assertEqual(saddle_points(matrix), [{"row": 1, "column": 1}]) + self.assertEqual(saddle_points(matrix), set([(1, 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), [dict()]) + self.assertEqual(saddle_points(matrix), set()) def test_identify_multiple_saddle_points_in_column(self): matrix = [[4, 5, 4], [3, 5, 5], [1, 5, 4]] - expected = [{"row": 1, "column": 2}, {"row": 2, "column": 2}, {"row": 3, "column": 2}] + expected = set([(1, 2), (2, 2), (3, 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 = [{"row": 2, "column": 1}, {"row": 2, "column": 2}, {"row": 2, "column": 3}] + expected = set([(2, 1), (2, 2), (2, 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 = [{"row": 3, "column": 3}] + expected = set([(3, 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), [{"row": 1, "column": 1}, {"row": 1, "column": 3}]) + self.assertEqual(saddle_points(matrix), set([(1, 3), (1, 1)])) def test_single_column_matrix_has_saddle_point_min_value(self): matrix = [[2], [1], [4], [1]] - self.assertEqual(saddle_points(matrix), [{"row": 2, "column": 1}, {"row": 4, "column": 1}]) + self.assertEqual(saddle_points(matrix), set([(2, 1), (4, 1)])) def test_single_row_matrix_has_saddle_point_in_max_value(self): matrix = [[2, 5, 3, 5]] - self.assertEqual(saddle_points(matrix), [{"row": 1, "column": 2}, {"row": 1, "column": 4}]) + self.assertEqual(saddle_points(matrix), set([(1, 2), (1, 4)])) # Additional tests for this track From a62f393891859a5361825afd05300cc2d9855465 Mon Sep 17 00:00:00 2001 From: Bethany Garcia Date: Thu, 23 May 2019 11:21:28 -0700 Subject: [PATCH 3/3] book-store: sync expected test results and input data with problem-specifications. --- exercises/book-store/book_store.py | 2 +- exercises/book-store/book_store_test.py | 32 +++++++++++----------- exercises/book-store/example.py | 36 +++++++++++++------------ 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/exercises/book-store/book_store.py b/exercises/book-store/book_store.py index f389c850424..8a84288d2d3 100644 --- a/exercises/book-store/book_store.py +++ b/exercises/book-store/book_store.py @@ -1,2 +1,2 @@ -def calculate_total(books): +def total(basket): pass diff --git a/exercises/book-store/book_store_test.py b/exercises/book-store/book_store_test.py index a09d979e93b..b82cf16a75c 100644 --- a/exercises/book-store/book_store_test.py +++ b/exercises/book-store/book_store_test.py @@ -1,58 +1,58 @@ import unittest -from book_store import calculate_total +from book_store import total # Tests adapted from `problem-specifications//canonical-data.json` @ v1.4.0 class BookStoreTest(unittest.TestCase): def test_only_a_single_book(self): - self.assertEqual(calculate_total([1]), 800) + self.assertEqual(total([1]), 800) def test_two_of_the_same_book(self): - self.assertEqual(calculate_total([2, 2]), 1600) + self.assertEqual(total([2, 2]), 1600) def test_empty_basket(self): - self.assertEqual(calculate_total([]), 0) + self.assertEqual(total([]), 0) def test_two_different_books(self): - self.assertEqual(calculate_total([1, 2]), 1520) + self.assertEqual(total([1, 2]), 1520) def test_three_different_books(self): - self.assertEqual(calculate_total([1, 2, 3]), 2160) + self.assertEqual(total([1, 2, 3]), 2160) def test_four_different_books(self): - self.assertEqual(calculate_total([1, 2, 3, 4]), 2560) + self.assertEqual(total([1, 2, 3, 4]), 2560) def test_five_different_books(self): - self.assertEqual(calculate_total([1, 2, 3, 4, 5]), 3000) + self.assertEqual(total([1, 2, 3, 4, 5]), 3000) def test_two_groups_of_4_is_cheaper_than_group_of_5_plus_group_of_3(self): - self.assertEqual(calculate_total([1, 1, 2, 2, 3, 3, 4, 5]), 5120) + self.assertEqual(total([1, 1, 2, 2, 3, 3, 4, 5]), 5120) def test_two_groups_of_4_is_cheaper_than_groups_of_5_and_3(self): - self.assertEqual(calculate_total([1, 1, 2, 3, 4, 4, 5, 5]), 5120) + self.assertEqual(total([1, 1, 2, 3, 4, 4, 5, 5]), 5120) def test_group_of_4_plus_group_of_2_is_cheaper_than_2_groups_of_3(self): - self.assertEqual(calculate_total([1, 1, 2, 2, 3, 4]), 4080) + self.assertEqual(total([1, 1, 2, 2, 3, 4]), 4080) def test_two_each_of_first_4_books_and_1_copy_each_of_rest(self): - self.assertEqual(calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5]), 5560) + self.assertEqual(total([1, 1, 2, 2, 3, 3, 4, 4, 5]), 5560) def test_two_copies_of_each_book(self): - self.assertEqual(calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]), 6000) + self.assertEqual(total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]), 6000) def test_three_copies_of_first_book_and_2_each_of_remaining(self): self.assertEqual( - calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1]), 6800) + total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1]), 6800) def test_three_each_of_first_2_books_and_2_each_of_remaining_books(self): self.assertEqual( - calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2]), 7520) + total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2]), 7520) def test_four_groups_of_4_are_cheaper_than_2_groups_each_of_5_and_3(self): self.assertEqual( - calculate_total([1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5]), + total([1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5]), 10240) diff --git a/exercises/book-store/example.py b/exercises/book-store/example.py index 15fb08fd099..4236656ada4 100644 --- a/exercises/book-store/example.py +++ b/exercises/book-store/example.py @@ -4,8 +4,8 @@ discount = [1.0, 1.0, 0.95, 0.9, 0.8, 0.75] -def groupCost(g): - return len(g) * discount[len(g)] +def group_cost(group): + return len(group) * discount[len(group)] class Grouping: @@ -13,24 +13,25 @@ def __init__(self, groups=None): self.groups = [set()] if groups is None else groups def total(self): - return sum(map(groupCost, self.groups)) * BASE_COST + return sum(map(group_cost, self.groups)) * BASE_COST def dup(self): return Grouping(list(map(set, self.groups))) - def add_to_valid(self, b): - """Returns all possible groupings from current grouping adding book b + def add_to_valid(self, book): + """Returns all possible groupings from the + current grouping adding book """ other = self.dup() other.groups.sort(key=lambda g: len(g)) results = [] - for i, g in enumerate(other.groups): - if b not in g: - o2 = other.dup() - o2.groups[i].add(b) - results.append(o2) + for index, group in enumerate(other.groups): + if book not in group: + other2 = other.dup() + other2.groups[index].add(book) + results.append(other2) if not results: - other.groups.append(set([b])) + other.groups.append(set([book])) return [other] return results @@ -38,12 +39,13 @@ def __lt__(self, other): return self.total() < other.total() -def step(rs, b): - return [g for r in rs for g in r.add_to_valid(b)] +def step(basket, book): + return [group for groupings in basket + for group in groupings.add_to_valid(book)] -def calculate_total(books): - if len(books) == 0: +def total(basket): + if len(basket) == 0: return 0 - start = Grouping([{books[0]}]) - return round(min(reduce(step, books[1:], [start])).total()) + start = Grouping([{basket[0]}]) + return round(min(reduce(step, basket[1:], [start])).total())