Skip to content

Instantly share code, notes, and snippets.

View brezniczky's full-sized avatar
💭
Setting my status

Janos Brezniczky brezniczky

💭
Setting my status
View GitHub Profile
@brezniczky
brezniczky / partial_demo.py
Last active January 3, 2020 15:17
Demonstrates that Python's partial() allows for 'overriding' the already frozen parameters
""" Partial's frozen arguments are not 'as frozen' as I would have thought
at first.
"""
from functools import partial
f = partial(int, base=2)
# results in 9 - great!
@brezniczky
brezniczky / lru_cache_demo.py
Created January 3, 2020 14:55
Demonstration of how the Python lru_cache perhaps isn't necessarily efficient by default
"""
Depending on the scenario, lru_cache() in functools may lead to inadvertent or
at least unexpected cache misses.
"""
from functools import lru_cache
eval_count = 0
@brezniczky
brezniczky / Reciprogra.txt
Last active June 4, 2018 23:39
Finding reciprocally connected nodes in a graph using map-reduce, psuedocode
def mapper(input):
for each edge node1 -> node2 in input:
emit (key: node1, node1 -> node2)
emit (key: node2, node2 <- node1)
def reducer(key: node, edges):
for each distinct edge node1 -> node2 in edges:
if node1 <= node2:
if node2 <- node1 in edges:
emit(node1, node2)
@brezniczky
brezniczky / permutations.R
Created June 28, 2016 21:46
How to generate all permutations of some numbers with a low heap allocation and without try-fail
get.perm = function(n, FUN = print) {
perm = n:1
switch_loop = function(L) {
# evaluate all pair exchanges on the given level (L)
if (L == 1)
FUN(perm)
else {
switch_pair = function(i1, i2) {
temp = perm[i1]
@brezniczky
brezniczky / test_is_ordered.R
Last active July 16, 2016 14:32
Test for the is_ordered.R gist.
# Verification of the is_ordered.R solution.
library(testthat)
test.generate.random.sequences = function() {
# generate some random sequence
#
# first: a random walk with a drift of unit increases or stagnation
n = 1000
root.sequence = c(1, 1 + cumsum(sample(c(0, 1), n, replace = TRUE)))
@brezniczky
brezniczky / is_it_ordered.R
Last active July 16, 2016 14:33
Checks if some criteria holds for a sequence of numbers, ensuring it may be sorted according to some ordering.
# This code exhibits a vectorized function telling whether a series of numbers
# is possibly sorted according to some (not necessarily increasing or
# decreasing) order.
#
# "Possibly" as in practice a random unsorted series may not contradict being
# sorted - e.g. 1, 3, 3, 2, 2 could be just a random discrete uniform sample.
#
# E.g. (1, 2, 5, 7, 7, 3, 3, 3, 8, 10, 10) is possibly sorted, but
# (1, 3, 2, 5, 2) cannot be, as 2 occurs in a disjoint way, separated by 5.
@brezniczky
brezniczky / first.R
Created May 16, 2016 03:01
Sinus plotter visualization (animGIF)
# install.packages("animation")
# A sinus plotter in R just to demo animations.
library(animation)
n.frames = 200
max.t = 60
max.i = 60
all.i = 1:max.i
@brezniczky
brezniczky / random_forest_visualization.R
Last active July 16, 2016 14:33
xgboost random forests shown converging in parallel
# Here I will try to visualize how an xgboost random forest approximates the
# training set in case of a binary classification as the number of weak
# classifiers in the ensemble increase, and/or as the maximum depth of trees
# change.
library(animation)
library(xgboost)
n.iters = 400
video.speed = 2 # factor, should be an integer
@brezniczky
brezniczky / blogger_a_more_efficient_filter_full.R
Last active July 16, 2016 14:35
Blogger code #4 (A more efficient filter, full code)
file.size.bytes = 1024 * 256
default.window.width = 256
# symbols in the range [0:(symbol.count - 1)] are expected
symbol.count = 256
set.seed(0)
test.data.bytes =
sample(x = 0:(symbol.count - 1), replace = TRUE, size = file.size.bytes)
@brezniczky
brezniczky / blogger_a_more_efficient_filter_2.R
Last active July 16, 2016 14:34
Blogger code #2 (filter, snippet #2)
quicker.count = function(data, window.width = default.window.width) {
# ...
# start and do the counting
for(i in 2:n.positions) {
# the value leaving the window is the first item in the
# previous window, i.e. data[i - 1]
old.data.idx = x[i - 1] + 1
frequency[old.data.idx] = frequency[old.data.idx] - 1
# the value entering the window is the last item in the
# current window