Skip to content

Instantly share code, notes, and snippets.

View 607011's full-sized avatar
🏁
Yes, please?!

Oliver Lau 607011

🏁
Yes, please?!
View GitHub Profile
@607011
607011 / tuple_hasher.hpp
Created December 15, 2023 16:07
std::tuple hasher
#ifndef __TUPLE_HASHER_HPP__
#define __TUPLE_HASHER_HPP__
#include <tuple>
#include <functional>
template <class T>
inline void hash_combine(std::size_t &seed, T const &v)
{
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
@607011
607011 / to_mp4_mac.sh
Created November 1, 2023 08:25
Convert video to a format that can be played with macOS's Quicktime Player
ffmpeg -i input.mov \
-y \
-vcodec libx264 \
-pix_fmt yuv420p \
output.mp4
@607011
607011 / permutations.js
Created August 6, 2023 13:45
All permutations
function* allPermutations(arr) {
const c = new Array(arr.length).fill(0);
let i = 1;
while (i < arr.length) {
if (c[i] < i) {
yield [...arr];
const k = i % 2 && c[i];
[arr[i], arr[k]] = [arr[k], arr[i]];
++c[i];
i = 1;
@607011
607011 / rc4.js
Created July 11, 2023 08:42
RC4 encoder/decoder
function rc4(msg, key) {
// If you need to encode strings instead of `Uint8Array`s you have to
// convert them with `new TextEncoder().encode('your message')` before
// calling this function.
console.assert(msg instanceof Uint8Array);
console.assert(key instanceof Uint8Array);
// setup S-box from key
let S = [...new Uint8Array(256).keys()];
let j = 0;
@607011
607011 / int2roman.py
Created May 26, 2023 12:49
Integer to Roman number literals; O(1) solution for constraint 0 < num < 4000
def int2roman(num: int) -> str:
ONES = ["","I","II","III","IV","V","VI","VII","VIII","IX"]
TENS = ["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"]
HUNDREDS = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"]
THOUSANDS = ["","M","MM","MMM"]
return THOUSANDS[num//1000] + HUNDREDS[(num%1000)//100] + TENS[(num%100)//10] + ONES[num%10];
@607011
607011 / int2roman.py
Last active May 26, 2023 12:39
Convert integer to Roman number literals
I2R = {1: 'I', 2: 'II', 3: 'III', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 20: 'XX', 30: 'XXX', 40: 'XL', 50: 'L', 90: 'XC', 100: 'C', 200: 'CC', 300: 'CCC', 400: 'CD', 500: 'D', 900: 'CM', 1000: 'M', 2000: 'MM', 3000: 'MMM'}
Candidates = [3000, 2000, 1000, 900, 500, 400, 300, 200, 100, 90, 50, 40, 30, 20, 10, 9, 5, 4, 3, 2, 1]
def int2roman(num: int) -> str:
roman = ''
while num > 0:
d = next(x for x in Candidates if num >= x)
num -= d
roman += I2R[d]
return roman
@607011
607011 / gist:5b94ba0f052f3b888889ea3ee1910be9
Created April 3, 2020 07:21
kill zombie's parent process
kill -KILL $(ps -A -ostat,ppid | awk '/[zZ]/{ print $2 }')
@607011
607011 / -Logger-as-Singleton-for-C++.md
Last active May 23, 2019 13:24
Simple log-to-file singleton

Logging facility for C++

Implemented as a singleton (eager as well as lazy).

main.cpp shows how to use it.

Compile with

cmake .
@607011
607011 / total.sh
Created January 30, 2019 15:08
Sum up file sizes
#!/bin/sh
fileglob=.
total=0
for size in $(ls -l ${fileglob} | tr -s ' ' | cut -d ' ' -f 5) ; do
total=$(( ${total} + ${size} ))
done
echo ${total}
@607011
607011 / endian.go
Created January 28, 2019 09:25
Get Endianness
func nativeEndianness() binary.ByteOrder {
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xabcd)
switch buf {
case [2]byte{0xcd, 0xab}:
return binary.LittleEndian
case [2]byte{0xab, 0xcd}:
return binary.BigEndian
default:
panic("Could not determine native endianness.")