Skip to content

Commit ace70a7

Browse files
authored
Merge pull request #10 from Code-by-practice/develop
feat(examples): data structure list
2 parents 32de77d + 4d32f7a commit ace70a7

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

codes/session_3/ds_list.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Open terminal > python3 ds_list.py (Data Structure)
2+
# Start typing below commands and see the output
3+
4+
birds = ['Owl', 'Macaw', 'Parrot', 'BeeHumming']
5+
6+
# Return the number of x appears in the list
7+
print('\nCount: ' + str(birds.count('Macaw')))
8+
9+
# Return zero-based index in the list, Raises a ValueError if no item exist
10+
print('\nIndex: ' + str(birds.index('Parrot')))
11+
12+
# Reverse the elements of the list in place
13+
print('\nReverse: ')
14+
birds.reverse()
15+
print(birds)
16+
17+
# Sort
18+
print('\nSort: ')
19+
birds.sort()
20+
print(birds)
21+
22+
# Append
23+
print('\nAppend Penguin')
24+
birds.append('Penguin')
25+
print(birds)
26+
27+
# Insert
28+
print('\nInsert Kingfisher on index 3')
29+
birds.insert(3, 'Kingfisher')
30+
print(birds)
31+
32+
# Pop
33+
print('\nPop bird on index 3')
34+
birds.pop(3)
35+
print(birds)
36+
37+
# Remove
38+
print('\nRemove bird Penguin')
39+
birds.remove('Penguin')
40+
print(birds)
41+
42+
# clear
43+
print('\nClear birds list')
44+
birds.clear()
45+
print(birds)
46+

codes/session_3/ds_list_as_queue.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Open terminal > python3 ds_list_as_stack.py (Data Structure)
2+
# Start typing below commands and see the output
3+
4+
# Stack:
5+
# First-in First-out
6+
7+
# Import collection.deque
8+
from collections import deque
9+
10+
queue = deque(['Owl', 'Macaw', 'Parrot', 'BeeHumming'])
11+
12+
# First-in
13+
print('\nFirst-in: ')
14+
queue.append('Kingfisher')
15+
queue.append('Penguin')
16+
print(queue)
17+
18+
print('\nFirst-out: ')
19+
print(queue.popleft()) # Owl
20+
print(queue.popleft()) # Macaw
21+
print(queue)

codes/session_3/ds_list_as_stack.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Open terminal > python3 ds_list_as_stack.py (Data Structure)
2+
# Start typing below commands and see the output
3+
4+
# Stack:
5+
# Last-in First-out
6+
7+
birds = ['Owl', 'Macaw', 'Parrot', 'BeeHumming']
8+
9+
# Append to stack i.e. Last-In
10+
print('Append to stack i.e. Last-In: ')
11+
birds.append('Panguin')
12+
birds.append('Kingfisher')
13+
print(birds)
14+
15+
# Pop from the stack i.e. First-out
16+
print('Pop from the stack i.e. First-out: ')
17+
birds.pop()
18+
birds.pop()
19+
print(birds)

0 commit comments

Comments
 (0)