Skip to content

Instantly share code, notes, and snippets.

View orlp's full-sized avatar
⚠️
This user's social credit score is unknown.

Orson Peters orlp

⚠️
This user's social credit score is unknown.
  • Leiden, Netherlands
View GitHub Profile
from collections import defaultdict
import sys
import pulp
class keydefaultdict(defaultdict):
def __missing__(self, key):
ret = self[key] = self.default_factory(key)
return ret
def horiz_disjoint(xvals):
@orlp
orlp / mazejudge.py
Last active January 17, 2021 10:58
import collections
import numpy as np
import scipy.sparse
import scipy.sparse.linalg
import sys
import imageio
import io
import base64
class MazeJudge:
@orlp
orlp / vose_alias_method.py
Last active November 19, 2020 02:55
A succint numpy implementation of Vose's Alias Method, an O(n) construction O(1) sampling time algorithm for a fixed, weighted distribution.
import numpy as np
from collections import deque
class VoseAliasMethod:
# Vose's Alias Method as described at https://www.keithschwarz.com/darts-dice-coins/.
def __init__(self, weights):
pmf = weights / np.sum(weights)
self.n = pmf.shape[0]
self.prob = np.zeros(self.n, dtype=np.float64)
self.alias = np.zeros(self.n, dtype=np.int64)
use slotmap::{SlotMap, SecondaryMap, new_key_type, Key};
use std::collections::HashSet;
new_key_type! {
struct BucketKey;
}
new_key_type! {
struct IndexKey;
}
use std::collections::{HashSet, HashMap};
#[derive(Clone, Debug)]
struct CountBucket {
prev: i64,
next: i64,
indices: HashSet<usize>,
}
import numpy as np
def expected_time_to_solve(N):
A = np.zeros((4*(N+2), 4*(N+2)))
# (n, forward) => (4n + 0), (n, backward) => (4n + 1), (n, inside) => (4n + 2), (n, facewall) => (4n + 3)
for i in range(4*(N+2)):
if i < 4:
A[i,4] = 1
elif i >= 4*(N+1):
A[i,i] = 1
import itertools as it
import networkx as nx
import math
K = 3
N = math.factorial(K) + K - 1
print("Constructing sets")
left = list(it.permutations(range(N)))
right = sorted({(p[:cloth] + p[cloth+K:], cloth) for p, cloth in it.product(it.permutations(range(N)), range(N-K+1))})
// Original idea by Mikkel Gjoel (https://computergraphics.stackexchange.com/a/5952/10515),
// simplified to one readable function.
// Demo at https://www.shadertoy.com/view/Wts3zH.
// Dithers and quantizes color value c in [0, 1] to the given color depth.
// It's expected that rng contains a uniform random variable on [0, 1].
uint dither_quantize(float c, uint depth, float rng) {
float cmax = float(depth) - 1.0;
float ci = c * cmax;
" quick replace occurences
let g:should_inject_replace_occurences = 0
function! MoveToNext()
if g:should_inject_replace_occurences
call feedkeys("n")
call repeat#set("\<Plug>ReplaceOccurences")
endif
let g:should_inject_replace_occurences = 0
endfunction
@orlp
orlp / gist:4487500
Last active August 9, 2018 13:51
Kademlia security protocol

Kademlia security protocol

In this little description we use the following notation:

+ concatenation
hash(x) the SHA-1 hash of x
sign(x, sk) the Ed25519 signature generated by signing x using signing key sk
verify(signature, x, vk) verifying the Ed25519 signature of x using veryfing key vk

There is a central identity server everyone trusts. The server has an Ed25519 keypair, the master keypair. Everyone possesses the master verification key (MVK), the signing key is kept secret (MSK).