Python Notes - cocoder39/coco39_LC GitHub Wiki
dictionary value is mutable
>>> mp = {3: [5, 4, 3]}
>>> mp[3].sort()
>>> print(mp)
{3: [3, 4, 5]}
default valu
>>> d = {1: 4}
>>> d.get(2, 4)
4
iterate dict reference: https://realpython.com/iterate-through-dictionary-python/
>>> for key in a_dict:
>>> print(key, '->', a_dict[key])
>>> for item in a_dict.items():
>>> print(item)
>>> for key, value in a_dict.items():
>>> print(key, '->', value)
>>> for key in a_dict.keys():
>>> print(key, '->', a_dict[key])
>>> for value in a_dict.values():
>>> print(value)
Counter
>>> s = collections.Counter('gallahad')
>>> print(s)
Counter({'a': 3, 'l': 2, 'h': 1, 'g': 1, 'd': 1})
deque
>>> queue = collections.deque()
>>> queue.append(1)
>>> queue.append(2)
>>> queue.appendleft(3)
>>> print(queue)
deque([3, 1, 2])
>>> queue.popleft()
3
>>> queue.pop()
2
>>>
set
>>> a = set([1,2,3])
>>> b = [1,2,3,4]
>>> a.update(b)
>>> a
{1, 2, 3, 4}
sort
from functools import cmp_to_key
def cmp_func(x, y):
"""
return a negative value (< 0) when the left item should be sorted before the right item
return a positive value (> 0) when the left item should be sorted after the right item
return 0 when both the left and the right item have the same weight and should be ordered "equally" without precedence
"""
if x + y > y + x:
return -1
elif x == y:
return 0
else:
return 1
# Sort nums by cmp_func decreasingly.
nums.sort(key = functools.cmp_to_key(cmp_func))
>>> data = [(1,2,3),(1,2,1),(1,1,4)]
>>> sorted(data, key=lambda d: (d[0],d[1]), reverse=False)
[(1, 1, 4), (1, 2, 3), (1, 2, 1)]
heap
time complexity of `heapq.heapify()` is O(N) instead of O(log N)
deep copy https://stackoverflow.com/questions/17873384/how-to-deep-copy-a-list
- copy.deepcopy()
- list:
new_list = old_list[:]
ornew_list = old_list.copy()
are shallow copies- they do not recursively makes copies of the inner objects. It only makes a copy of the outermost list, while still referencing the inner lists from the previous variable, hence, when you mutate the inner lists, the change is reflected in both the original list and the shallow copy. But the outer layer is a deep copy. Overall, don't rely on this for deep copy
random
- random.random() gives you a random floating point number in the range [0.0, 1.0)
- random.uniform(a, b) gives you a random floating point number in the range [a, b]
- random.randrange(a,b) gives you a random integer number in the range [a, b)
- random.randint(a,b) gives you a random integer number in the range [a, b]
Constructor
class Solution:
def __init__(self, a):
self.a = a
def func(self, n: int) -> int:
return n - self.a
split
>>> s = "1:end:5"
>>> print(s.split(':'))
['1', 'end', '5']
>>> a = [1, 2, 3, 4, 5]
>>> print(a[::-1])
[5, 4, 3, 2, 1]
>>>
list extend
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a.extend(b)
>>> print(a)
[1, 2, 3, 4, 5, 6]
private method
# Declaring public method
def fun(self):
print("Public method")
# Declaring private method
def __fun(self):
print("Private method")
Reference
- Main method and import: https://www.educative.io/edpresso/what-is-the-main-method-in-python?affiliate_id=5082902844932096&utm_source=google&utm_medium=cpc&utm_campaign=grokking-ci&utm_term=&utm_campaign=Grokking+Coding+Interview+-+USA%2B&utm_source=adwords&utm_medium=ppc&hsa_acc=5451446008&hsa_cam=1871092258&hsa_grp=84009716779&hsa_ad=396821895536&hsa_src=g&hsa_tgt=aud-470210443636:dsa-1287243227899&hsa_kw=&hsa_mt=b&hsa_net=adwords&hsa_ver=3&gclid=Cj0KCQjw9O6HBhCrARIsADx5qCTDdxeMpVlZYq6glt_ggA6IjNCJnYpZM4WV_0FlVRIzyy0SYrJjsFAaAkTWEALw_wcB
- Unit Testing: https://realpython.com/python-testing/
- Google Python Style Guidance: https://google.github.io/styleguide/pyguide.html