Skip to content

Commit 2d9af26

Browse files
committed
Added error handling instruction append, added additional test cases, corrected JinJa2 template, and regenerated test cases.
1 parent f4f20bb commit 2d9af26

File tree

5 files changed

+50
-26
lines changed

5 files changed

+50
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
# Notes regarding the implementation of `smallest` and `largest`:
1+
# Instructions append
2+
3+
## Notes regarding the implementation of `smallest` and `largest`:
24

35
Both functions must take two keyword arguments:
46
- `max_factor`: int
57
- `min_factor`: int, default 0
68

7-
Their return value must be a tuple (value, factors) where value is the
8-
palindrome itself, and factors is an iterable containing both factors of the
9-
palindrome in arbitrary order.
9+
Their return value must be a `tuple -- (value, factors)` where `value` is the
10+
palindrome itself, and `factors` is an `iterable` containing both factors of the
11+
palindrome in arbitrary order.
12+
13+
14+
## Exception messages
15+
16+
Sometimes it is necessary to [raise an exception](https://docs.python.org/3/tutorial/errors.html#raising-exceptions). When you do this, you should always include a **meaningful error message** to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the [built in error types](https://docs.python.org/3/library/exceptions.html#base-classes), but should still include a meaningful message.
17+
18+
This particular exercise requires that you use the [raise statement](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement) to "throw" a `ValueError` when the `largest()` or `smallest()` function receives a pair of factors that are not in the correct range. The tests will only pass if you both `raise` the `exception` and include a message with it.
19+
20+
To raise a `ValueError` with a message, write the message as an argument to the `exception` type:
21+
22+
```python
23+
# if the max_factor is less than the min_factor
24+
raise ValueError("min must be <= max")
25+
```

exercises/practice/palindrome-products/.meta/example.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
from __future__ import division
21
from itertools import chain
32
from math import log10, floor, ceil
43

54

65
def largest(min_factor, max_factor):
7-
return get_extreme_palindrome_with_factors(max_factor, min_factor,
8-
"largest")
6+
return get_extreme_palindrome_with_factors(max_factor, min_factor, "largest")
97

108

119
def smallest(max_factor, min_factor):
12-
return get_extreme_palindrome_with_factors(max_factor, min_factor,
13-
"smallest")
10+
return get_extreme_palindrome_with_factors(max_factor, min_factor, "smallest")
1411

1512

1613
def get_extreme_palindrome_with_factors(max_factor, min_factor, extreme):
@@ -53,10 +50,7 @@ def palindromes(max_factor, min_factor, reverse=False):
5350
most of the palindromes just to find the one it needs.
5451
"""
5552
if max_factor < min_factor:
56-
raise ValueError("invalid input: min is {min_factor} "
57-
"and max is {max_factor}"
58-
.format(min_factor=min_factor,
59-
max_factor=max_factor))
53+
raise ValueError("min must be <= max")
6054

6155
minimum = min_factor ** 2
6256
maximum = max_factor ** 2

exercises/practice/palindrome-products/.meta/template.j2

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
{%- set expected = case["expected"] -%}
1010
def test_{{ case["description"] | to_snake }}(self):
1111
{%- if case is error_case %}
12-
with self.assertRaisesWithMessage(ValueError):
12+
with self.assertRaises(ValueError) as err:
1313
{{ value_factor_unpacking(case) }}
14+
self.assertEqual(type(err.exception), ValueError)
15+
self.assertEqual(err.exception.args[0], "{{ case["expected"]["error"] }}")
1416
{%- else %}
1517
{{ value_factor_unpacking(case) }}
1618
{%- if expected["value"] is none %}
@@ -31,5 +33,3 @@ class {{ exercise | camel_case }}Test(unittest.TestCase):
3133

3234
def assertFactorsEqual(self, actual, expected):
3335
self.assertEqual(set(map(frozenset, actual)), set(map(frozenset, expected)))
34-
35-
{{ macros.footer() }}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
def largest(min_factor, max_factor):
2+
"""Given a range of numbers, find the largest palindromes which
3+
are products of two numbers within that range.
4+
5+
:param min_factor: int with a default value of 0
6+
:param max_factor: int
7+
:return: tuple of (palindrome, iterable).
8+
Iterable should contain both factors of the palindrome in an arbitrary order.
9+
"""
10+
211
pass
312

413

514
def smallest(min_factor, max_factor):
15+
"""Given a range of numbers, find the smallest palindromes which
16+
are products of two numbers within that range.
17+
18+
:param min_factor: int with a default value of 0
19+
:param max_factor: int
20+
:return: tuple of (palindrome, iterable).
21+
Iterable should contain both factors of the palindrome in an arbitrary order.
22+
"""
23+
624
pass

exercises/practice/palindrome-products/palindrome_products_test.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,16 @@ def test_empty_result_for_largest_if_no_palindrome_in_the_range(self):
6060
self.assertFactorsEqual(factors, [])
6161

6262
def test_error_result_for_smallest_if_min_is_more_than_max(self):
63-
with self.assertRaisesWithMessage(ValueError):
63+
with self.assertRaises(ValueError) as err:
6464
value, factors = smallest(min_factor=10000, max_factor=1)
65+
self.assertEqual(type(err.exception), ValueError)
66+
self.assertEqual(err.exception.args[0], "min must be <= max")
6567

6668
def test_error_result_for_largest_if_min_is_more_than_max(self):
67-
with self.assertRaisesWithMessage(ValueError):
69+
with self.assertRaises(ValueError) as err:
6870
value, factors = largest(min_factor=2, max_factor=1)
71+
self.assertEqual(type(err.exception), ValueError)
72+
self.assertEqual(err.exception.args[0], "min must be <= max")
6973

7074
def assertFactorsEqual(self, actual, expected):
7175
self.assertEqual(set(map(frozenset, actual)), set(map(frozenset, expected)))
72-
73-
# Utility functions
74-
def assertRaisesWithMessage(self, exception):
75-
return self.assertRaisesRegex(exception, r".+")
76-
77-
78-
if __name__ == "__main__":
79-
unittest.main()

0 commit comments

Comments
 (0)