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 / 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 / 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]
@james4388
james4388 / clean_sql.py
Created June 19, 2020 22:44
Clean up sql comment
def clean_comment(sql: str) -> str:
lines = sql.split('\n')
output = []
for line in lines:
escape = False
quote = False
i = 0
buffer = []
n = len(line)
@james4388
james4388 / split_array.py
Created June 1, 2020 19:05
Split array (not reorder item) in two equal sum. My stupid moment
from random import randint
def split(nums):
total = sum(nums)
if total % 2 != 0:
return False
half = total // 2
sub_sum = 0
for i, num in enumerate(nums):
sub_sum += num
@james4388
james4388 / parallelMergeSort.go
Last active May 9, 2020 05:31
Merge sort using coroutine. Not really parallel but just for fun
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"sync"