Skip to content

Commit 7d058a3

Browse files
committed
Add my solution for Arrays chapter
1 parent 4d8a294 commit 7d058a3

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Question 1
2+
list = {'January': 2200, 'February': 2350, 'March': 2600, 'April': 2130, 'May': 2190}
3+
4+
# 1. In Feb, how many dollars you spent extra compare to January?
5+
print(list['February']-list['January'])
6+
# 2. Find out your total expense in first quarter (first three months) of the year.
7+
print(list['January']+list['February']+list['March'])
8+
# 3. Find out if you spent exactly 2000 dollars in any month
9+
for i in list:
10+
if list[i] == 2000:
11+
print(i)
12+
else: print('No such month')
13+
# 4. June month just finished and your expense is 1980 dollar. Add this item to our monthly expense list
14+
list['June'] = 1980
15+
print(list)
16+
# 5. You returned an item that you bought in a month of April and got a refund of 200$. Make a correction to your monthly expense list
17+
# based on this
18+
list['April'] = list['April'] - 200
19+
print(list['April'])
20+
21+
# Question 2
22+
heros=['spider man','thor','hulk','iron man','captain america']
23+
24+
# 1. Length of the list
25+
print(len(heros))
26+
# 2. Add 'black panther' at the end of this list
27+
heros.append('black panther')
28+
print(heros)
29+
# 3. You realize that you need to add 'black panther' after 'hulk',
30+
# so remove it from the list first and then add it after 'hulk'
31+
heros.remove('black panther')
32+
heros.insert(3,'black panther')
33+
print(heros)
34+
# 4. Now you don't like thor and hulk because they get angry easily :)
35+
# So you want to remove thor and hulk from list and replace them with doctor strange (because he is cool).
36+
# Do that with one line of code.
37+
heros[1:3] = ['doctor strange']
38+
print(heros)
39+
# 5. Sort the heros list in alphabetical order
40+
heros.sort()
41+
print(heros)
42+
43+
# Question 3

0 commit comments

Comments
 (0)