Skip to content

Instantly share code, notes, and snippets.

@dobrokot
dobrokot / filename_punctuation_strip.py
Created November 3, 2014 13:38
strip punctiation from file names
import os, re
fs = []
for x in os.listdir('.'):
if x.endswith('.mp4'):
fs.append((os.path.getsize(x), x))
fs2 = []
@dobrokot
dobrokot / glass_water_avva.py
Last active August 29, 2015 14:10
avva glass water problem
# -*- encoding: UTF-8 -*-
# http://avva.livejournal.com/2827068.html
# run as:
# ( echo шир. узк. ; python glass_water_avva.py | tac ) | column -t
# v1 - широкий стакан
# v2 - узкий стакан
from fractions import Fraction
@dobrokot
dobrokot / download-build-clang.sh
Last active August 29, 2015 14:10
download and build clang
set -eu
#скачиваю и распаковываю архивы из интернета
curl -sS http://llvm.org/releases/3.5.0/llvm-3.5.0.src.tar.xz | tar -xJf - &
curl -sS http://llvm.org/releases/3.5.0/cfe-3.5.0.src.tar.xz | tar -xJf - &
curl -sS http://llvm.org/releases/3.5.0/compiler-rt-3.5.0.src.tar.xz | tar -xJf - &
curl -sS http://llvm.org/releases/3.5.0/clang-tools-extra-3.5.0.src.tar.xz | tar -xJf - &
wait #жду, пока всё скачается
#mv в существующую папку создаёт лишний уровень иерархии, поэтому проверка на несуществование
@dobrokot
dobrokot / alloc-mem-test.cpp
Created December 8, 2014 22:33
allocate memory to test system behavior
#include <stdio.h>
#include <stdlib.h>
// malloc some number of gigabytes, to test impact on system
// g++ -O3 alloc-mem-test.cpp && ./a.out
size_t WIDTH = 75;
int progress_bar(size_t i, size_t size) {
return i * WIDTH / size;
}
@dobrokot
dobrokot / namedtuple_memory_test.py
Last active August 29, 2015 14:13
python tuple/named type memory test
from collections import namedtuple
import numpy
Point = namedtuple('Point', ['x', 'y'])
class Point2:
def __init__(self, x, y):
self.x = x
self.y = y
A=10; B=20; bash -c "A=30; B=40; foo() { echo $A \$B; }; declare -f foo"
------------------------------------
Output:
foo ()
{
echo 10 $B
}
#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;
@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
int f(int x) {
if (x == 0)
return 0;
int sign = x < 0 ? -1 : 1;
return ((x&1) ? -x : +x) - sign;
}
#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); }