Skip to content

Commit 1acc399

Browse files
mohanrajanrcmccandless
authored andcommitted
Implement new Concept Exercise: list-methods #2170
* Added New Concept Exercise List Methods Part 1 * Review Change 1
1 parent 90bcbc6 commit 1acc399

File tree

9 files changed

+749
-0
lines changed

9 files changed

+749
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
Python allows you to manipulate `list` in a lot of ways. A `list` is simple a collection of objects. They are mutable, ordered and indexed. Let's look at the methods that are available to manipulate a `list` object.
2+
3+
When you manipulate a list with a list-method, you are changing the properties of the list you pass. That is, **you will alter the list** object that is being used with the list-method. If you do not want to change the original list, you need to copy the list and then work on the copied list.
4+
5+
To begin, you first need a `list` object to use the functions.
6+
7+
```python
8+
ex_list = list() # (or) ex_list = []
9+
print(ex_list)
10+
#=> []
11+
```
12+
13+
## Add an Item to the List
14+
15+
Topics to discuss:
16+
17+
1. `append`
18+
2. `insert`
19+
3. `extend`
20+
21+
If you want to add an item to an existing list, you use the list-method `append()` for it. As the name represent, it appends the item to the **end** of the list.
22+
23+
```python
24+
ex_list = [1, 2, 3]
25+
ex_list.append(9)
26+
print(ex_list)
27+
#=> [1, 2, 3, 9]
28+
```
29+
30+
There is another way to add an item to the list. `insert()` method also gives you the ability to add the item to a particular index in the list.
31+
32+
`insert` takes 2 parameters.
33+
34+
1. index at which you want to add the item
35+
2. item
36+
37+
Note: if the index is 0, the item will be added to the first of the list. if the index is more than or equal to the length of the list, then item will be added to the last of the list (like the append function)
38+
39+
```python
40+
ex_list = [1, 2, 3]
41+
ex_list.insert(0, -2)
42+
print(ex_list)
43+
#=> [-2, 1, 2, 3]
44+
ex_list.insert(1, 0)
45+
print(ex_list)
46+
#=> [-2, 0, 1, 2, 3]
47+
```
48+
49+
If you have another list object who's content you need to be copied to your list, then you can use the `extend` function.
50+
The extend function takes an iterable as its parameter.
51+
52+
```python
53+
ex_list = [1, 2, 3]
54+
extend_list = [5, 6, 7]
55+
56+
ex_list.extend(extend_list)
57+
print(ex_list)
58+
#=> [1, 2, 3, 5, 6, 7]
59+
60+
ex_list.extend([8, 9])
61+
print(ex_list)
62+
#=> [1, 2, 3, 5, 6, 7, 8, 9]
63+
```
64+
65+
# Removing Items from the list
66+
67+
Topics to discuss:
68+
69+
1. `remove`
70+
2. `pop`
71+
3. `clear`
72+
73+
If you want to remove an item from a list, you can use the `remove` function which takes one parameter. Feed the function with the item you want to remove from the list. the `remove` function will throw a `ValueError` if the item does not exist in the loop.
74+
75+
```python
76+
ex_list = [1, 2, 3]
77+
ex_list.remove(2)
78+
print(ex_list)
79+
#=> [1, 3]
80+
ex_list.remove(0)
81+
#=> ValueError: list.remove(x): x not in list
82+
```
83+
84+
There is another way to remove an item from the list. If you know the index of the item which you want to remove, you can use `pop` function. Pop takes 1 parameter, the index of the item you need to remove, and return it to you. If you specify an index more than the length of the list, you will get an `IndexError`. If you dont specify an index, the function will remove the last element and return it to you.
85+
86+
```python
87+
ex_list = [1, 2, 3]
88+
ex_list.pop(0)
89+
#=> 1
90+
print(ex_list)
91+
#=> [2, 3]
92+
ex_list.pop()
93+
#=> [2]
94+
```
95+
96+
If you want to clear the list (ie: all the items in the list), then you can use the `clear` function. It does not take any parameter as input but will remove all the elements in the list.
97+
98+
```python
99+
ex_list = [1, 2, 3]
100+
ex_list.clear()
101+
print(ex_list)
102+
#=> []
103+
```
104+
105+
## Reverse the list
106+
107+
The items in the list can be reordered in the reverse order with the `reverse` function.
108+
109+
```python
110+
ex_list = [1, 2, 3]
111+
ex_list.reverse()
112+
print(ex_list)
113+
#=> [3, 2, 1]
114+
```
115+
116+
## Sort the list
117+
118+
When you have an random ordered list of items, you can sort them with the help of `sort` function. If you have items that are alphanumerical, you dont have to provide any inputs to sort.
119+
120+
ex: you have list of numbers, or you have a list of names and you want to sort them
121+
122+
```python
123+
ex_list = ["Tony", "Natasha", "Thor", "Bruce"]
124+
ex_list.sort()
125+
print(ex_list)
126+
#=> ["Bruce", "Natasha", "Thor", "Tony"]
127+
```
128+
129+
If you want the sort to be in descending order, you can use the reverse parameter.
130+
131+
```python
132+
ex_list = ["Tony", "Natasha", "Thor", "Bruce"]
133+
ex_list.sort(reverse=True)
134+
print(ex_list)
135+
#=> ["Tony", "Thor", "Natasha", "Bruce"]
136+
```
137+
138+
If you have a list of items that are complex, you can use the key parameter in the sort. Lets see more about this later.
139+
140+
Internally, python uses `Timsort` to sort the list.
141+
142+
## Occurances of an item in the list.
143+
144+
You can find the occurances of an element in the list with the help of the `count` function. It takes one parameter which will be the item, for which you need to find the number of occurances.
145+
146+
```python
147+
ex_list = [1, 4, 7, 8, 2, 9, 2, 1, 1, 0, 4, 3]
148+
ex_list.count(1)
149+
#=> 3
150+
```
151+
152+
## Find the Index of the Item.
153+
154+
If you want to find the index of an item you want in the list, you can use the `index` function. If you have multiple occurances of the same item, it will provide you the index of the first occurance. If you do not have any occurances of the item, then you will have a `ValueError` raised.
155+
156+
Index starts with 0.
157+
158+
```python
159+
ex_list = [1, 4, 7, 8, 2, 9, 2, 1, 1, 0, 4, 3]
160+
ex_list.index(4)
161+
#=> 1
162+
ex_list.index(10)
163+
#=>ValueError: 10 is not in list
164+
```
165+
166+
You can also provide a start (and an end) to the index from where you need the search has to happen for the item.
167+
168+
```python
169+
ex_list = [1, 4, 7, 8, 2, 9, 2, 1, 1, 0, 4, 3]
170+
ex_list.index(4, 2, 12)
171+
#=> 10
172+
```
173+
174+
## Copy a List.
175+
176+
Lists are a collection of items which are referenced to an index, Hence if you do an assignment of a list object to another variable. Any change you make to the assigned variable will also have an impact on the original variable.
177+
178+
```python
179+
ex_list = ["Tony", "Natasha", "Thor", "Bruce"]
180+
new_list = ex_list
181+
new_list.append("Clarke")
182+
print(new_list)
183+
#=> ["Tony", "Natasha", "Thor", "Bruce", "Clarke"]
184+
print(ex_list)
185+
#=> ["Tony", "Natasha", "Thor", "Bruce", "Clarke"]
186+
```
187+
188+
You need to use the `copy` function in order to do a `shallow_copy` of the list. ( More about `shallow_copy` and `deep_copy` later.)
189+
190+
```python
191+
ex_list = ["Tony", "Natasha", "Thor", "Bruce"]
192+
new_list = ex_list.copy()
193+
new_list.append("Clarke")
194+
print(new_list)
195+
#=> ["Tony", "Natasha", "Thor", "Bruce", "Clarke"]
196+
print(ex_list)
197+
#=> ["Tony", "Natasha", "Thor", "Bruce"]
198+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# General
2+
3+
- Make sure you have good understanding of creating lists and manipulating them.
4+
5+
## 1. Add Me to the queue
6+
7+
- You need to find the ticket type with basic if else statement.
8+
- You can then add or `append` the person to the queue based on the ticket type.
9+
10+
## 2. We came as a Group
11+
12+
- You need to extend your list. That is, you have an iterable number of names that you need to extend to your existing queue.
13+
14+
## 3. Where are my friends
15+
16+
- You need to find the `index` of the friend name from the queue.
17+
18+
## 4. Can I please join with them?
19+
20+
- Since you know the `index`, you need to `insert` the friend to the index in the queue.
21+
22+
## 5. Mean person in the queue
23+
24+
- You know the name of the person who is mean. you have to just `remove` them from the queue.
25+
26+
# 6. DoppelGangers
27+
28+
- The number of people with the same name can be 0 till the whole queue length. All you need to do is find the `count` or the occurances of the name in the queue.
29+
30+
## 7. Remove the last guy
31+
32+
- Since you know that you need to remove the last person on the queue, you just need to `pop` him/her out.
33+
34+
## 9. New Ride Begins
35+
36+
- First, you need to copy the person names from the original queue into a new queue,
37+
- You then have to `extend` or `append` one by one the extra people who are waiting for the new ride.
38+
39+
## 10. Sort the Queue List
40+
41+
- Since the order is unspecified, it is in ascending order, hence you need to `sort` the queue and return.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
You own a theme park. And for some reason, you own only one ride. The biggest roller coaster in the world. Although, you have only one ride, people stand in line for hours for the ride.
2+
3+
You do have 2 queues for this ride.
4+
5+
1. Normal Queue
6+
2. Express Queue ( Also known as Fast-track Queue) - people pay extra to get faster access to the ride.
7+
8+
You are given a tasks to finish the following functions for the completion of this exercise.
9+
10+
## 1. Add Me to the queue
11+
12+
Define the `add_me_to_the_queue()` function that takes 4 parameters `express_queue, normal_queue, ticket_type, person_name` and returns the queue to which the person is added.
13+
14+
1. express queue is a list
15+
2. normal queue is a list
16+
3. ticket_type is a int in which 1 means he belongs to express queue, 0 means he belongs to normal queue.
17+
4. person name is the name of the person to be added to the respective queue.
18+
19+
Once you have added the name to the respective queue, returned the added queue,
20+
21+
```python
22+
add_me_to_the_queue(express_queue=["Tony", "Bruce"], normal_queue=["RobotGuy", "WW"], ticket_type=1, person_name="RichieRich")
23+
# => ["Tony", "Bruce", "RichieRich"]
24+
25+
add_me_to_the_queue(express_queue=["Tony", "Bruce"], normal_queue=["RobotGuy", "WW"], ticket_type=0, person_name="HawkEye")
26+
# => ["RobotGuy", "WW", "HawkEye"]
27+
```
28+
29+
## 2. Where are my friends
30+
31+
One guy came late to the park and wants to join with his friends. He doesn't know where they are standing but wants to join with them.
32+
33+
Define the `find_his_friend()` function that takes 2 parameters `queue, friend_name`.
34+
35+
1. Queue is the list of people standing in the queue.
36+
2. friend_name is the name of the friend who's index you need to find.
37+
38+
Indexing starts from 0.
39+
40+
```python
41+
find_his_friend(queue=["Natasha", "Steve", "Tchalla", "Wanda", "Rocket"], friend_name="Steve")
42+
# => 1
43+
```
44+
45+
## 3. Can I please join with them?
46+
47+
Define the `add_person_with_his_friends()` function that takes 3 parameters `queue, index, person_name`.
48+
49+
1. Queue is the list of people standing in the queue.
50+
2. index is the location the person has to be added
51+
3. person_name is the name of the person to add at that index.
52+
53+
```python
54+
add_person_with_his_friends(queue=["Natasha", "Steve", "Tchalla", "Wanda", "Rocket"], index=1, person_name="Bucky")
55+
# => ["Natasha", "Bucky", "Steve", "Tchalla", "Wanda", "Rocket"]
56+
```
57+
58+
## 4. Mean person in the queue
59+
60+
You just heard from the queue that there is a mean person in the queue and you need to throw him out.
61+
62+
Define the `remove_the_mean_person()` function that takes 2 parameters `queue, person_name`.
63+
64+
1. Queue is the list of people standing in the queue.
65+
2. person_name is the name of the person whom we need to kick out.
66+
67+
```python
68+
remove_the_mean_person(queue=["Natasha", "Steve", "Eltran", "Wanda", "Rocket"], person_name="Eltran")
69+
#=> ["Natasha", "Bucky", "Steve", "Wanda", "Rocket"]
70+
```
71+
72+
# 5. DoppelGangers
73+
74+
You may not have seen 2 people look like the same person before but you sure would have seen people with the same name. It looks like today you are seeing the same scenario. You want to know how many times a name has occured in the queue.
75+
76+
Define the `how_many_dopplegangers()` function that takes 2 parameters `queue, person_name`.
77+
78+
1. Queue is the list of people standing in the queue.
79+
2. person_name is the name of the person whom you think have been occuring more than once in the queue.
80+
81+
Return the number of occurances of the name, in `int` format.
82+
83+
```python
84+
how_many_dopplegangers(queue=["Natasha", "Steve", "Eltran", "Natasha", "Rocket"], person_name="Natasha")
85+
#=> 2
86+
```
87+
88+
## 6. Remove the last guy
89+
90+
There is an overcrowd in the queue and you need to remove the last person out. You will have to define the function `remove_the_last_person()` that takes 1 parameter `queue` which will be called again and again till the queue count is back to normal. Queue is the list of people standing in the queue.
91+
92+
You should also return the name of the person who was removed.
93+
94+
```python
95+
remove_the_last_person(queue=["Natasha", "Steve", "Eltran", "Natasha", "Rocket"])
96+
#=> Rocket
97+
```
98+
99+
## 7. Sort the Queue List
100+
101+
For Admin Purpose, you need the list of names in the queue in sorted order.
102+
103+
Define the `sorted_names()` function that takes 1 parameter `queue`. Queue is the list of people standing in the queue.
104+
105+
```python
106+
sorted_names(queue=["Natasha", "Steve", "Eltran", "Natasha", "Rocket"])
107+
#=>['Natasha', 'Natasha', 'Rocket', 'Steve', 'Eltran']
108+
```

0 commit comments

Comments
 (0)