This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |