Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions exercises/change/change_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from change import find_minimum_coins

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

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

class ChangeTest(unittest.TestCase):
def test_single_coin_change(self):
Expand Down Expand Up @@ -37,13 +37,26 @@ def test_no_coins_make_0_change(self):
self.assertEqual(find_minimum_coins(0, [1, 5, 10, 21, 25]), [])

def test_error_testing_for_change_smaller_than_smallest_coin(self):
self.assertEqual(find_minimum_coins(3, [5, 10]), -1)
with self.assertRaisesWithMessage(ValueError):
find_minimum_coins(3, [5, 10])

def test_error_if_no_combination_can_add_up_to_target(self):
self.assertEqual(find_minimum_coins(94, [5, 10]), -1)
with self.assertRaisesWithMessage(ValueError):
find_minimum_coins(94, [5, 10])

def test_cannot_find_negative_change_values(self):
self.assertEqual(find_minimum_coins(-5, [1, 2, 5]), -1)
with self.assertRaisesWithMessage(ValueError):
find_minimum_coins(-5, [1, 2, 5])

# Utility functions
def setUp(self):
try:
self.assertRaisesRegex
except AttributeError:
self.assertRaisesRegex = self.assertRaisesRegexp

def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions exercises/change/example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def find_minimum_coins(total_change, coins):
if total_change < 0:
return -1
raise ValueError("cannot find negative change values")
min_coins_required = [1e9] * (total_change + 1)
last_coin = [0]*(total_change + 1)
min_coins_required[0] = 0
Expand All @@ -15,7 +15,7 @@ def find_minimum_coins(total_change, coins):
last_coin[change] = change - coin
min_coins_required[change] = final_result
if min_coins_required[total_change] == 1e9:
return -1
raise ValueError("no combination can add up to target")
else:
last_coin_value = total_change
array = []
Expand Down