Skip to content

Commit 2224765

Browse files
committed
learning dict
1 parent ca818c6 commit 2224765

9 files changed

Lines changed: 67 additions & 9 deletions

File tree

File renamed without changes.

‎ds_algo/dict/test/test_dict.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# !/usr/bin/env python3
2+
3+
# author: greyshell
4+
# description: play with dict
5+
6+
7+
def main():
8+
"""
9+
- TBD
10+
:return:
11+
"""
12+
# create a empty dict
13+
a = {}
14+
d = dict()
15+
16+
# initialise
17+
name_dept = {
18+
"alice": "cse",
19+
"bob": "ee",
20+
"carol": "it"
21+
}
22+
23+
# look up: method 1
24+
try:
25+
dept = name_dept["no_key"] # throws exception when the key is not found
26+
print(f"{dept}")
27+
except KeyError as e:
28+
print(f"error: {e}")
29+
30+
# look up: method 2
31+
if "no_key" in name_dept: # inefficient because it queries the dict twice
32+
dept = name_dept["no_key"]
33+
print(f"{dept}")
34+
35+
# look up: method 3 (recommended)
36+
dept = name_dept.get("alice", "no_key") # provides the support for default value
37+
print(f"{dept}")
38+
39+
# add items
40+
41+
42+
if __name__ == '__main__':
43+
main()
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def main():
99
- python list is backed by dynamic array, so occasionally it needs to be resized when item is added / removed
1010
- it does not guarantee the stable O(1) push and pop
1111
- however, it provides the amortize time complexity is O(1) when the item is added / remove at the end of list
12-
- adding / removing from front is much slower and it takes O(n) time as shifting of existing elements are required
13-
- IndexError => pop from an empty list
12+
- adding / removing from front / middle is much slower and it takes O(n) time as shifting of existing elements
13+
are required
1414
- no parallel processing support / it handles locking and unlocking
1515
:return:
1616
"""
@@ -29,8 +29,12 @@ def main():
2929
print(f"peek the stack top: {data}")
3030

3131
# pop: O(1), stack shrinks from right to left
32-
data = s.pop()
33-
print(f"popped: {data}")
32+
# raise IndexError => pop from an empty list
33+
try:
34+
data = s.pop()
35+
print(f"popped: {data}")
36+
except IndexError as e:
37+
print(f"{e}")
3438

3539
print(f"display the stack elements: {list(s)}")
3640

ds_algorithm/stack/test/testlib_stack02.py renamed to ds_algo/stack/test/test_stack02.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def main():
1010
"""
1111
- deque is backed by doubly linked list
1212
- it that guarantees the stable O(1) push and pop
13-
- IndexError => pop from an empty list
1413
- no parallel processing support / it handles locking and unlocking
1514
:return:
1615
"""
@@ -30,8 +29,12 @@ def main():
3029
print(f"peek the stack top: {data}")
3130

3231
# pop: O(1), stack shrinks from right to left
33-
data = s.pop()
34-
print(f"popped: {data}")
32+
# raise IndexError => pop from an empty list
33+
try:
34+
data = s.pop()
35+
print(f"popped: {data}")
36+
except IndexError as e:
37+
print(f"{e}")
3538

3639
print(f"display the stack elements: {list(s)}")
3740

ds_algorithm/stack/test/testlib_stack03.py renamed to ds_algo/stack/test/test_stack03.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ def main():
2323
# not found any method for peek and display stack elements
2424

2525
# pop: O(1)
26-
data = s.get_nowait() # not wait for producer to add the element if the stack is empty
27-
print(f"popped: {data}")
26+
data = s.get_nowait()
27+
data = s.get_nowait()
28+
29+
try:
30+
data = s.get_nowait() # not wait for producer to add the element if the stack is empty
31+
print(f"popped: {data}")
32+
except Exception as e:
33+
print(f"{e}")
34+
35+
exit(0)
2836

2937
data = s.get() # poped out the last stack element
3038
data = s.get() # waits for ever as the stack is empty, producer needs to add an element

0 commit comments

Comments
 (0)