Skip to content

Instantly share code, notes, and snippets.

Avatar

Orson Peters orlp

View GitHub Profile
@orlp
orlp / vose_alias_method.py
Last active Nov 19, 2020
A succint numpy implementation of Vose's Alias Method, an O(n) construction O(1) sampling time algorithm for a fixed, weighted distribution.
View vose_alias_method.py
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)
View count_tracker.rs
use std::collections::{HashSet, HashMap};
#[derive(Clone, Debug)]
struct CountBucket {
prev: i64,
next: i64,
indices: HashSet<usize>,
}
View count_tracker.rs
use slotmap::{SlotMap, SecondaryMap, new_key_type, Key};
use std::collections::HashSet;
new_key_type! {
struct BucketKey;
}
new_key_type! {
struct IndexKey;
}
View mazejudge.py
import collections
import numpy as np
import scipy.sparse
import scipy.sparse.linalg
import sys
import imageio
import io
import base64
class MazeJudge:
View rc2maze.py
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
View match.py
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))})
View Villagers.md

Villager mechanics in 1.14. All this data has been deduced by using the following commands:

/data get entity @e[type=minecraft:villager,limit=1,sort=nearest] Brain.memories
/execute as orlp at orlp run tellraw @s {"nbt":"Brain.memories","entity":"@e[type=villager,sort=nearest,limit=1]"}

A villager is awake from 0-11999 and tries to sleep between 12000 and 23999.

A villager will claim a bed if:

View quantize_with_dither.glsl
// 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;
View gen.py
from collections import namedtuple, defaultdict
EarleyItem = namedtuple("EarleyItem", ["rule", "dot", "origin"])
def item_advance(item):
return EarleyItem(rule=item.rule, dot=item.dot + 1, origin=item.origin)
class Generator:
def __init__(self, G, terminals):
self.G = [["S'", G[0][0]]] + list(G)
View gf.py
import sympy as sp
import itertools as it
from sympy.physics.quantum import TensorProduct
def companion_matrix(p):
"""Gives companion matrix for polynomial p."""
p = sp.Poly(p)
assert p.is_monic
You can’t perform that action at this time.