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 / mjpeg_stream_multipart_writer.py
Last active December 26, 2023 04:19
MJPEG stream using aiohttp, opencv, multipartwriter
import asyncio
import cv2
from aiohttp import web, MultipartWriter
async def mjpeg_handler(request):
boundary = "boundarydonotcross"
response = web.StreamResponse(status=200, reason='OK', headers={
'Content-Type': 'multipart/x-mixed-replace; '
'boundary=--%s' % boundary,
@james4388
james4388 / stringtree.kt
Last active November 3, 2023 06:20
StringTree Kotlin
import java.lang.IndexOutOfBoundsException
abstract class Node {
var length: Int = 0
abstract fun charAt(index: Int): Char
abstract fun subString(start: Int, end: Int): String
abstract fun delete(index: Int)
}
@james4388
james4388 / string_tree.py
Last active November 3, 2023 04:59
String Tree implementation
from abc import ABC, abstractmethod
class Node(ABC):
length: int = 0
@abstractmethod
def char_at(self, index: int) -> str:
raise IndexError()
@abstractmethod
@james4388
james4388 / pick-all.js
Created March 30, 2023 22:02
Pick all brick
const wait = async (time) => new Promise((resolve) => setTimeout(resolve, time));
const pickAll = async () => {
const btns = document.querySelectorAll('button[data-test="pab-item-btn-pick"]');
for (const btn of btns) {
btn.click();
await wait(100);
}
}
@james4388
james4388 / deque.ts
Created July 13, 2022 21:50
Deque implementation in TS
interface Queue<T> {
append(item: T);
appendLeft(item: T);
count(): number;
peak(): T;
peakLeft(): T;
pop(): T;
popLeft(): T;
clear();
[Symbol.iterator]();
@james4388
james4388 / parallelMap.ts
Created July 20, 2021 06:47
Map in parallel and only allow to run with concurrencyLimit
type InputWithIndex<I> = [I, number];
async function parallelMap<I, O>(inputs: I[], asyncMapper: (...input: I[]) => O, concurrencyLimit: number = 5): Promise<O[]> {
concurrencyLimit = Math.max(1, concurrencyLimit);
const inputStack: InputWithIndex<I>[] = inputs.map((input: I, index: number): InputWithIndex<I> => [input, index]).reverse();
const results = new Array(inputs.length).fill(undefined);
const workers = new Array(concurrencyLimit).fill(undefined);
async function work() {
if (inputStack.length) {
@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
@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 / readme.md
Last active December 28, 2020 23:51
Solve (jigsaw, fill tile) puzzle by backtracking

Puzzle must have tiles and a final state. Screenshot_20201226-104548

Example:

SMALL_GAME_FINAL = (
    'RB',
    'BR'
)
@james4388
james4388 / recipe: cherry-pick tags
Created December 14, 2020 19:55 — forked from nickfloyd/recipe: cherry-pick tags
To cherry pick from head and commit back into a tag
-from master in working branch
>> git branch [new branch] [tag]
>> git checkout [branch]
-pull commit out and add it to the commit at the top of the tag
>> git cherry-pick [commit] or git cherry-pick [firstcommit]^..[lastcommit] if you have a range
-resolve conflicts
-delete the local tag
>> git git tag -d [tag]
-add a new tag at the head of the old one
>> git tag [tag]