Skip to content

Instantly share code, notes, and snippets.

@JackInTaiwan
Last active December 25, 2022 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackInTaiwan/51823c7b0962a0699d211b543c999794 to your computer and use it in GitHub Desktop.
Save JackInTaiwan/51823c7b0962a0699d211b543c999794 to your computer and use it in GitHub Desktop.
Some Syntax Hints of Python3 for LeetCode practice
# List
x = ["A", "B"]
x.insert(0, "C") # ["C", "A", "B"]
x = [(1, 2), (2, 2), (2, 1)]
x.sort() # [(1, 2), (2, 1), (2, 2)]
from copy import deepcopy
y = x.copy() # shallow copy
y = deepcopy(x) # deep copy
# Set
x = set([0, 1])
y = set([2, 3])
next(iter(x)) # 0
x.pop() # 0
x[0] # Error! __getitem__ is not supported!
x | y # {0, 1, 2, 3}
x - y # {0, 1}
x + y # Error! __add__ is not supported!
x.update(y) # {0, 1, 2, 3}
x.update([0,2,2,3]) # {0, 1, 2, 3}
# Heap
min_heap = [(1, ("A", "B"))]
heapq.heapify(min_heap)
heapq.heappop(min_heap) # (1, ("A", "B"))
heapq.heappush(min_heap, (3, ("A", "B"))) # (1, ("A", "B"))
heapq.heappushpop(min_heap, (4, ("B", "B"))) # (3, ("A", "B"))
# Deque (double-ended queue)
from collections import deque
queue = deque([0, 1])
queue.copy()
queue.append(2) # [0, 1, 2]
queue.appendleft(-1) # [-1, 0, 1, 2]
queue.pop() # 2
queue.popleft() # -1
queue[-1] # 0
for ele in queue:
print(ele) # 0, 1
# SortedList
from sortedcontainers import SortedList
sorted_list = SortedList()
sorted_list.add(target_num)
sorted_list.index(target_num)
# defaultdict
from collections import defaultdict
dd = defaultdict(int)
dd["a"] = 1
dd["a"] # 1
dd["b"] # 0
# Counter
from collections import Counter
counter = Counter(["a", "b", "c", "c"])
list(counter.items()) # [("a", 1), ("b", 1), ("c", 2)]
counter.most_common(1) # [("c", 2)]
counter.most_common(2) # [("c", 2), ("a", 1)]
# Reduce, map and filter function
from functools import reduce # only need to import reduce
x = [1, 2, 3, 2, 1]
y = reduce(lambda a, b: a+b, x) # 9
y = list(map(lambda a: a>1, x)) # [False, True, True, True, False]
y = list(filter(lambda a: a>1, x)) # [2, 3, 2]
# Tell the format of a string
a = ""
b = "5"
c = "5.5"
d = "-5"
e = "a"
f = "a5"
a.isnumeric(), a.isalpha() # False, False
b.isnumeric(), b.isalpha() # True, False
c.isnumeric(), c.isalpha() # False, False
d.isnumeric(), d.isalpha() # False, False
e.isnumeric(), e.isalpha() # False, True
f.isnumeric(), f.isalpha() # False, False
# What are hashable?
x = dict()
x[(3, 4)] # tuple is hashable
x[{3, 4}] # set is unhashable
x[[3, 4]] # list is unhashable
x[{3: 4}] # dict is unhashable
x[(3, 4, (5, 6))] # nested set is hashable
x[(3, 4, [5, 6])] # nested set with list is unhashable
# Some common mistakes
x = "abc"
x[0] = "d" # Error! Str does not support item assignment
x.split() # Unexpected result: still "abc"
list(x) # Desirable result: ["a", "b", "c"]
# Time Complexity
# log1 + log2 + log3 + ... + logn = n*logn
# 1*log1 + 2*log2 + 3*log3 + ... + n*logn = n^2*logn
# Build a heap with N elements: O(N) rather than O(NlogN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment