Skip to content

Instantly share code, notes, and snippets.

View james4388's full-sized avatar
🎯
Focusing

Nhu H Trinh james4388

🎯
Focusing
View GitHub Profile
@james4388
james4388 / base.py
Last active December 31, 2020 18:45
Solve multiple puzzles that have initial states and final state
from typing import *
from abc import ABCMeta, abstractmethod
from collections import deque
class PuzzleSolver(metaclass=ABCMeta):
"""Abstract class for solve puzzle that have initial state and end state
Simple breath first search
from any state -> generate next reachable state and eliminated duplicate state
until reach goal or max depth reached
@james4388
james4388 / water_pour.py
Created April 8, 2020 05:57
Pouring water problem
"""
Inital we have N empty cup with capacity(c1, c2, c3... cn)
Posible move:
empty one cup
fill one cup
pour from one cup to another
We have unlimited water source, find a way to fill a target amount
"""
from collections import deque
@james4388
james4388 / median_sorted.py
Last active February 8, 2020 01:33
Median of two sorted arrays
"""
Merged to sorted array O(M + N) then find median O(1)
Space: O(M + N) for temporary array
"""
def merge(first: List[int], second: List[int]) -> List[int]:
M = len(first)
N = len(second)
i = j = 0
result = []
while i < M and j < N:
from collections import deque, defaultdict
def tarjan_recursive(graph):
unvisited = set(graph.keys())
low_link = {}
visited_ids = {} # Node id, also keep track of visited node
stack_set = set()
stack = []
groups = []
@james4388
james4388 / DOMStore.js
Created January 8, 2020 06:26
Map like structure that can has key is an object or DOM Node.
/* Using array to store and search for key. Time complexity O(n) */
class DOMStoreArray {
constructor () {
this.data = [];
}
find (node) {
for (let item of this.data) {
if (item[0] === node) {
return item;
@james4388
james4388 / wrapTags.js
Last active January 8, 2020 00:06
Wrap HTML Tag to a string for given ranges
/*
apply all tag from tags to the string s
const tags = [
[0, 4, 'i'], // start at index, end at index, style
[6, 9, 'b'],
[7, 10, 'u']
]
const s = 'Hello World!'
return html string: <i>Hell</i>o <b>W<u>orl</u></b><u>d</u>!
At one index there will be only one open or close;
@james4388
james4388 / folder_fun.py
Last active December 11, 2019 18:01
Have fun with graph folder structure
from collections import deque
'''
Graph problems
A
C
E
F
D
G
@james4388
james4388 / EventEmitter.js
Last active November 27, 2019 01:37
Implement EventEmitter
class EventEmitter {
constructor (isAsync = false) {
this.events = new Map();
this.isAsync = isAsync; // Allow to notify listener async
}
/*
* Subcribe a callback to an event. Callback will not be duplicate.
* return unsibcribe function
* Usage: const unsub = ee.subcribe(eventName, callback);
@james4388
james4388 / reverse_print.py
Last active November 14, 2019 07:11
Print linked list reversed in various ways
class Node:
def __init__(self, value):
self.value = value
self.next = None
def length(head):
"""Get linked list length"""
count = 0
node = head
while node:
@james4388
james4388 / segment_tree.py
Last active January 11, 2021 06:39
Segment Tree
def range_sum(tree, left_child, right_child):
return tree[left_child] + tree[right_child]
def build_segment_tree(arr, tree=None,
merge_fn=range_sum,
node_index=0, lo=0, hi=None, initial=None):
"""
Build a segment tree based on arr of leaf node
tree will have the size of 4 * len(arr)
node_index is tree index