forCodingInterviews Flashcards
if there are duplicate elements x in a list, then what does a.remove(‘x’) remove?
the first occurrence of x
finding the intersection of two sets
result = s1.intersection(s2) s1 & s2
finding the union of two sets
resul = s1.union(s2) or s1 | s2
how to iterate over a set?
for val in set: print(val)
convert a set into a list
list(s)
set membership
if element in set
dict membership
id key in d
list membership
if element in list
what does a filter function return and what’s one way to make it usable
it returns a filter object and can convert into list using list(filter(function, iterable))
can you use a filter object with a set?
yes
how to create a list of tuples from a dictionary such that a tuple is (key, value)?
list(dict.items());
adding an element to a tuple
remmeber tuples are immutable so t2 = t1 + (newElement,) REMEMBER THE COMMA AT THE END. This is creating a new tuple since tuples are immutable.
inserting element at a particular location n in a tuple
t3 = t2[:n] + (‘a’,) + t2[n:]
removing an element froma particular location n in a tupel
t3 = t1[:n] + t1[n+1:]
what objects pass by reference?
all mutable objects, i.e. list, set and dict. Non mutable objects are passed by value so any changes to those args inside the function will not be reflected outside.
adding elements to a set
a.add(2)
whats the difference between b=a and b = a[:] where a is a list
b=a –> any changes to a will be reflected in b
b = a[:] –> creates a copy of a so changes in a will not be reflected
for nested lists use deepcop
what are the largest and smallest numbers in python?
float(‘inf’) and float(‘-inf’)
how do you replace a char c in a string s with char d.
s.replace(c,d)
what is time complexity of python sort function
nlogn - uses Timsort
creating permutations (combinations) in python
from itertools import permutations
p = permutations(list,r)
for combination use combinations
returns a list of tuples, each containing a permutation (or combination)
check if n is power of 2
n & (n-1) == 0 and n not 0
check if power of 4
check for power of 2 and (n-1) % 3 == 0
if s is a string what does sorted(s) do?
splits characters of s into a list and sorts it. If there are numbers they come first
how to convert a string of 0s and 1s into a base 10 integer?
i = int(s,2) assuming it’s currently binary
how to convert all chars of a string to upper case?
s.upper()
how to convert all chars of a string to lower case?
s.lower()
how to ascertain if all chars of a string are digits?
s.isdigit()
how to convert all chars of a string are alphabets?
s.isalpha() will return false even if there is one space
convert a character is ascii representation?
ord(char)
convert an integer to a char using ascii representation
chr(int)
convert an integer into a string representation
str(int)
how to create a named tuple
from collections import namedtuple
Node = namedtuple(“Node”, [“key”, “value”])
new_node = Node(‘k’, 1)
when to use == and when to use “is”
except when checking if a variable is None, use ==
what is the default data types of python division operation?
float
what if you want python to return result of division in integer type and not float?
use //
if you want python to evaluate default arguments fresh everytime it is called, what coding pattern to use?
func (default =None): if not default: default = desired_value
what objects are hashable in python
all immutale objects specifically strings, tuples, integers. All mutable objects such as list, set and dict are not hashable.
how to sort a list based on a function f of it’s values (rather than the value itself)
a.sorted(key = lambda x: f(x))
s1 nd s2 are two sets; get their symmetric difference. elements in set s1 only or s2 only.
A ^ B
python random number generator
randrange(a,b,step); random number will be equal step a < b. However to get random element from list use choice(list)
create a dictionary with default value empty list
d = defaultdict(list)
creating a nested default dictionary
defaultdict(lambda: defaultdict(list))
create a list with cumulative sums
from itertools import accumulate