Initialize integer as positive or negative infinity: {python} i: int = float('-inf'|'inf')
Integer vs Float:
10e9 is float, 10**9 is int
Collections §
Sets §
Set difference{python} set(list1) - set(list2)
Set lookup {python}i in {1, 2, 3}
==Constant time O ( 1 ) lookup==
List §
get index of var from list: {python}arr.index(val)
get all index of var from list: {python}indices = [i for i, x in enumerate(lst) if x == var]
List Sort {python} list2 = sorted(list1)
Deduplication{python} list1 = list(set(list1))
Counter {python}Counter("mississippi")
returns {python}{'i': 4, 's': 4, 'p': 2, 'm': 1}
O ( n )
Accumulate from itertools:{python} accumulate(list1)
Assign slice: {python}dp[:4] = [1, 1, 2, 5]
Multidim (m × n ): `{python}arr = [[0]*n for _ in range(m)]
! note the dims appear in reverse order
Dictionary (=Hashmap in python) §
Default value for dictionary entries{python}dictionary = collections.defaultdict(list|int)
Helps when you have a list of dicts, or accumulating a count in a dict.
var in works for keys, not values:
d = { 'a' : 1 , 'b' : 2 }
print ( 'a' in d) # True → key check
print ( 1 in d) # False → 1 is a value, not a k
from queue import PriorityQueue
pq = PriorityQueue()
for x in [ 5 , 2 , 8 ]:
pq.put(( - x, x))
_, max_val = pq.get() # pops the max item
pq.queue[ 0 ] # peek at the front of queue
print (max_val)
All Conditions True §
def is_increasing (lst):
return all (earlier < later for earlier, later in zip (lst, lst[ 1 :]))
# Example usage:
my_list = [ 1 , 2 , 3 , 4 , 5 ]
print (is_increasing(my_list)) # Output: True