PyLeet Flashcards

1
Q

How do you get a list of numbers in a particular range?

A

list(range(0,3))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you import Counter?

A

from collections import Counter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you get the highest count item from a Counter?

A

ctr.most_common()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Initialize a list of “False” n times.

A

[False] * n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Initialize a two dimensional list of [0] n * m times.

A

[[0]*n for _ in range(m)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Where would one initialize a variable upon class instantiation?

A

def __init__():

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you loop through two arrays at once, side by side?

A

for i, j in zip(a, b):
print(i, j)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Diff between sort() and sorted()?

A

sort() sorts in place, sorted() returns a new list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does one import components for heap operations?

A

from heapq import heapify, heappush, heappop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you initialize a heap?

A

heapify(my_list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you add an element to a heap?

A

heappush(my_list, new_item)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you remove the next element from a heap?

A

heappop(my_list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to do integer division (no decimal or remainder)?

A

a//b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to get the first index of a character in a string?

A

s.index(‘e’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to get the last index of a character in a string?

A

str.rfind.(‘e’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to get the next index of a character after a given index in a string?

A

s.index(‘l’, 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you count the number of occurences of something in a list?

A

ls.count(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do I replace part of a string?

A

s.replace(‘earl’, ‘world’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are two ways to remove a particular item from a set?

A

fruits.remove(‘apple’), fruits.discard(‘apple’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do you remove the last item from a list?

A

my_list.pop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What’s the difference between the two ways to remove an item from a set?

A

remove throws an error if the item doesn’t exist, discard doesn’t

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you insert an item into a list at a certain index?

A

my_list.insert(3, “Apple”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How to initialize 2 variables at once?

A

x, y = 0, 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you loop through the index and value of a list at the same time?

A

for i, v in enumerate(ls):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do you loop through the key and value of a dictionary?

A

for k, v in d.items():

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What happens if you enumerate a dictionary?

A

It loops through the index # and the keys, as if it were a list of keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What happens if you enumerate a dictionary.values()?

A

Loop through the list of values with index # and value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What’s the syntax for sorting a list of lists using sorted?

A

x = sorted(ls, key=lambda x: x[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What’s the syntax for sorting via a custom sort function?

A

x = sorted(ls, key=lambda x: my_func(x))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What are two ways to reverse a list?

A

ls.reverse(), ls[::-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is bitwise AND?

A

a & b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is bitwise OR?

A

a | b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is bitwise XOR?

A

a ^ b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is bitwise NOT?

A

~a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What is bitwise left shift?

A

a &laquo_space;1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is bitwise right shift?

A

a&raquo_space; 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How do you display the bytes of an integer?

A

bin(n)[2:]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What’s the difference between remove() and pop()?

A

remove() removes by value, pop() removes by index. If no argument is passed to pop(), it removes the last item in the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What’s the fastest way to tally/ count the number of occurances of something in a list?

A

from collections import Counter
Counter(myString/myList)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

How does one import Counter?

A

from collections import Counter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

How does one update a Counter with an additional list or string?

A

my_counter.update(new_list_or_string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

How does one access a counter value?

A

my_counter[key]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

How do you loop through items in a Counter?

A

for k, v in my_counter.items()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

How does one get the n most common items from a counter? What is the resultant format?

A

my_counter.most_common(2) : [(key1, count1), (key2, count2)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

How to import SortedList or SortedSet?

A

from sortedcontainers import SortedList

46
Q

How to add an item to a SortedSet/List/

A

sorted_collection.add()

47
Q

How to add multiple items to a SortedSet or SortedList?

A

sorted_collection.update()

48
Q

how to drop an item from a SortedSet or SortedList?

A

sorted_collection.discard(value_to_remove)

49
Q

Why does this code not work for initializing a multi dimensional array: [[False] * n] * m

A

It creates m rows all pointing to the same list, so that if you were to change the first item to True, then all first items of each row would appear to be True.

50
Q

How does one unpack (“apple”,
“banana”, “pear”) into 3 variables at one?

A

f1, f2, f3 = my_fruits

51
Q

Can you unpack only the first n items of a list of length n+ m? If so how?

A

Yes: a, b, *_ = my_list

52
Q

Create simple lamba function to add two numbers?

A

add = lamba a, b: a+ b

53
Q

How would one map a list of nums to a list of its squares?

A

squares = map(lambda x: x*x, nums)

54
Q

How would one filter out even numbers from a list?

A

even = filter(lambda x: x%2==0, nums)

55
Q

How would one use ANY to determine if any numbers are even?

A

any_even = any(x%2==0 for x in nums)

56
Q

How would one use ALL to determine if all numbers are positive?

A

all_positive = all(x > 0 for x in nums)

57
Q

Create a generator that yields items from the list my_nums.

A

gen = (n for n in my_nums)

58
Q

What’s the difference between tuple comprehension and creating a generator?

A

tuple comprehension say: tuple(x for x in iteratable), whereas a generator is (x for x in iterable)

59
Q

Create a simple countdown generator using yield

A

def countdown_gen:
while(n > 0):
yield n
n -= 1

60
Q

Two ways to create a set?

A

my_set = set(), my_set = {1, 2, 3}

61
Q

What does this create: my_set = {1, 2, 3, 1}

A

A set with only 1, 2, 3 (second instance of 1 is dropped)

62
Q

Give an example of set comprehension creating a set with numbers 1-5

A

my_set = {n for n in range(1, 6)}

63
Q

What is a type annotation?

A

Specifies the return type of a function:

def multiply(a, b) -> int:

64
Q

What datatype is used for decimals in python?

A

float

65
Q

How does one initialize positive infinity?

A

float(‘inf’)

66
Q

What is the correct syntax for getting a sorted copy of a list in reverse order?

A

sorted_list = sorted(original, reverse=True)

67
Q

How does one remove the first item from a python list?

A

item = my_list.pop(0)

68
Q

How does one loop the range 1-5 reversed? (2 ways)

A
  1. for i in reversed(range(1,6)):
  2. for i in range(5,0,-1):
69
Q

What are 3 ways to copy a list in python?

A
  1. new_list = orig[:]
  2. new_list = list(orig)
  3. from copy import copy
    new_list = copy(orig)
70
Q

What happens if you enumerate a string in python?

A

for i, ch in enumerate(s):

loops through both the index and character.

71
Q

What python data structsure can be used on a LRU type cache and why?

A

OrderedDict, because it tracks the order of the entries into the dictionary.

72
Q

How does one import OrderedDict?

A

from collection import OrderedDict.

73
Q

How does one move an item in an OrderedDict to the end of the dictionary?

A

my_dict.move_to_end(key)

74
Q

How does one move an item in an OrderedDict to the beginning of the dictionary?

A

my_dict.move_to_end(key, last=False)

75
Q

How does one import SortedList?

A

from sortedcontainers import SortedList

76
Q

How does one give a custom sort function to a SortedList?

A

def my_sort_func(x):
return x[1]
custom_sl = SortedList(key=my_sort_func)

77
Q

How does one view the first member of a SortedList?

A

sorted_list[0]

78
Q

How does one remove the first element of a SortedList?

A

sorted_list.pop(0)

79
Q

What does __slots__ do?

A

Allocates memory slots on a class instance.

80
Q

Show can example of slots statement for rows and columns on a class instance.

A

__slots__ = (“rows”, “columns”)

81
Q

What python method checks if a character is a letter?

A

ch.isalpha()

82
Q

How to check if a character is a number in python?

A

ch.isdigit()

83
Q

How do check if a character is alphanumeric in python?

A

ch.isalnum()

84
Q

How might one filter out no alhpanumeric characters using filter()?

A

new_string = ““.join(filter(lambda ch:ch.isalnum(), my_str))

85
Q

What are 3 ways to append two lists in python?

A
  1. orig += additional_list
  2. combined = l1 + l2
  3. orig,extend(additional_list)
86
Q

What’s the difference between set comphrehensions and dictionary comprehensions in python?

A

Both use {}, but dict comp uses {k:v for k in iterable} while set comp uses {k for jk i iterable}

87
Q

What would be a way to map a list of numbers to their percentage of the sum total? (use map)

A

total = sum(nums)
pcts = list(map(lambda x : x/total, nums))

88
Q

How does one tell if a character is a number in python?

A

ch.isdigit()

89
Q

How does one sort a list in place?

A

my_list.sort()

90
Q

What’s the most efficient data structure for operations at the beginning and end of a list? How to import?

A

from collection import deque

91
Q

What are the 4 main ops of a deque?

A

pop() - remove from end
popleft() - remove from beginning
append() - add item to end
appendleft() - add to front of list

92
Q

How does one instantiate a deque?

A
93
Q

What is the result of 5&3?

A

011 & 101 = 001 == 1

94
Q

What is the result of 5 | 3?

A

011 or 101 = 111 == 7

95
Q

What is the result of 5^3

A

011 not-or 101 = 110 = 6

96
Q

What is the result of ~5

A

010 = 2

97
Q

What does a not operator do?

A

Flips the bits

98
Q

How does exclusive OR work in python?

A

Only becomes true if one OR the other bit is true.

99
Q

What is the exclusive or operator in python?

A
100
Q

What is 5 &laquo_space;1?

A

101 shifted left one bit = 1010 = 10

101
Q

What is 5&raquo_space; 1?

A

101 shifted right one bit = 010 = 2

102
Q

What’s the fastest way to integer dive by 2 in python?

A

x&raquo_space;= 2

103
Q

Can you left shift in place?

A

x&raquo_space;=1

104
Q

Can you right shift in place?

A

x «= 1

105
Q

Describe how to find the minimum spanning tree (minimium weights to connect all graph nodes) (Krustkal)

A
  1. Sort graph by edges
  2. Create a parent table of length N (vertices)
  3. Loop through edges, track the parents of each V, and add any edge that isn’t in a connected subgraph.
106
Q

What is the big O complexity of Kruskal’s algo (Min Spanning Tree?) Why?

A

E * log(E) # of edges
Sort edges = e*log(e)
loop through n

107
Q

What’s another MST, Prim’s?

A
  1. Make a dict of all nodes/weights to each node.
  2. Start arb node, heapify weights of that node.
  3. Choose lowest weight, add that to MST, then add remaining nodes from that node to the heap
108
Q

How do you remove the first item of an OrderedDict?

A

my_ordered_dict.popitem(last=False)

109
Q

How do we sort a list of 3 numbers?
[[1,2,3], [2,3,4], [1,0,2]]

A

my_list.sort(key = lambda x: (x[0], -x[1], x[2]))

110
Q

in python how do you import deque?

A

from collections import deque

111
Q

How do you instantiate a deque?

A

my_deque = deque()