Python Flashcards
Create an array of 0s
temp = [0]*(r-l+1)
sort an array
arr.sort()
iterate over keys in dict
for key in dict: print(key,dict[key])
defaultdict
from collections import defaultdict d = defaultdict(int)
seen = defaultdict(set)
for x in range(1000):
for y in range(1000):
seen[x].add(y) # insert a new pair
assert y in seen[x] # check if pair in dictionary
Declare a set
By default h = {} is a map so for set, we do h= set()
reverse list
list.reverse() -> in place
new_list = list[::-1] -> extra space
comparator
from functools import cmp_to_key
key=cmp_to_key(lambda x,y: 1 if str(y)+str(x) > str(x)+str(y) else -1))
iterate digits of a number if need to iterate msb to lsb
dictmap = { '0' : 0, '1' : 1, '2' : 2, '3' : 3, '8' : 8, '9' : 9 }
def each_dig_in_num(n): dm = dictmap #faster if local s = repr(n) #repr is faster than str for char in s: foo(dm[char])
iterate digits of a number if order doesnโt matter
n2 = n while n2 > 0: d = n2 % 10 n2 /= 10 foo(d)
other ways to iterate digits of a number using typecasting
2.
n = 12345
#1. s = str(n) for i in s: d = int(i) foo(d)
nl = map(int, str(n))
for d in nl:
foo(d)
nl = [int(x) for x in str(n)]
for d in nl:
foo(d)
Of those, I have, a bit surprised, found that #1 is the fastest, and
that #2 using map() is faster than #3 using list comprehension. I also
registered that that repr(n) is about 8% faster than str(n)
stack using deque
from collections import deque
stack = deque()
Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.
min heap (default)
heapify(), heappush() and heappop()
# importing "heapq" to implement heap queue import heapq
li = [5, 7, 9, 1, 3] # initializing list
# using heapify to convert list into heap heapq.heapify(li)
# printing created heap print (list(li))
# using heappush() to push elements into heap heapq.heappush(li,4)
# using heappop() to pop smallest element print (heapq.heappop(li))
Heap operations
import heapq
- heapify(iterable)
- heappush(heap, ele)
- heappop(heap)
- heappushpop(heap, ele)
- heapreplace(heap, ele): In this, element is first popped, then the element is pushed.i.e, the value larger than the pushed value can be returned. heapreplace() returns the smallest value originally in heap regardless of the pushed element as opposed to heappushpop().
- nlargest(k, iterable, key = fun)
- nsmallest(k, iterable, key = fun)
Integer division of negative numbers
Because // is floor division, so rounding down, not towards zero.-22 / 10 is -2.2, rounding down makes that -3
deque operations
append() :- This function is used to insert the value in its argument to the right end of deque.
appendleft() :- This function is used to insert the value in its argument to the left end of deque.
pop() :- This function is used to delete an argument from the right end of deque.
popleft() :- This function is used to delete an argument from the left end of deque.