Skip to content

Commit 3445ffd

Browse files
authored
Edited test error messages to include function calls and params. Edited docs for spelling and small grammar fixes. (#3528)
[no important files changed]
1 parent 31ced03 commit 3445ffd

File tree

4 files changed

+101
-68
lines changed

4 files changed

+101
-68
lines changed

concepts/lists/about.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Accessing elements, checking for membership via `in`, or appending items to the
1818
For a similar data structure that supports memory efficient `appends`/`pops` from both sides, see [`collections.deque`][deque], which has approximately the same O(1) performance in either direction.
1919

2020

21-
Because lists are mutable and can contain references to arbitrary objects, they also take up more space in memory than a fixed-size [`array.array`][array.array] type of the same apparent length.
21+
Because lists are mutable and can contain references to arbitrary Python objects, they also take up more space in memory than an [`array.array`][array.array] or a [`tuple`][tuple] (_which is immutable_) of the same apparent length.
2222
Despite this, lists are an extremely flexible and useful data structure and many built-in methods and operations in Python produce lists as their output.
2323

2424

@@ -135,7 +135,8 @@ TypeError: 'int' object is not iterable
135135

136136
## Accessing elements
137137

138-
Items inside lists (_as well as elements in other sequence types such as [`str`][string] & [`tuple`][tuple]_), can be accessed using _bracket notation_. Indexes can be from **`left`** --> **`right`** (_starting at zero_) or **`right`** --> **`left`** (_starting at -1_).
138+
Items inside lists (_as well as elements in other sequence types such as [`str`][string] & [`tuple`][tuple]_), can be accessed using _bracket notation_.
139+
Indexes can be from **`left`** --> **`right`** (_starting at zero_) or **`right`** --> **`left`** (_starting at -1_).
139140

140141

141142
<table>
@@ -173,9 +174,11 @@ Items inside lists (_as well as elements in other sequence types such as [`str`]
173174
'Toast'
174175
```
175176

176-
A section of a list can be accessed via _slice notation_ (`<list>[start:stop]`). A _slice_ is defined as an element sequence at position `index`, such that `start <= index < stop`. [_Slicing_][slice notation] returns a copy of the "sliced" items and does not modify the original `list`.
177+
A section of a list can be accessed via _slice notation_ (`<list>[start:stop]`).
178+
A _slice_ is defined as an element sequence at position `index`, such that `start <= index < stop`.
179+
[_Slicing_][slice notation] returns a copy of the "sliced" items and does not modify the original `list`.
177180

178-
A `step` parameter can also be used in the slice (`[start:stop:step]`) to "skip over" or filter the returned elements (_for example, a `step` of 2 will select every other element in the section_):
181+
A `step` parameter can also be used in the slice (`<list>[<start>:<stop>:<step>]`) to "skip over" or filter the returned elements (_for example, a `step` of 2 will select every other element in the section_):
179182

180183
```python
181184
>>> colors = ["Red", "Purple", "Green", "Yellow", "Orange", "Pink", "Blue", "Grey"]
@@ -269,7 +272,7 @@ Lists can also be combined via various techniques:
269272
>>> first_one
270273
['George', 5, 'cat', 'Tabby']
271274

272-
# This loops through the first list and appends it's items to the end of the second list.
275+
# This loops through the first list and appends its items to the end of the second list.
273276
>>> first_one = ["cat", "Tabby"]
274277
>>> second_one = ["George", 5]
275278

@@ -284,7 +287,7 @@ Lists can also be combined via various techniques:
284287
## Some cautions
285288

286289
Recall that variables in Python are _labels_ that point to _underlying objects_.
287-
`lists` add one more layer as _container objects_ -- they hold object references for their collected items.
290+
`lists` add one more layer as _container objects_ -- they hold object _references_ for their collected items.
288291
This can lead to multiple potential issues when working with lists, if not handled properly.
289292

290293

@@ -305,21 +308,22 @@ A `shallow_copy` will create a new `list` object, but **will not** create new ob
305308

306309
# Altering the list via the new name is the same as altering the list via the old name.
307310
>>> same_list.append("Clarke")
308-
>>> same_list
309311
["Tony", "Natasha", "Thor", "Bruce", "Clarke"]
312+
310313
>>> actual_names
311314
["Tony", "Natasha", "Thor", "Bruce", "Clarke"]
312315

313316
# Likewise, altering the data in the list via the original name will also alter the data under the new name.
314317
>>> actual_names[0] = "Wanda"
315-
>>> same_list
316318
['Wanda', 'Natasha', 'Thor', 'Bruce', 'Clarke']
317319

318320
# If you copy the list, there will be two separate list objects which can be changed independently.
319321
>>> copied_list = actual_names.copy()
320322
>>> copied_list[0] = "Tony"
323+
321324
>>> actual_names
322325
['Wanda', 'Natasha', 'Thor', 'Bruce', 'Clarke']
326+
323327
>>> copied_list
324328
["Tony", "Natasha", "Thor", "Bruce", "Clarke"]
325329
```
@@ -455,4 +459,4 @@ The collections module also provides a `UserList` type that can be customized to
455459
[set]: https://docs.python.org/3/library/stdtypes.html#set
456460
[slice notation]: https://docs.python.org/3/reference/expressions.html#slicings
457461
[string]: https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str
458-
[tuple]: https://docs.python.org/3/library/stdtypes.html#tuple
462+
[tuple]: https://docs.python.org/3/library/stdtypes.html#tuple

exercises/concept/card-games/.docs/hints.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,44 @@
44

55
## 1. Tracking Poker Rounds
66

7-
- Lists in Python may be [constructed][constructed] in several ways.
7+
- Lists in Python may be [constructed][constructed] in multiple ways.
88
- This function should [return][return] a `list`.
99

1010
## 2. Keeping all Rounds in the Same Place
1111

12-
- Sequence types such as `list` already support [common operations][common sequence operations].
12+
- Sequence types such as `list` support [common operations][common sequence operations].
1313
- This function should [return][return] a `list`.
1414

1515
## 3. Finding Prior Rounds
1616

17-
- Sequence types such as `list` already support a few [common operations][common sequence operations].
17+
- Sequence types such as `list` support a few [common operations][common sequence operations].
1818
- This function should [return][return] a `bool`.
1919

2020
## 4. Averaging Card Values
2121

22-
- To get the average, this function should count how many items are in the `list` and sum up their values. Then, return sum/count.
22+
- To get the average, this function should count how many items are in the `list` and sum up their values. Then, return the sum divided by the count.
2323

2424
## 5. Alternate Averages
2525

26-
- Sequence types such as `list` already support a few [common operations][common sequence operations].
27-
- To access an element use the square brackets (`<list>[]`) notation.
28-
- Remember that the first element of the `list` is at index 0 from the left.
29-
- In Python, negative indexing starts the count from the right-hand side. This mean that you can find the last element of a `list` at `index -1`.
26+
- Sequence types such as `list` support a few [common operations][common sequence operations].
27+
- To access an element, use the square brackets (`<list>[]`) notation.
28+
- Remember that the first element of the `list` is at index 0 from the **left-hand** side.
29+
- In Python, negative indexing starts at -1 from the **right-hand** side. This means that you can find the last element of a `list` by using `<list>[-1]`.
3030
- Think about how you could reuse the code from the functions that you have already implemented.
3131

3232
## 6. More Averaging Techniques
3333

3434
- Sequence types such as `list` already support a few [common operations][common sequence operations].
3535
- Think about reusing the code from the functions that you just implemented.
36-
- The slice syntax supports a step value.
36+
- The slice syntax supports a _step value_ (`<list>[<start>:<stop>:<step>]`).
3737

3838
## 7. Bonus Round Rules
3939

40-
- Lists are mutable. Once a `list` is created, you can modify, delete or add any type of element you wish.
40+
- Lists are _mutable_. Once a `list` is created, you can modify, delete or add any type of element you wish.
4141
- Python provides a wide range of [ways to modify `lists`][ways to modify `lists`].
4242

4343

4444
[common sequence operations]: https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range
4545
[constructed]: https://docs.python.org/3/library/stdtypes.html#list
46-
[iterate over a list in python]: https://www.geeksforgeeks.org/iterate-over-a-list-in-python/
4746
[return]: https://www.w3schools.com/python/ref_keyword_return.asp
4847
[ways to modify `lists`]: https://realpython.com/python-lists-tuples/#lists-are-mutable

exercises/concept/card-games/.docs/introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A [`list`][list] is a mutable collection of items in _sequence_.
44
Like most collections (_see the built-ins [`tuple`][tuple], [`dict`][dict] and [`set`][set]_), lists can hold reference to any (or multiple) data type(s) - including other lists.
5-
Like any [sequence][sequence type], items can be accessed via `0-based index` number from the left and `-1-base index` from the right.
5+
Like any [sequence][sequence type], items can be accessed via `0-based index` number from the left and `-1-based index` from the right.
66
Lists can be copied in whole or in part via [slice notation][slice notation] or `<list>.copy()`.
77

88
Lists support both [common][common sequence operations] and [mutable][mutable sequence operations] sequence operations such as `min()`/`max()`, `<list>.index()`, `<list>.append()` and `<list>.reverse()`.
+77-47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import pytest
3+
34
from lists import (
45
get_rounds,
56
concatenate_rounds,
@@ -16,92 +17,121 @@ class CardGamesTest(unittest.TestCase):
1617
@pytest.mark.task(taskno=1)
1718
def test_get_rounds(self):
1819

19-
input_vars = [0, 1, 10, 27, 99, 666]
20+
input_data = [0, 1, 10, 27, 99, 666]
21+
result_data = [[0, 1, 2], [1, 2, 3],
22+
[10, 11, 12], [27, 28, 29],
23+
[99, 100, 101], [666, 667, 668]]
2024

21-
results = [[0, 1, 2], [1, 2, 3],
22-
[10, 11, 12], [27, 28, 29],
23-
[99, 100, 101], [666, 667, 668]]
25+
for variant, (number, expected) in enumerate(zip(input_data, result_data), start=1):
26+
with self.subTest(f'variation #{variant}', number=number, expected=expected):
27+
actual_result = get_rounds(number)
28+
error_message = (f'Called get_rounds({number}). '
29+
f'The function returned {actual_result}, '
30+
f'but the tests expected rounds {expected} '
31+
f'given the current round {number}.')
2432

25-
for variant, (number, rounds) in enumerate(zip(input_vars, results), start=1):
26-
error_message = f'Expected rounds {rounds} given the current round {number}.'
27-
with self.subTest(f'variation #{variant}', input=number, output=rounds):
28-
self.assertEqual(rounds, get_rounds(number), msg=error_message)
33+
self.assertEqual(actual_result, expected, msg=error_message)
2934

3035
@pytest.mark.task(taskno=2)
3136
def test_concatenate_rounds(self):
3237

33-
input_vars = [([], []), ([0, 1], []), ([], [1, 2]),
38+
input_data = [([], []), ([0, 1], []), ([], [1, 2]),
3439
([1], [2]), ([27, 28, 29], [35, 36]),
3540
([1, 2, 3], [4, 5, 6])]
3641

37-
results = [[], [0, 1], [1, 2], [1, 2],
38-
[27, 28, 29, 35, 36],
39-
[1, 2, 3, 4, 5, 6]]
42+
result_data = [[], [0, 1], [1, 2], [1, 2],
43+
[27, 28, 29, 35, 36],
44+
[1, 2, 3, 4, 5, 6]]
45+
46+
for variant, ((rounds_1, rounds_2), expected) in enumerate(zip(input_data, result_data), start=1):
47+
with self.subTest(f'variation #{variant}', rounds_1=rounds_1, rounds_2=rounds_2, expected=expected):
48+
actual_result = concatenate_rounds(rounds_1, rounds_2)
49+
error_message = (f'Called concatenate_rounds({rounds_1}, {rounds_2}). '
50+
f'The function returned {actual_result}, but the tests '
51+
f'expected {expected} as the concatenation '
52+
f'of {rounds_1} and {rounds_2}.')
4053

41-
for variant, ((rounds_1, rounds_2), rounds) in enumerate(zip(input_vars, results), start=1):
42-
error_message = f'Expected {rounds} as the concatenation of {rounds_1} and {rounds_2}.'
43-
with self.subTest(f'variation #{variant}', input=(rounds_1, rounds_2), output=rounds):
44-
self.assertEqual(rounds, concatenate_rounds(rounds_1, rounds_2), msg=error_message)
54+
self.assertEqual(actual_result, expected, msg=error_message)
4555

4656
@pytest.mark.task(taskno=3)
4757
def test_list_contains_round(self):
4858

49-
input_vars = [([], 1), ([1, 2, 3], 0), ([27, 28, 29, 35, 36], 30),
50-
([1], 1), ([1, 2, 3], 1), ([27, 28, 29, 35, 36], 29)]
59+
input_data = [([], 1), ([1, 2, 3], 0),
60+
([27, 28, 29, 35, 36], 30),
61+
([1], 1), ([1, 2, 3], 1),
62+
([27, 28, 29, 35, 36], 29)]
63+
result_data = [False, False, False, True, True, True]
5164

52-
results = [False, False, False, True, True, True]
65+
for variant, ((rounds, round_number), expected) in enumerate(zip(input_data, result_data), start=1):
66+
with self.subTest(f'variation #{variant}', rounds=rounds, round_number=round_number, expected=expected):
67+
actual_result = list_contains_round(rounds, round_number)
68+
error_message = (f'Called list_contains_round({rounds}, {round_number}). '
69+
f'The function returned {actual_result}, but round {round_number} '
70+
f'{"is" if expected else "is not"} in {rounds}.')
5371

54-
for variant, ((rounds, round_number), contains) in enumerate(zip(input_vars, results), start=1):
55-
error_message = f'Round {round_number} {"is" if contains else "is not"} in {rounds}.'
56-
with self.subTest(f'variation #{variant}', input=(rounds, round_number), output=contains):
57-
self.assertEqual(contains, list_contains_round(rounds, round_number), msg=error_message)
72+
self.assertEqual(actual_result, expected, msg=error_message)
5873

5974
@pytest.mark.task(taskno=4)
6075
def test_card_average(self):
6176

62-
input_vars = [[1], [5, 6, 7], [1, 2, 3, 4], [1, 10, 100]]
77+
input_data = [[1], [5, 6, 7], [1, 2, 3, 4], [1, 10, 100]]
78+
result_data = [1.0, 6.0, 2.5, 37.0]
6379

64-
results = [1.0, 6.0, 2.5, 37.0]
80+
for variant, (hand, expected) in enumerate(zip(input_data, result_data), start=1):
81+
with self.subTest(f'variation #{variant}', hand=hand, expected=expected):
82+
actual_result = card_average(hand)
83+
error_message = (f'Called card_average({hand}). '
84+
f'The function returned {actual_result}, but '
85+
f'the tests expected {expected} as the average of {hand}.')
6586

66-
for variant, (hand, average) in enumerate(zip(input_vars, results), start=1):
67-
error_message = f'Expected {average} as the average of {hand}.'
68-
with self.subTest(f'variation #{variant}', input=hand, output=average):
69-
self.assertEqual(average, card_average(hand), msg=error_message)
87+
self.assertEqual(actual_result, expected, msg=error_message)
7088

7189
@pytest.mark.task(taskno=5)
7290
def test_approx_average_is_average(self):
7391

74-
input_vars = [[0, 1, 5], [3, 6, 9, 12, 150], [1, 2, 3, 5, 9],
92+
input_data = [[0, 1, 5], [3, 6, 9, 12, 150], [1, 2, 3, 5, 9],
7593
[2, 3, 4, 7, 8], [1, 2, 3], [2, 3, 4],
7694
[2, 3, 4, 8, 8], [1, 2, 4, 5, 8]]
7795

78-
results = [False, False, False, False, True, True, True, True]
96+
result_data = [False, False, False, False, True, True, True, True]
97+
98+
for variant, (hand, expected) in enumerate(zip(input_data, result_data), start=1):
99+
with self.subTest(f'variation #{variant}', hand=hand, expected=expected):
100+
actual_result = approx_average_is_average(hand)
101+
error_message = (f'Called approx_average_is_average({hand}). '
102+
f'The function returned {actual_result}, but '
103+
f'the hand {hand} {"does" if expected else "does not"} '
104+
f'yield the same approximate average.')
79105

80-
for variant, (hand, same) in enumerate(zip(input_vars, results), start=1):
81-
error_message = f'Hand {hand} {"does" if same else "does not"} yield the same approximate average.'
82-
with self.subTest(f'variation #{variant}', input=hand, output=same):
83-
self.assertEqual(same, approx_average_is_average(hand), msg=error_message)
106+
self.assertEqual(actual_result, expected, msg=error_message)
84107

85108
@pytest.mark.task(taskno=6)
86109
def test_average_even_is_average_odd(self):
87110

88-
input_vars = [[5, 6, 8], [1, 2, 3, 4], [1, 2, 3], [5, 6, 7], [1, 3, 5, 7, 9]]
111+
input_data = [[5, 6, 8], [1, 2, 3, 4], [1, 2, 3], [5, 6, 7], [1, 3, 5, 7, 9]]
112+
result_data = [False, False, True, True, True]
89113

90-
results = [False, False, True, True, True]
114+
for variant, (input_hand, expected) in enumerate(zip(input_data, result_data), start=1):
115+
with self.subTest(f'variation #{variant}', input_hand=input_hand, expected=expected):
116+
actual_result = average_even_is_average_odd(input_hand)
117+
error_message = (f'Called average_even_is_average_odd({input_hand}). '
118+
f'The function returned {actual_result}, but '
119+
f'the hand {"does" if expected else "does not"} '
120+
f'yield the same odd-even average.')
91121

92-
for variant, (hand, same) in enumerate(zip(input_vars, results), start=1):
93-
error_message = f'Hand {hand} {"does" if same else "does not"} yield the same odd-even average.'
94-
with self.subTest(f'variation #{variant}', input=hand, output=same):
95-
self.assertEqual(same, average_even_is_average_odd(hand), msg=error_message)
122+
self.assertEqual(actual_result, expected, msg=error_message)
96123

97124
@pytest.mark.task(taskno=7)
98125
def test_maybe_double_last(self):
99126

100-
input_vars = [[1, 2, 11], [5, 9, 11], [5, 9, 10], [1, 2, 3]]
127+
input_data = [(1, 2, 11), (5, 9, 11), (5, 9, 10), (1, 2, 3), (1, 11, 8)]
128+
result_data = [[1, 2, 22], [5, 9, 22], [5, 9, 10], [1, 2, 3], [1, 11, 8]]
101129

102-
results = [[1, 2, 22], [5, 9, 22], [5, 9, 10], [1, 2, 3]]
130+
for variant, (hand, expected) in enumerate(zip(input_data, result_data), start=1):
131+
with self.subTest(f'variation #{variant}', hand=list(hand), expected=expected):
132+
actual_result = maybe_double_last(list(hand))
133+
error_message = (f'Called maybe_double_last({list(hand)}). '
134+
f'The function returned {actual_result}, but '
135+
f'the tests expected {expected} as the maybe-doubled version of {list(hand)}.')
103136

104-
for variant, (hand, doubled_hand) in enumerate(zip(input_vars, results), start=1):
105-
error_message = f'Expected {doubled_hand} as the maybe-doubled version of {hand}.'
106-
with self.subTest(f'variation #{variant}', input=hand, output=doubled_hand):
107-
self.assertEqual(doubled_hand, maybe_double_last(hand), msg=error_message)
137+
self.assertEqual(actual_result, expected, msg=error_message)

0 commit comments

Comments
 (0)