Skip to content

Instantly share code, notes, and snippets.

@jonathanagustin
jonathanagustin / topKFrequent1.py
Last active April 29, 2020 10:15
Top K Frequent Words - leetcode
"""
https://leetcode.com/problems/top-k-frequent-words/
Given a non-empty list of words, return the k most frequent elements.
Your answer should be sorted by frequency from highest to lowest.
If two words have the same frequency, then the word with the
lower alphabetical order comes first.
collections.Counter([iterable-or-mapping]) counts hashable objects
@jonathanagustin
jonathanagustin / removePunctuation.py
Last active April 29, 2020 09:57
Clean way of removing punctuation from a string
import string
# make table for translate() to remove punctuation
table = str.maketrans('', '', string.punctuation)
my_string = "!!!asdf"
new_string = my_string.translate(table)
print(new_string)
@jonathanagustin
jonathanagustin / buildGraph.py
Last active July 6, 2020 07:08
Python - build graph with list of edges
def buildGraph(edges):
from collections import defaultdict
graph = defaultdict(list)
for u, v in edges:
graph[u].append(v)
graph[v].append(u) # delete this line if directed graph
@jonathanagustin
jonathanagustin / QueueVsDeque.md
Last active April 30, 2020 06:18
Python Queue vs Deque - pop() vs popleft()

Tip: To make the code more efficient, you can use the deque object from the collections module instead of a list, for implementing queue. This way you can use the popleft() method instead of the pop(0) built-in function on queue. This will result in a quicker code as popleft() has a time complexity of O(1) while pop(0) has O(n).

Deque (Doubly Ended Queue) in python is implemented using the module “collections“. Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.

@jonathanagustin
jonathanagustin / timingDecorator.py
Created May 2, 2020 09:18
Python - time a function with a decorator
from functools import wraps
from time import time
def measure(func):
@wraps(func)
def _time_it(*args, **kwargs):
start = int(round(time() * 1000))
try:
return func(*args, **kwargs)
finally:
end_ = int(round(time() * 1000)) - start
@jonathanagustin
jonathanagustin / maxHeap.py
Last active May 2, 2020 12:17
Python Max Heap
import heapq
class Neg():
def __init__(self, x):
self.x = x
def __cmp__(self, other):
return -cmp(self.x, other.x)
def maxheappush(heap, item):
@jonathanagustin
jonathanagustin / maxOccurredIntegerInNRanges.cpp
Created June 5, 2020 16:02
maximum occurred integer in n ranges
int main() {
int Q; cin >> Q;
map<long long, int> points;
for (int i = 0; i < Q; i++) {
long long l, r; cin >> l >> r;
points[l]++;
points[r + 1]--;
}
int best = 0, cur = 0;
@jonathanagustin
jonathanagustin / letterOrderDict.py
Last active June 6, 2020 20:06
Python way of generating letter-order key-value dictionary
order = 'abcdefghijklmnopqrstuvwxyz'
character_order = {c: i for i, c in enumerate(order)}
@jonathanagustin
jonathanagustin / RecursiveBFS.py
Created July 21, 2020 04:04
Basic Recursive BFS
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
@jonathanagustin
jonathanagustin / IterativeBFS.py
Created July 21, 2020 04:14
Basic Iterative BFS
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]: