Skip to content

Commit 220fce3

Browse files
authored
Updated Exercise docs, toml files, test files and example solutions for: (#3329)
* Palindrom Products (toml and tests) * Roman Numerals (tests) * Phone Number (toml, tests, and example solution) * Resistor Color Trio (intro, toml, tests, and example solution)
1 parent b3f23ce commit 220fce3

File tree

11 files changed

+113
-42
lines changed

11 files changed

+113
-42
lines changed

exercises/practice/palindrome-products/.meta/tests.toml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[5cff78fe-cf02-459d-85c2-ce584679f887]
6-
description = "finds the smallest palindrome from single digit factors"
13+
description = "find the smallest palindrome from single digit factors"
714

815
[0853f82c-5fc4-44ae-be38-fadb2cced92d]
9-
description = "finds the largest palindrome from single digit factors"
16+
description = "find the largest palindrome from single digit factors"
1017

1118
[66c3b496-bdec-4103-9129-3fcb5a9063e1]
1219
description = "find the smallest palindrome from double digit factors"
@@ -15,13 +22,13 @@ description = "find the smallest palindrome from double digit factors"
1522
description = "find the largest palindrome from double digit factors"
1623

1724
[cecb5a35-46d1-4666-9719-fa2c3af7499d]
18-
description = "find smallest palindrome from triple digit factors"
25+
description = "find the smallest palindrome from triple digit factors"
1926

2027
[edab43e1-c35f-4ea3-8c55-2f31dddd92e5]
2128
description = "find the largest palindrome from triple digit factors"
2229

2330
[4f802b5a-9d74-4026-a70f-b53ff9234e4e]
24-
description = "find smallest palindrome from four digit factors"
31+
description = "find the smallest palindrome from four digit factors"
2532

2633
[787525e0-a5f9-40f3-8cb2-23b52cf5d0be]
2734
description = "find the largest palindrome from four digit factors"
@@ -37,3 +44,6 @@ description = "error result for smallest if min is more than max"
3744

3845
[eeeb5bff-3f47-4b1e-892f-05829277bd74]
3946
description = "error result for largest if min is more than max"
47+
48+
[16481711-26c4-42e0-9180-e2e4e8b29c23]
49+
description = "smallest product does not use the smallest factor"

exercises/practice/palindrome-products/palindrome_products_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,10 @@ def test_error_result_for_largest_if_min_is_more_than_max(self):
7171
self.assertEqual(type(err.exception), ValueError)
7272
self.assertEqual(err.exception.args[0], "min must be <= max")
7373

74+
def test_smallest_product_does_not_use_the_smallest_factor(self):
75+
value, factors = smallest(min_factor=3215, max_factor=4000)
76+
self.assertEqual(value, 10988901)
77+
self.assertFactorsEqual(factors, [[3297, 3333]])
78+
7479
def assertFactorsEqual(self, actual, expected):
7580
self.assertEqual(set(map(frozenset, actual)), set(map(frozenset, expected)))

exercises/practice/phone-number/.docs/instructions.append.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ To raise a `ValueError` with a message, write the message as an argument to the
1010

1111
```python
1212
# if a phone number has less than 10 digits.
13-
raise ValueError("incorrect number of digits")
13+
raise ValueError("must not be fewer than 10 digits")
1414

1515
# if a phone number has more than 11 digits.
16-
raise ValueError("more than 11 digits")
16+
raise ValueError("must not be greater than 11 digits")
1717

1818
# if a phone number has 11 digits, but starts with a number other than 1.
1919
raise ValueError("11 digits must start with 1")

exercises/practice/phone-number/.meta/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def _clean(self, number):
2525

2626
def _normalize(self, number):
2727
if len(number) < 10:
28-
raise ValueError('incorrect number of digits')
28+
raise ValueError('must not be fewer than 10 digits')
2929

3030
if len(number) > 11:
31-
raise ValueError('more than 11 digits')
31+
raise ValueError('must not be greater than 11 digits')
3232

3333
if len(number) == 10 or len(number) == 11 and number.startswith('1'):
3434
if number[-10] == '0':

exercises/practice/phone-number/.meta/tests.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ description = "cleans numbers with multiple spaces"
2020

2121
[598d8432-0659-4019-a78b-1c6a73691d21]
2222
description = "invalid when 9 digits"
23+
include = false
24+
25+
[2de74156-f646-42b5-8638-0ef1d8b58bc2]
26+
description = "invalid when 9 digits"
27+
reimplements = "598d8432-0659-4019-a78b-1c6a73691d21"
2328

2429
[57061c72-07b5-431f-9766-d97da7c4399d]
2530
description = "invalid when 11 digits does not start with a 1"
@@ -32,6 +37,11 @@ description = "valid when 11 digits and starting with 1 even with punctuation"
3237

3338
[c6a5f007-895a-4fc5-90bc-a7e70f9b5cad]
3439
description = "invalid when more than 11 digits"
40+
include = false
41+
42+
[4a1509b7-8953-4eec-981b-c483358ff531]
43+
description = "invalid when more than 11 digits"
44+
reimplements = "c6a5f007-895a-4fc5-90bc-a7e70f9b5cad"
3545

3646
[63f38f37-53f6-4a5f-bd86-e9b404f10a60]
3747
description = "invalid with letters"

exercises/practice/phone-number/phone_number_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_invalid_when_9_digits(self):
2424
with self.assertRaises(ValueError) as err:
2525
PhoneNumber("123456789")
2626
self.assertEqual(type(err.exception), ValueError)
27-
self.assertEqual(err.exception.args[0], "incorrect number of digits")
27+
self.assertEqual(err.exception.args[0], "must not be fewer than 10 digits")
2828

2929
def test_invalid_when_11_digits_does_not_start_with_a_1(self):
3030
with self.assertRaises(ValueError) as err:
@@ -44,7 +44,7 @@ def test_invalid_when_more_than_11_digits(self):
4444
with self.assertRaises(ValueError) as err:
4545
PhoneNumber("321234567890")
4646
self.assertEqual(type(err.exception), ValueError)
47-
self.assertEqual(err.exception.args[0], "more than 11 digits")
47+
self.assertEqual(err.exception.args[0], "must not be greater than 11 digits")
4848

4949
def test_invalid_with_letters(self):
5050
with self.assertRaises(ValueError) as err:

exercises/practice/resistor-color-trio/.docs/instructions.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ So an input of `"orange", "orange", "black"` should return:
4646

4747
> "33 ohms"
4848
49-
When we get more than a thousand ohms, we say "kiloohms".
50-
That's similar to saying "kilometer" for 1000 meters, and "kilograms" for 1000 grams.
49+
When we get to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms".
50+
That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams".
5151

52-
So an input of `"orange", "orange", "orange"` should return:
52+
For example, an input of `"orange", "orange", "orange"` should return:
5353

5454
> "33 kiloohms"
55+
56+
[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix
Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
COLORS = [
2-
'black',
3-
'brown',
4-
'red',
5-
'orange',
6-
'yellow',
7-
'green',
8-
'blue',
9-
'violet',
10-
'grey',
11-
'white'
2+
'black',
3+
'brown',
4+
'red',
5+
'orange',
6+
'yellow',
7+
'green',
8+
'blue',
9+
'violet',
10+
'grey',
11+
'white'
1212
]
1313

1414

1515
def label(colors):
1616
value = 10 * COLORS.index(colors[0]) + COLORS.index(colors[1])
1717
value *= 10 ** COLORS.index(colors[2])
18-
return str(value) + ' ohms' if value < 1000 else str(value // 1000) + ' kiloohms'
18+
label = str(value)
19+
20+
if len(label) < 4 :
21+
unit = 'ohms'
22+
elif len(label) < 7:
23+
label = str(value//1000)
24+
unit = 'kiloohms'
25+
elif len(label) <= 8 :
26+
label = str(value//1000000)
27+
unit = 'megaohms'
28+
elif len(label) >= 9:
29+
label = str(value//1000000000)
30+
unit = 'gigaohms'
31+
32+
return f'{value if value < 1000 else label} {unit}'

exercises/practice/resistor-color-trio/.meta/tests.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,18 @@ description = "Green and brown and orange"
2323

2424
[f5d37ef9-1919-4719-a90d-a33c5a6934c9]
2525
description = "Yellow and violet and yellow"
26+
27+
[5f6404a7-5bb3-4283-877d-3d39bcc33854]
28+
description = "Blue and violet and blue"
29+
30+
[7d3a6ab8-e40e-46c3-98b1-91639fff2344]
31+
description = "Minimum possible value"
32+
33+
[ca0aa0ac-3825-42de-9f07-dac68cc580fd]
34+
description = "Maximum possible value"
35+
36+
[0061a76c-903a-4714-8ce2-f26ce23b0e09]
37+
description = "First two colors make an invalid octal number"
38+
39+
[30872c92-f567-4b69-a105-8455611c10c4]
40+
description = "Ignore extra colors"

exercises/practice/resistor-color-trio/resistor_color_trio_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,18 @@ def test_green_and_brown_and_orange(self):
2222

2323
def test_yellow_and_violet_and_yellow(self):
2424
self.assertEqual(label(["yellow", "violet", "yellow"]), "470 kiloohms")
25+
26+
def test_blue_and_violet_and_blue(self):
27+
self.assertEqual(label(["blue", "violet", "blue"]), "67 megaohms")
28+
29+
def test_minimum_possible_value(self):
30+
self.assertEqual(label(["black", "black", "black"]), "0 ohms")
31+
32+
def test_maximum_possible_value(self):
33+
self.assertEqual(label(["white", "white", "white"]), "99 gigaohms")
34+
35+
def test_first_two_colors_make_an_invalid_octal_number(self):
36+
self.assertEqual(label(["black", "grey", "black"]), "8 ohms")
37+
38+
def test_ignore_extra_colors(self):
39+
self.assertEqual(label(["blue", "green", "yellow", "orange"]), "650 kiloohms")

0 commit comments

Comments
 (0)