Skip to content

Instantly share code, notes, and snippets.

@sekrasoft
sekrasoft / README.md
Created December 29, 2023 14:42
Watch accuracy monitoring

Overview

This is a python script allowing a user to monitor and measure their watch accuracy.

The script analyzes a data file watch1.txt containing the next human readable data:

# some comment lines starting with '#'
--- YYYY-MM
=== space separated watch list
DDHH diff1[/corr1] diff2[/corr2] ...
@sekrasoft
sekrasoft / README.md
Last active August 4, 2022 15:14
My version for standupmaths/fiveletterworda

See https://youtu.be/_-AfhLQfb6w and standupmaths/fiveletterworda

The Algorithm

Every word having unique Latin letters could be represented as a mask, each letter being represented by its own bit. 26 bits is enough, I use int that tends to have 32 bits. E.g. cat is the set of letter 1, letter 3, and letter 20 that is 2^0 + 2^2 + 2^19 = 524293.

So if two words having masks M1 and M2 share letters, then M1 & M2 != 0. If they don't, M1 & M2 == 0.

First, we build a mapping from masks to corresponding word lists (e.g. swing and wings have the same mask). Then for each mask we build a list of masks not sharing bits with the mask (a word list not sharing any letter with the chosen word).

@sekrasoft
sekrasoft / ndarray.cpp
Created March 24, 2020 18:16
Multidimentional array wrapper
#include <iostream>
#include <cassert>
template <typename T, size_t dim>
class NDArray: protected NDArray<T, dim - 1> {
public:
template <typename T1, typename... D>
NDArray (T1* data, size_t size, D...sizes): NDArray<T, dim - 1>(data, sizes...), size(size) {}
NDArray<T, dim - 1> operator [] (size_t i) {
assert(i >= 0 && i < size);
@sekrasoft
sekrasoft / README.md
Created October 18, 2017 18:22
Декомпозиция кортежей без std::tie и C++17

Декомпозиция кортежей в декларативном стиле:

my_tuple t(1, 2.0, 'x'); // кортеж произвольного типа my_tuple
int a; float b; char c;  // переменные, в которые хотим распаковать
decomposed(t) = a, b, c; // присваивание (задом наперёд, т.к. C++ не даёт реализовать "=" снаружи класса,
                         //               самая удачная из возможных "прямая" нотация - std::tie(a, b, c) = ...)

Для работы требуется задать правила декомпозиции (в данном случае - класса my_tuple):

DECOMPOSER_RULE(my_tuple, 0, int, const my_tuple& t, t.a)

#include <stdio.h>
#include <stdlib.h>
#define TO_STRING1(X) #X
#define TO_STRING(X) TO_STRING1(X)
#define LINE TO_STRING(__LINE__)
#define malloc(N) log_p(malloc(log_s(N, "CALLING MALLOC @ " \
LINE)), "CALLED MALLOC @ " LINE)
#define free(p) free(log_p(p, "FREE @ " LINE))
@sekrasoft
sekrasoft / church.h
Last active August 10, 2016 11:44
Compile-time Church numerals (C++11)
// Church numerals
namespace Church {
// zero = \f x -> x
template <template <typename> class f, typename x>
using Zero = x;
// succ = \n -> \f x -> f (n f x)
template <template <template <typename> class, typename> class n> struct Succ {
template <template <typename> class f, typename x>
@sekrasoft
sekrasoft / church.h
Last active August 10, 2016 11:44
Compile-time Church numerals (C++98)
// Church numerals
namespace Church {
// zero = \f x -> x
template <template <typename> class f, typename x> struct Zero {
typedef x value;
};
// succ = \n -> \f x -> f (n f x)
template <template <template <typename> class, typename> class n> struct Succ {
#include <stdio.h>
#include <stdlib.h>
void swap(void* a, void* b, size_t size) {
char tmp;
for(size_t i = 0; i < size; ++i) {
tmp = ((char*)a)[i];
((char*)a)[i] = ((char*)b)[i];
((char*)b)[i] = tmp;
#include <stdio.h>
#include <stdlib.h>
typedef double number;
typedef number* numbers;
typedef size_t index;
typedef number (*function)(number);
numbers map(function f, numbers xs, size_t size) {
numbers ys = malloc(size * sizeof(number));
#include <stdio.h>
#include <stdlib.h>
#define TEST_RAND_MAX 0xff
#define MAX 129
#define CYCLES 10000
int test_rand() {
return rand() & 0xff; // 0..255
}