Skip to content

Commit 2127027

Browse files
BethanyGItamar Gal
authored andcommitted
Ran Prettier on instructions.md for Lists Concept exercise.
* Initial draft of "lists" concept exercise for Python track (Issue #859) * Ran prettier to format markdown instructions.md file. Co-authored-by: Itamar Gal <[email protected]>
1 parent 4305478 commit 2127027

File tree

4 files changed

+481
-0
lines changed

4 files changed

+481
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
def to_list(arg1, arg2, arg3, arg4, arg5):
2+
return [arg1, arg2, arg3, arg4, arg5]
3+
4+
5+
def list_twice(_list):
6+
return [_list, list(_list)]
7+
8+
9+
def concatenate_lists(list1, list2):
10+
return list1 + list2
11+
12+
13+
def list_contains_object(_list, _object):
14+
return _object in _list
15+
16+
17+
def first_and_last(_list):
18+
return [_list[0], _list[-1]]
19+
20+
21+
def interior_of_list(_list):
22+
return _list[1:-1]
23+
24+
25+
def even_elements(_list):
26+
return _list[::2]
27+
28+
29+
def odd_elements(_list):
30+
return _list[1::2]
31+
32+
33+
def unshuffle(_list):
34+
return _list[::2] + _list[1::2]
35+
36+
37+
def print_list(_list):
38+
for element in _list:
39+
print(element)
40+
41+
42+
def multitype_list():
43+
return [1, '1', 1.0]
44+
45+
46+
def swap_first_and_last(_list):
47+
_list[0], _list[-1] = _list[-1], _list[0]

exercises/concept/lists/lists.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def to_list(arg1, arg2, arg3, arg4, arg5):
2+
pass
3+
4+
5+
def list_twice(_list):
6+
pass
7+
8+
9+
def concatenate_lists(list1, list2):
10+
pass
11+
12+
13+
def list_contains_object(_list, _object):
14+
pass
15+
16+
17+
def first_and_last(_list):
18+
pass
19+
20+
21+
def interior_of_list(_list):
22+
pass
23+
24+
25+
def even_elements(_list):
26+
pass
27+
28+
29+
def odd_elements(_list):
30+
pass
31+
32+
33+
def unshuffle(_list):
34+
pass
35+
36+
37+
def print_list(_list):
38+
pass
39+
40+
41+
def multitype_list():
42+
pass
43+
44+
45+
def swap_first_and_last(_list):
46+
pass

0 commit comments

Comments
 (0)