Skip to content

Instantly share code, notes, and snippets.

View lotabout's full-sized avatar

Jinzhou Zhang lotabout

View GitHub Profile
@lotabout
lotabout / Launcher.java
Created March 12, 2024 11:10
Java Agent example
package me.lotabout;
import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
@lotabout
lotabout / ping.py
Last active December 24, 2023 06:08
Simple ping implementation in python3 for practicing TCP/IP
#!/usr/bin/env python3
import os
import struct
import socket
import time
def checksum(bytestr):
# ref
# - https://en.wikipedia.org/wiki/IPv4_header_checksum
@lotabout
lotabout / traceroute.py
Created March 13, 2021 07:13
Simple traceroute implementation in Python3
#!/usr/bin/env python3
import struct
import socket
import time
# Need to run with root permission cause RAW socket is used
# ref:
# - https://dnaeon.github.io/traceroute-in-python/
@lotabout
lotabout / MyBenchmark.java
Last active December 24, 2023 05:54
CompletableFuture.supplyAsync profile
package me.lotabout;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
@lotabout
lotabout / expanding_nebula.py
Created October 5, 2017 03:19
Solution for google foobar: Expanding Nebula
def generate(c1,c2,bitlen):
a = c1 & ~(1<<bitlen)
b = c2 & ~(1<<bitlen)
c = c1 >> 1
d = c2 >> 1
return (a&~b&~c&~d) | (~a&b&~c&~d) | (~a&~b&c&~d) | (~a&~b&~c&d)
from collections import defaultdict
def build_map(n, nums):
mapping = defaultdict(set)
@lotabout
lotabout / mutable_closure.rs
Created June 11, 2016 08:46
Rust closure as mutable callback.
use std::thread;
fn do_something<'a>(mut callback: Box<FnMut() + 'a>) {
// check http://stackoverflow.com/questions/35651279/error-closure-may-outlive-the-current-function-but-it-will-not-outlive-it
// about why adding 'a will work
callback();
}
fn main() {
let mut ret = 0;
@lotabout
lotabout / send-to-download.js
Last active August 12, 2022 11:33
Greasemonkey script for send content to backend localhost.
// ==UserScript==
// @name Post test
// @namespace jinzhou
// @description Post Test
// @include *
// @version 1
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// ==/UserScript==
@lotabout
lotabout / primes.clj
Last active March 7, 2022 15:58
Infinite sequence of prime numbers in Clojure.
; Sieve of Eratosthenes
; Checkout https://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
(def primes
(concat
[2]
(lazy-seq
(let [prime-inner
(fn prime-inner [x table]
(if (table x)
(prime-inner (inc x)
@lotabout
lotabout / tmux-tiled.py
Created June 23, 2017 10:31
DWM like pane management for tmux
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import re
import json
import sys
from math import ceil
TMUX_BUFFER_NAME = 'dwm'
@lotabout
lotabout / google-translate.py
Last active November 11, 2020 13:07
Translate texts
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# translate utility that utilize google translator, support python2 & python3
# Note that the order or arguments in the URL matters.
try:
from urllib import urlencode
except:
from urllib.parse import urlencode