|
| 1 | +## Instructions |
| 2 | + |
| 3 | +As a magician-to-be, Elyse needs to practice some basics. She has a stack of cards that she wants to manipulate. In this exercise we'll use lists to help Elyse perform her card tricks. |
| 4 | + |
| 5 | +[//]: # 'Creating Lists' |
| 6 | + |
| 7 | +## 1. Creating a List |
| 8 | + |
| 9 | +Some magic tricks involve forming a [hand](https://en.wikipedia.org/wiki/Glossary_of_card_game_terms#hand) as in a regular card game, e.g. forming a single hand from five individual cards. |
| 10 | + |
| 11 | +Implement a function `to_list` that takes five arguments and returns a single list with those arguments as its elements: |
| 12 | + |
| 13 | +```python |
| 14 | +>>> to_list('10H', 'JH', 'QH', 'KH', 'AH') |
| 15 | +['10H', 'JH', 'QH', 'KH', 'AH'] |
| 16 | +``` |
| 17 | + |
| 18 | +[//]: # 'Create Lists Using the `list` Type' |
| 19 | + |
| 20 | +## 2. Creating a copy of a List |
| 21 | + |
| 22 | +In a traditional deck of cards, each card is unique and only occurs once. But for some magic tricks, Elyse needs to sneak in duplicate copies of some cards. |
| 23 | + |
| 24 | +Implement a function `list_twice` that takes a single list as its only argument, and returns a list which consists of two copies of the input list: |
| 25 | + |
| 26 | +```python |
| 27 | +>>> list_twice(['AC', 'AD', 'AH', 'AS']) |
| 28 | +[['AC', 'AD', 'AH', 'AS'], ['AC', 'AD', 'AH', 'AS']] |
| 29 | +``` |
| 30 | + |
| 31 | +[//]: # 'Concatenating Lists Using the `+` Operator' |
| 32 | + |
| 33 | +## 3. Concatenating Lists |
| 34 | + |
| 35 | +For some tricks, Elyse needs to take two stacks of cards and put one on top of the other |
| 36 | + |
| 37 | +Implement a function `concatenate_lists` that takes two lists and returns a single list consisting of all the elements in the first list, followed by all the elements in the second list: |
| 38 | + |
| 39 | +```python |
| 40 | +>>> concatenate_lists(['2C', '2H', '2D'], ['KC', 'KD']) |
| 41 | +['2C', '2H', '2D', 'KC', 'KD'] |
| 42 | +``` |
| 43 | + |
| 44 | +[//]: # 'Checking for List Membership using the `in` keyword' |
| 45 | + |
| 46 | +## 4. Testing List Membership |
| 47 | + |
| 48 | +Elyse is practicing one particular trick in which she has a volunteer select a set of cards from the deck, and she has to guess whether a certain card is in their hand. |
| 49 | + |
| 50 | +Implement a function `list_contains_object` that takes two arguments, an arbitrary object and a list, and returns `True` if the object is an element of the list: |
| 51 | + |
| 52 | +```python |
| 53 | +>>> list_contains_object(['AC', 'AD', 'AH', 'AS'], 'AC') |
| 54 | +True |
| 55 | + |
| 56 | +>>> list_contains_object(['AC', 'AD', 'AH', 'AS'], '10C') |
| 57 | +False |
| 58 | +``` |
| 59 | + |
| 60 | +[//]: # 'Accessing List Elements by Index' |
| 61 | + |
| 62 | +## 5. Accessing List Elements by Index |
| 63 | + |
| 64 | +For some tricks, Elyse needs to be able to take cards from the top and bottom of the deck through slight-of-hand. |
| 65 | + |
| 66 | +Implement a function `first_and_last` that returns the first and last elements from a list: |
| 67 | + |
| 68 | +```python |
| 69 | +>>> first_and_last(['2C', '2H', '2D', 'KC', 'KD']) |
| 70 | +['2C', 'KD'] |
| 71 | +``` |
| 72 | + |
| 73 | +[//]: # 'Accessing Sublists by Slicing' |
| 74 | + |
| 75 | +## 6. Accessing Sublists by Slicing |
| 76 | + |
| 77 | +For other tricks, Elyse needs to be able to take some cards from inbetween other cards through slight-of-hand. |
| 78 | + |
| 79 | +Implement a function `interior_of_list` that returns all elements of a list _except_ for the first and last elements: |
| 80 | + |
| 81 | +```python |
| 82 | +>>> interior_of_list(['2C', '2H', '2D', 'KC', 'KD']) |
| 83 | +['2H', '2D', 'KC'] |
| 84 | +``` |
| 85 | + |
| 86 | +One of Elyse's most amazing tricks to reorder a shuffled deck. |
| 87 | + |
| 88 | +Implement a function `even_elements` that returns every element of even index from a list: |
| 89 | + |
| 90 | +```python |
| 91 | +>>> even_elements(['2C', '2H', '2D', 'KC', 'KD']) |
| 92 | +['2C', '2D', 'KD'] |
| 93 | +``` |
| 94 | + |
| 95 | +Implement a function `odd_elements` that returns every element of odd index from a list: |
| 96 | + |
| 97 | +```python |
| 98 | +>>> odd_elements(['2C', '2H', '2D', 'KC', 'KD']) |
| 99 | +['2H', 'KC'] |
| 100 | +``` |
| 101 | + |
| 102 | +Implement a function `unshuffle` that "unshuffles" a set of cards by appending the elements of odd index to the elements of even index: |
| 103 | + |
| 104 | +```python |
| 105 | +>>> unshuffle(['2C', '2H', '2D', 'KC', 'KD']) |
| 106 | +['2C', '2D', 'KD', '2H', 'KC'] |
| 107 | +``` |
| 108 | + |
| 109 | +## 7. Iterating Over List Items |
| 110 | + |
| 111 | +For some tricks, Elyse guesses all the cards in a volunteers hands and names them out loud. |
| 112 | + |
| 113 | +Implement a function `print_list` that prints each element of a list on a separate line: |
| 114 | + |
| 115 | +```python |
| 116 | +>>> print_list(['2C', '2H', '2D', 'KC', 'KD']) |
| 117 | +2C |
| 118 | +2H |
| 119 | +2D |
| 120 | +KC |
| 121 | +KD |
| 122 | +``` |
| 123 | + |
| 124 | +## 8. Creating Lists with Mixed Data Types |
| 125 | + |
| 126 | +For some tricks, Elyse sneaks in objects that aren't even playing cards! |
| 127 | + |
| 128 | +Suppose that you're given a function `count_types` that returns a count of the number of distinct types that occur in a given list: |
| 129 | + |
| 130 | +```python |
| 131 | +>>> count_types(['2C', '2H', '2D', 'KC', 'KD']) |
| 132 | +1 |
| 133 | +>>> count_types([1, 2, 3]) |
| 134 | +1 |
| 135 | +>>> count_types(['2C', '2H', '2D', 'KC', 'KD', 1, 2, 3]) |
| 136 | +2 |
| 137 | +``` |
| 138 | + |
| 139 | +Write a function `multitype_list` that returns a list of elements of at least three different types: |
| 140 | + |
| 141 | +```python |
| 142 | +>>> count_types(multitype_list()) > 2 |
| 143 | +True |
| 144 | +``` |
| 145 | + |
| 146 | +## 9. Modifying Values in Lists |
| 147 | + |
| 148 | +For some tricks, Elyse needs to use sleight-of-hand to swap cards without anyone noticing. |
| 149 | + |
| 150 | +Implement a function `swap_first_and_last` that takes a list and swaps the positions of the first and last elements in the list: |
| 151 | + |
| 152 | +```python |
| 153 | +>>> _list = ['2C', '2H', '2D', 'KC', 'KD'] |
| 154 | +>>> swap_first_and_last(_list) |
| 155 | +>>> _list |
| 156 | +['KD', '2H', '2D', 'KC', '2C'] |
| 157 | +``` |
0 commit comments