File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree File renamed without changes.
Original file line number Diff line number Diff line change 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 ()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments