Skip to content

Commit be11464

Browse files
committed
Card-Games Improvements
1 parent ff8ef35 commit be11464

4 files changed

Lines changed: 79 additions & 100 deletions

File tree

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,39 @@
33
Elyse is really looking forward to playing some poker (and other card games) during her upcoming trip to Vegas.
44
Being a big fan of "self-tracking" she wants to put together some small functions that will help her with tracking tasks and has asked for your help thinking them through.
55

6-
76
## 1. Tracking Poker Rounds
87

98
Elyse is especially fond of poker, and wants to track how many rounds she plays - and _which rounds_ those are.
109
Every round has its own number, and every table shows the round number currently being played.
1110
Elyse chooses a table and sits down to play her first round. She plans on playing three rounds.
1211

13-
1412
Implement a function `get_rounds(<round_number>)` that takes the current round number and returns a single `list` with that round and the _next two_ that are coming up:
1513

16-
1714
```python
1815
>>> get_rounds(27)
1916
[27, 28, 29]
2017
```
2118

22-
2319
## 2. Keeping all Rounds in the Same Place
2420

2521
Elyse played a few rounds at the first table, then took a break and played some more rounds at a second table ... but ended up with a different list for each table!
2622
She wants to put the two lists together, so she can track all of the poker rounds in the same place.
2723

28-
2924
Implement a function `concatenate_rounds(<rounds_1>, <rounds_2>)` that takes two lists and returns a single `list` consisting of all the rounds in the first `list`, followed by all the rounds in the second `list`:
3025

31-
3226
```python
3327
>>> concatenate_rounds([27, 28, 29], [35, 36])
3428
[27, 28, 29, 35, 36]
3529
```
3630

37-
3831
## 3. Finding Prior Rounds
3932

4033
Talking about some of the prior Poker rounds, another player remarks how similarly two of them played out.
4134
Elyse is not sure if she played those rounds or not.
4235

43-
4436
Implement a function `list_contains_round(<rounds>, <round_number>)` that takes two arguments, a list of rounds played and a round number.
4537
The function will return `True` if the round is in the list of rounds played, `False` if not:
4638

47-
4839
```python
4940
>>> list_contains_round([27, 28, 29, 35, 36], 29)
5041
True
@@ -59,7 +50,6 @@ Elyse wants to try out a new game called Black Joe.
5950
It's similar to Black Jack - where your goal is to have the cards in your hand add up to a target value - but in Black Joe the goal is to get the _average_ of the card values to be 7.
6051
The average can be found by summing up all the card values and then dividing that sum by the number of cards in the hand.
6152

62-
6353
Implement a function `card_average(<hand>)` that will return the average value of a hand of Black Joe.
6454

6555
```python
@@ -69,16 +59,18 @@ Implement a function `card_average(<hand>)` that will return the average value o
6959

7060
## 5. Alternate Averages
7161

72-
In Black Joe, speed is important.
73-
Elyse thinks she can "shortcut" the traditional calculation to get an _average-like_ number by taking the average of the first and last card values in the hand - or by using the "middle" card value (the median) of the hand.
74-
She is hoping one of these calculations might come close to the average, but be quicker to calculate during game play. She'd like you to create a function to test her theory out.
62+
In Black Joe, speed is important. Elyse is going to try and find a faster way of finding the average.
7563

64+
She has thought of two ways of getting an _average-like_ number:
7665

77-
Implement a function `approx_average_is_average(<hand>)` that returns a Boolean.
78-
The function should return `True` if _one (or both)_ of Elyse's approximations yields the same value as calculating the "full" average of a hand.
79-
The function should return `False` if _neither_ approximation formula yields the same value as the "full" average.
80-
For the sake of a simple median, we are going to assume the hand always has an odd number of card values.
66+
- Take the average of the _first_ and _last_ number in the hand.
67+
- Using the median (middle card) of the hand.
68+
69+
Implement the function `approx_average_is_average(<hand>)`, given `hand`, a list containing the values of the cards in your hand.
8170

71+
Return `True` if either _one_ `or` _both_ of the, above named, strategies result in a number _equal_ to the _actual average_.
72+
73+
Note: _The length of all hands are odd, to make finding a median easier._
8274

8375
```python
8476
>>> approx_average_is_average([1, 2, 3])
@@ -98,7 +90,6 @@ Intrigued by the results of her averaging experiment, Elyse is wondering if taki
9890

9991
Implement a function `average_even_is_average_odd(<hand>)` that returns a Boolean indicating if the average of the cards at even indexes is the same as the average of the cards at odd indexes.
10092

101-
10293
```python
10394
>>> average_even_is_average_odd([1, 2, 3])
10495
True
@@ -107,16 +98,13 @@ True
10798
False
10899
```
109100

110-
111101
## 7. Bonus Round Rules
112102

113103
Every 11th hand in Black Joe is a bonus hand with a bonus rule: if the last card you draw is a Jack, you double its value.
114104

115-
116105
Implement a function `maybe_double_last(<hand>)` that takes a hand and checks if the last card is a Jack (11).
117106
If the the last card **is** a Jack (11), double its value before returning the hand.
118107

119-
120108
```python
121109
>>> hand = [5, 9, 11]
122110
>>> maybe_double_last(hand)

exercises/concept/card-games/.meta/exemplar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def approx_average_is_average(hand):
4444
"""
4545
4646
:param hand: list - cards in hand.
47-
:return: bool - is approximate average the same as true average?
47+
:return: bool - if approximate average equals to the `true average`.
4848
"""
4949

5050
real_average = card_average(hand)

exercises/concept/card-games/lists.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def approx_average_is_average(hand):
4444
"""
4545
4646
:param hand: list - cards in hand.
47-
:return: bool - is approximate average the same as true average?
47+
:return: bool - if approximate average equals to the `true average`.
4848
"""
4949

5050
pass

exercises/concept/card-games/lists_test.py

Lines changed: 68 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,108 +15,99 @@ class CardGamesTest(unittest.TestCase):
1515

1616
@pytest.mark.task(taskno=1)
1717
def test_get_rounds(self):
18-
data = [
19-
(0, [0, 1, 2]),
20-
(1, [1, 2, 3]),
21-
(10, [10, 11, 12]),
22-
(27, [27, 28, 29]),
23-
(99, [99, 100, 101]),
24-
(666, [666, 667, 668]),
25-
]
26-
27-
for variant, (number, rounds) in enumerate(data, start=1):
18+
19+
input_vars = [0, 1, 10, 27, 99, 666]
20+
21+
results = [[0, 1, 2], [1, 2, 3],
22+
[10, 11, 12], [27, 28, 29],
23+
[99, 100, 101], [666, 667, 668]]
24+
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}.'
2827
with self.subTest(f'variation #{variant}', input=number, output=rounds):
29-
error_message = f'Expected rounds {rounds} given the current round {number}.'
3028
self.assertEqual(rounds, get_rounds(number), msg=error_message)
3129

30+
3231
@pytest.mark.task(taskno=2)
3332
def test_concatenate_rounds(self):
34-
data = [
35-
(([], []), []),
36-
(([0, 1], []), [0, 1]),
37-
(([], [1, 2]), [1, 2]),
38-
(([1], [2]), [1, 2]),
39-
(([27, 28, 29], [35, 36]), [27, 28, 29, 35, 36]),
40-
(([1, 2, 3], [4, 5, 6]), [1, 2, 3, 4, 5, 6]),
41-
]
42-
43-
for variant, ((rounds_1, rounds_2), rounds) in enumerate(data, start=1):
33+
34+
input_vars = [([], []), ([0, 1], []), ([], [1, 2]),
35+
([1], [2]), ([27, 28, 29], [35, 36]),
36+
([1, 2, 3], [4, 5, 6])]
37+
38+
results = [[], [0, 1], [1, 2], [1, 2],
39+
[27, 28, 29, 35, 36],
40+
[1, 2, 3, 4, 5, 6]]
41+
42+
for variant, ((rounds_1, rounds_2), rounds) in enumerate(zip(input_vars, results), start=1):
43+
error_message = f'Expected {rounds} as the concatenation of {rounds_1} and {rounds_2}.'
4444
with self.subTest(f'variation #{variant}', input=(rounds_1, rounds_2), output=rounds):
45-
error_message = f'Expected {rounds} as the concatenation of {rounds_1} and {rounds_2}.'
4645
self.assertEqual(rounds, concatenate_rounds(rounds_1, rounds_2), msg=error_message)
4746

47+
4848
@pytest.mark.task(taskno=3)
4949
def test_list_contains_round(self):
50-
data = [
51-
(([], 1), False),
52-
(([1, 2, 3], 0), False),
53-
(([27, 28, 29, 35, 36], 30), False),
54-
(([1], 1), True),
55-
(([1, 2, 3], 1), True),
56-
(([27, 28, 29, 35, 36], 29), True),
57-
]
58-
59-
for variant, ((rounds, round_number), contains) in enumerate(data, start=1):
50+
51+
input_vars = [([], 1), ([1, 2, 3], 0), ([27, 28, 29, 35, 36], 30),
52+
([1], 1), ([1, 2, 3], 1), ([27, 28, 29, 35, 36], 29)]
53+
54+
results = [False, False, False, True, True, True]
55+
56+
for variant, ((rounds, round_number), contains) in enumerate(zip(input_vars, results), start=1):
57+
error_message = f'Round {round_number} {"is" if contains else "is not"} in {rounds}.'
6058
with self.subTest(f'variation #{variant}', input=(rounds, round_number), output=contains):
61-
error_message = f'Round {round_number} {"is" if contains else "is not"} in {rounds}.'
62-
self.assertEqual(contains,list_contains_round(rounds, round_number),msg=error_message)
59+
self.assertEqual(contains, list_contains_round(rounds, round_number), msg=error_message)
60+
6361

6462
@pytest.mark.task(taskno=4)
6563
def test_card_average(self):
66-
data = [
67-
([1], 1.0),
68-
([5, 6, 7], 6.0),
69-
([1, 2, 3, 4], 2.5),
70-
([1, 10, 100], 37.0),
71-
]
72-
73-
for variant, (hand, average) in enumerate(data, start=1):
64+
65+
input_vars = [[1], [5, 6, 7], [1, 2, 3, 4], [1, 10, 100]]
66+
67+
results = [1.0, 6.0, 2.5, 37.0]
68+
69+
for variant, (hand, average) in enumerate(zip(input_vars, results), start=1):
70+
error_message=f'Expected {average} as the average of {hand}.'
7471
with self.subTest(f'variation #{variant}', input=hand, output=average):
75-
msg=f'Expected {average} as the average of {hand}.'
76-
self.assertEqual(average,card_average(hand),msg=msg)
72+
self.assertEqual(average, card_average(hand), msg=error_message)
73+
7774

7875
@pytest.mark.task(taskno=5)
7976
def test_approx_average_is_average(self):
80-
data = [
81-
([0, 1, 5], False),
82-
([3, 6, 9, 12, 150], False),
83-
([1, 2, 3, 5, 9], False),
84-
([2, 3, 4, 7, 8], False),
85-
([1, 2, 3], True),
86-
([2, 3, 4], True),
87-
([2, 3, 4, 8, 8], True),
88-
([1, 2, 4, 5, 8], True),
89-
]
90-
91-
for variant, (hand, same) in enumerate(data, start=1):
77+
78+
input_vars = [[0, 1, 5], [3, 6, 9, 12, 150], [1, 2, 3, 5, 9],
79+
[2, 3, 4, 7, 8], [1, 2, 3], [2, 3, 4],
80+
[2, 3, 4, 8, 8], [1, 2, 4, 5, 8]]
81+
82+
results = [False, False, False, False, True, True, True, True]
83+
84+
for variant, (hand, same) in enumerate(zip(input_vars, results), start=1):
85+
error_message = f'Hand {hand} {"does" if same else "does not"} yield the same approximate average.'
9286
with self.subTest(f'variation #{variant}', input=hand, output=same):
93-
error_message = f'Hand {hand} {"does" if same else "does not"} yield the same approximate average.'
9487
self.assertEqual(same, approx_average_is_average(hand), msg=error_message)
9588

89+
9690
@pytest.mark.task(taskno=6)
9791
def test_average_even_is_average_odd(self):
98-
data = [
99-
([5, 6, 8], False),
100-
([1, 2, 3, 4], False),
101-
([1, 2, 3], True),
102-
([5, 6, 7], True),
103-
]
104-
105-
for variant, (hand, same) in enumerate(data, start=1):
92+
93+
input_vars = [[5, 6, 8], [1, 2, 3, 4], [1, 2, 3], [5, 6, 7], ]
94+
95+
results = [False, False, True, True]
96+
97+
for variant, (hand, same) in enumerate(zip(input_vars, results), start=1):
98+
error_message=f'Hand {hand} {"does" if same else "does not"} yield the same odd-even average.'
10699
with self.subTest(f'variation #{variant}', input=hand, output=same):
107-
msg=f'Hand {hand} {"does" if same else "does not"} yield the same odd-even average.'
108-
self.assertEqual(same, average_even_is_average_odd(hand),msg=msg)
100+
self.assertEqual(same, average_even_is_average_odd(hand),msg=error_message)
101+
109102

110103
@pytest.mark.task(taskno=7)
111104
def test_maybe_double_last(self):
112-
data = [
113-
([1, 2, 11], [1, 2, 22]),
114-
([5, 9, 11], [5, 9, 22]),
115-
([5, 9, 10], [5, 9, 10]),
116-
([1, 2, 3], [1, 2, 3]),
117-
]
118-
119-
for variant, (hand, doubled_hand) in enumerate(data, start=1):
105+
106+
input_vars = [[1, 2, 11], [5, 9, 11], [5, 9, 10], [1, 2, 3]]
107+
108+
results = [[1, 2, 22], [5, 9, 22], [5, 9 , 10], [1, 2, 3]]
109+
110+
for variant, (hand, doubled_hand) in enumerate(zip(input_vars, results), start=1):
111+
error_message=f'Expected {doubled_hand} as the maybe-doubled version of {hand}.'
120112
with self.subTest(f'variation #{variant}', input=hand, output=doubled_hand):
121-
msg=f'Expected {doubled_hand} as the maybe-doubled version of {hand}.'
122-
self.assertEqual(doubled_hand,maybe_double_last(hand),msg=msg)
113+
self.assertEqual(doubled_hand,maybe_double_last(hand),msg=error_message)

0 commit comments

Comments
 (0)