Skip to content

Commit

Permalink
Redo elyses-enchantments/lists.py as card-games
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Good authored and BethanyG committed May 12, 2021
1 parent 40eec70 commit 3cd390f
Show file tree
Hide file tree
Showing 12 changed files with 521 additions and 504 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,42 @@

## General

## 1. Creating a List
## 1. Tracking Poker Rounds

- `Lists` in python may be [constructed](https://docs.python.org/3/library/stdtypes.html#list) in several ways.
- This function should [return](https://www.w3schools.com/python/ref_keyword_return.asp) a `list`.

## 2. Creating a copy of a List

- `Lists` can be [nested](https://realpython.com/python-lists-tuples/#lists-can-be-nested), this means that a `list` can be an element of another `list`.
- This function should [return](https://www.w3schools.com/python/ref_keyword_return.asp) a `list` containing two `lists`.

## 3. Concatenating Lists
## 2. Keeping all Rounds in the Same Place

- Sequence types such as `list` already support a few [common operations](https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range)
- This function should [return](https://www.w3schools.com/python/ref_keyword_return.asp) a `list`.

## 4. Testing List Membership
## 3. Finding Prior Rounds

- Sequence types such as `list` already support a few [common operations](https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range)
- This function should [return](https://www.w3schools.com/python/ref_keyword_return.asp) a `boolean`.

## 5. Accessing List Elements by Index
## 4. Averaging Card Values

- There are a few techniques to [iterate over a `list` in python](https://www.geeksforgeeks.org/iterate-over-a-list-in-python/).
- To get the average, this function should count how many items are in the list and sum up their values. Then, return sum divided by count.

## 5. Alternate Averages

- Sequence types such as `list` already support a few [common operations](https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range)
- To access an element use the square brackets notation.
- Remember that the first element of the `list` is at index 0
- In python, negative indexing starts the count from the end. This mean that you can find the last element of the `list` at index -1.
- This function should [return](https://www.w3schools.com/python/ref_keyword_return.asp) a `list` containing two elements.
- Think about reusing the code from the functions that you just implemented.

## 6. Accessing Sublists by Slicing
## 6. More Averaging Techniques

- Sequence types such as `list` already support a few [common operations](https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range)
- For the last part of the exercise, think about reusing the code from the functions that you just implemented.
- These functions should [return](https://www.w3schools.com/python/ref_keyword_return.asp) a `list`.
- Think about reusing the code from the functions that you just implemented.
- The slice syntax supports a step value.

## 7. Iterating Over List Items

- There are a few techniques to [iterate over a `list` in python](https://www.geeksforgeeks.org/iterate-over-a-list-in-python/).
- You should use the function `print` on each element of the `list` to print each of them on a separate line.
- This function should not [return](https://www.w3schools.com/python/ref_keyword_return.asp) anything so you should end it with just the statement `return`.

## 8. Creating Lists with Mixed Data Types

- There are many [built-in types](https://docs.python.org/3/library/stdtypes.html) in python.
- [`Lists` Can Contain Arbitrary Objects](https://realpython.com/python-lists-tuples/#lists-can-contain-arbitrary-objects).
- This function should [return](https://www.w3schools.com/python/ref_keyword_return.asp) a `list`.

## 9. Modifying Values in Lists
## 7. Bonus Round Rules

- `Lists` in python are mutable, this means that once a `list` is created, you can modify, delete or add any element as you wish.
- Python provides a wide range of [ways to modify `lists`](https://realpython.com/python-lists-tuples/#lists-are-mutable).
- This function should not [return](https://www.w3schools.com/python/ref_keyword_return.asp) anything so you should end it with the statement `return` alone.
- This function should not [return](https://www.w3schools.com/python/ref_keyword_return.asp) anything so you should end it with just the statement `return`.
101 changes: 101 additions & 0 deletions exercises/concept/card-games/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Instructions

## Instructions

Elyse is really looking forward to playing some poker (and other card games) during her upcoming trip to Vegas. 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.

## 1. Tracking Poker Rounds

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

Implement a function `to_rounds` that takes the current round number and returns a single list with that round and the _next two_ that are coming up:

```python
>>> to_rounds(27)
[27, 28, 29]
```

## 2. Keeping all Rounds in the Same Place

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! She wants to put the two lists together, so she can track all of the poker rounds in the same place.

Implement a function `concatenate_rounds` 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:

```python
>>> concatenate_rounds([27, 28, 29], [35, 36])
[27, 28, 29, 35, 36]
```

## 3. Finding Prior Rounds

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

Implement a function `list_contains_round` that takes two arguments, a list of rounds played and a round number. The function will return `True` if the round is in the list of rounds played, `False` if not:

```python
>>> list_contains_round([27, 28, 29, 35, 36], 29)
True

>>> list_contains_round([27, 28, 29, 35, 36], 30)
False
```

## 4. Averaging Card Values

Elyse wants to try out a new game called Black Joe. 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. 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.

Implement a function `card_average` that will return the average value of a hand of Black Joe.

```python
>>> card_average([5, 6, 7])
6.0
```

## 5. Alternate Averages

In Black Joe, speed is important. 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. She's 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.

Implement a function `approx_average_is_average` that returns a boolean, indicating if either of Elyse's approximation formulas is the same as calculating the "full" average of a hand. For the sake of a simple median, we are going to assume the hand always has an odd number of card values.

```python
>>> approx_average_is_average([1, 2, 3])
True

>>> approx_average_is_average([2, 3, 4, 8, 8])
True

>>> approx_average_is_average([1, 2, 3, 5, 9])
False
```

## 6. More Averaging Techniques

Intrigued by the results of her averaging experiment, Elyse is wondering if taking the average of _even_ values versus the average of _odd_ values would give the same results. Time for another test function!

Implement a function `average_even_is_average_odd` that returns a boolean indicating of the average of the even valued cards is the same as the average of the odd valued cards.

```python
>>> average_even_is_average_odd([1, 2, 3])
True

>>> average_even_is_average_odd([1, 2, 3, 4])
False
```

## 7. Bonus Round Rules

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.

Implement a function `maybe_double_last` that takes a hand and checks if the last card is a Jack (11). If the the last card is a Jack (11), double its value.

```python
>>> hand = [5, 9, 11]
>>> maybe_double_last(hand)
>>> hand
[5, 9, 22]

>>> hand = [5, 9, 10]
>>> maybe_double_last(hand)
>>> hand
[5, 9, 10]
```
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"blurb": "Learn about lists by helping Elyse practice her playing card enchantments.",
"blurb": "Learn about lists by tracking hands in card games.",
"contributors": [
"valentin-p",
"bethanyg"
"valentin-p"
],
"authors": [
"itamargal"
"itamargal",
"isaacg",
"bethanyg"
],
"files": {
"solution": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ The goal of this exercise is to teach the basics of the `list` data type in Pyth

- Create a `list` via constructor (`list()`) & literal (`[]`)
- Combine two or more lists by concatenation via `+`
- Check for an items membership/absence in a list using `in` and/or `not in`
- Check for an items membership/absence in a list using `in`
- Access items in a list via index (`bracket notation`)
- Access a range of items in a list via list slicing (`[start:stop:step]`)
- Iterate through a list using `for item in`
- Understand that `lists` can store mixed/any data types
- Understand that `lists` are mutable. Assigning a new value to a list index will change the value at that index.

## Out of scope
Expand Down
39 changes: 39 additions & 0 deletions exercises/concept/card-games/.meta/exemplar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def to_rounds(number):
"""Return a list of rounds that includes `number` and the next two."""
return [number, number + 1, number + 2]


def concatenate_rounds(rounds1, rounds2):
"""Return a list of rounds that combines `rounds1` and `rounds2`."""
return rounds1 + rounds2


def list_contains_round(rounds, number):
"""Return whether `rounds` includes the `number` round."""
return number in rounds


def card_average(hand):
"""Return the average of the hand."""
total = 0
count = 0
for card in hand:
total += card
count += 1
return total / count


def approx_average_is_average(hand):
"""Return whether the average of the first and last card is the average."""
return card_average([hand[0], hand[-1]]) == card_average(hand)


def average_even_is_average_odd(hand):
"""Return whether the average of the even cards is the same as that odd cards."""
return card_average(hand[::2]) == card_average(hand[1::2])


def maybe_double_last(hand):
"""Double the value of the last card if it is a Jack (11)."""
if hand[-1] == 11:
hand[-1] *= 2
33 changes: 33 additions & 0 deletions exercises/concept/card-games/lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def to_rounds(number):
"""Return a list of rounds that includes `number` and the next two."""
pass


def concatenate_rounds(rounds1, rounds2):
"""Return a list of rounds that combines `rounds1` and `rounds2`."""
pass


def list_contains_round(rounds, number):
"""Return whether `rounds` includes the `number` round."""
pass


def card_average(hand):
"""Return the average of the hand."""
pass


def approx_average_is_average(hand):
"""Return whether the average of the first and last card is the average."""
pass


def average_even_is_average_odd(hand):
"""Return whether the average of the even cards is the same as that odd cards."""
pass


def maybe_double_last(hand):
"""Double the value of the last card if it is a Jack (11)."""
pass
Loading

0 comments on commit 3cd390f

Please sign in to comment.