This is my effort to continuously practive technical questions and further my understanding of data structures and algorithms. Below are my lessons on Python as I complete this problems.
from collections import OrderedDict
d = OrderedDict()
popitem: pops item from end in LIFO fashion if last is True, from begin in FIFO fashion of last is False
popped = d.popitem(last = True | False)
d.move_to_end(key, last = True | False)
from collections import defaultdict
d = defaultdict()
d['x'] = 'y'
d.get('z')
AND make sure to compare return to None in if statement; in case key does exist and return is 0. Ex:
if (d.get('z') == None) and not if (d.get('z')) # in case (key, val) = ('z',0)
l_sorted = sorted(l, key = lambda , reverse = True | False) could be: x : (x[0], x[1]), len, other built-in functions...
l.sort(reverse = True | False, key = lambda x: )
import heapq
list1 = list() #populate it
heapq.heapify(list1)
heapq.heappush(list1, elem)
heapq.heappop(list1)
heapq.nsmallest(n, list1) # returns list; NOTE: this will be sorted but indexing list1[0:n] will not
heapq.nlargest(n, list1)
list(heapq.merge(list1, list2) # will merge two sorted lists and return them as a list
*list or *str will unpack the iterable into individual elements.
ex) l = [1,2,3] so *l will yield 1,2,3
s = "app", so [*s] = ["a","p","p"]
**dict will unpack the dictionary into its keys
ex) d = {1:a, 2:b, 3:c} so **d will yield a,b,c
random.randint(inclusiveLowerBound, inclusiveUpperBound) #returns int between bounds random.choice(listToPickFrom) #returns an element of the input list