Skip to content

Instantly share code, notes, and snippets.

@dobrokot
dobrokot / sRGB_color_mixing.py
Created January 24, 2016 16:07
test color mixing in sRGB
def sRGB_to_linear(c):
a = 0.055
if c <= 0.04045:
return c / 12.92
else:
return ((c + a) / (1+a)) ** 2.4
def linear_to_sRGB(c):
a = 0.055
if c <= 0.0031308:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import telegram
from time import sleep
try:
from urllib.error import URLError
except ImportError:
@dobrokot
dobrokot / haskell_like_lazy_lists.cpp
Last active November 4, 2015 12:34
haskell_like_lazy_lists.cpp
#include <memory>
#include <functional>
#include <iostream>
#include <assert.h>
struct ListNode {
private:
typedef std::function<std::shared_ptr<ListNode>()> NextGetterFunc;
NextGetterFunc next_getter;
std::shared_ptr<ListNode> cached_next;
@dobrokot
dobrokot / random_baskets.py
Created June 24, 2015 16:12
random basket number of elements variance
import random
def main():
basket = [0]*5
rand = random.randrange
for i in xrange(27777777):
basket[rand(0, 5)%5] += 1
for v in basket:
# http://slobin.livejournal.com/515819.html
import math
def interval(k, n):
# http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
# Wilson score interval
z = 1.96 #95% confidence
p = 1.0*k / n
#include <string.h>
#include <stdio.h>
typedef char *p;
volatile p src = NULL;
volatile p dst = NULL;
volatile size_t n = 0;
#define TEST(x) if (x) ; else { printf("error, %s\n", #x); }
int f(int x) {
if (x == 0)
return 0;
int sign = x < 0 ? -1 : 1;
return ((x&1) ? -x : +x) - sign;
}
@dobrokot
dobrokot / suffix_array_sort.py
Last active August 29, 2015 14:18
suffix array sort
def trivial_suffix_sort(txt):
n = len(txt)
txt2 = txt + txt
tmp = sorted((txt2[i:i+n], i) for i in xrange(n))
return [x[1] for x in tmp]
def suffix_sort(txt):
# рассматривает txt как кольцо, и сортирует строки txt[i:i + len(l)],
# возвращает список индексов sort_permutation[i], где начинается i-ая сортированая строка
# см. также эквивалентную функцию trivial_suffix_sort
#include <iostream>
#include <algorithm>
int f[100];
int mx[100]; // mx[x] = (-x modulo N)
int main() {
for (int i = 0; i < N; ++i) {
f[i] = i;
mx[i] = (N - i) % N;
A=10; B=20; bash -c "A=30; B=40; foo() { echo $A \$B; }; declare -f foo"
------------------------------------
Output:
foo ()
{
echo 10 $B
}