Skip to content

Instantly share code, notes, and snippets.

View magiskboy's full-sized avatar

Nguyễn Khắc Thành magiskboy

View GitHub Profile
function main() {
const output = groupNode(node2);
console.log(output);
}
/**
* Case 1: Both rows can be middle alignment
*/
const node1 = {
@magiskboy
magiskboy / main_1.py
Created July 5, 2023 10:08
Optimize Python code without extensions
from queue import LifoQueue
import time
d = [
(0, 1),
(1, 0),
(-1, 0),
(0, -1),
]
@magiskboy
magiskboy / parser.py
Created June 25, 2023 13:59
Simple HTTP parser, 1M req/s
# STATE
STATE_METHOD = 0
STATE_PATH = 1
STATE_HTTP_VERSION = 2
STATE_HEADER = 3
STATE_BODY = 4
class ParserBase:
def __init__(self, protocol):
@magiskboy
magiskboy / sandbox.js
Created June 20, 2023 07:48
Example for sandbox environments in Javascript
const { Console } = require("console");
const stream = require("stream");
const sandboxProxies = new WeakMap();
function compileCode(src) {
src = 'with (sandbox) {' + src + '}';
const code = new Function('sandbox', src);
return function(sandbox) {
@magiskboy
magiskboy / application.py
Last active June 20, 2023 02:19
The simple HTTP server is based on socket programming
from urllib.parse import parse_qs
import json
class Headers(dict):
def __setitem__(self, k, v):
super().__setitem__(k.lower(), v)
def __getitem__(self, k):
return super().__getitem__(k.lower())
import threading
import multiprocessing
import time
import requests
def measure(func):
def f(*args, **kwargs):
start = time.time()
ret = func(*args, **kwargs)
print(f"{func.__name__} takes {time.time()-start} seconds")
<html>
<head>
<title>Kmean</title>
</head>
<body>
<canvas id="kmean" width="640" height="480"></canvas>
<script type="application/javascript">
const canvas = document.getElementById("kmean");
const ctx = canvas.getContext("2d");
const lib = require("./lib");
/**
* @param {number} id
* @param {(number) => Promise<import('./lib').CacheValue>} fetcher
* @typedef {Object} Product
* @property {number} id
* @returns {Promise<Product>|undefined}
*/
async function getProductWithCached(id, fetcher) {
import ts, { SyntaxKind } from "typescript";
const source = `
function fib(n: number): number {
if (n < 2) return n;
return fib(n - 2) + fib(n - 1);
}
console.log("foo");
class Node:
def __init__(self, key, data):
self.key = key
self.data = data
self.children = []
def display(self, level=0):
tab = " " * (level * 3)
print(tab + self.data)
for child in self.children: