Skip to content

Instantly share code, notes, and snippets.

View kachayev's full-sized avatar
🇺🇦
Fighting for freedom

Oleksii Kachaiev kachayev

🇺🇦
Fighting for freedom
View GitHub Profile
@kachayev
kachayev / channels.erl
Last active December 15, 2017 19:36
Process communication programming
-module(channels).
-compile(export_all).
make() ->
Ref = make_ref(),
Pid = spawn(?MODULE, channel, [Ref]),
{channel, Pid, Ref}.
channel(Ref) ->
receive
$ mkdir p1
$ cd p1/
$ rebar create-app appid=hotdo
$ ls
$ mkdir deps
$ touch rebar.config
$ # feeling rebar config file
$ wget -O rebar.config https://gist.github.com/kachayev/5657307/raw/fb9b0cd0d62e053efe54774bf2033e6e651c6622/hotdo.rebar.config
$ rebar get-deps
$ tree -L 2
@kachayev
kachayev / stream.rkt
Last active June 22, 2017 08:28
Stream implementation based on lambda / delay / force
#lang racket
(define stream
(letrec ((next
(lambda (n)
(cons n (delay (next (+ n 1)))))))
(next 0)))
(define head car)
(define (tail st) (force (cdr st)))
## More infromation about SkipList data structure one can find:
## on Wikipedia http://en.wikipedia.org/wiki/Skip_list
## on Stackoverflow http://stackoverflow.com/questions/256511/skip-list-vs-binary-tree
from math import log
from random import randint, random
class SkipNode(object):
__slots__ = "value", "next"
@kachayev
kachayev / closest.py
Last active December 18, 2015 18:29
Simple visualization for finding closest points algorithm (2D variant)
import Tkinter as tk
import time
from operator import itemgetter
from math import sqrt
from random import randint
from Queue import Queue
from threading import Thread
def vdist(q, (x1,y1), (x2,y2)):
@kachayev
kachayev / topological.py
Last active December 30, 2022 10:21
Topological sort with Python (using DFS and gray/black colors)
# Simple:
# a --> b
# --> c --> d
# --> d
graph1 = {
"a": ["b", "c", "d"],
"b": [],
"c": ["d"],
"d": []
}
@kachayev
kachayev / uf.py
Created July 9, 2013 10:36
Union-Find data structure implementation with examples (Kruskala MST, Tarjan LCA etc)
# Union-Find (Disjoint Set Union)
# more information on wiki:
# http://en.wikipedia.org/wiki/Disjoint-set_data_structure
class UF(object):
def __init__(self, size):
self.p = [None]*size
self.rank = [1]*size
@kachayev
kachayev / pairing_heap.erl
Created July 9, 2013 15:30
Pairing heap data structure implementation in Erlang
%% Pairing Heap implementation
%% more information on wiki:
%% http://en.wikipedia.org/wiki/Pairing_heap
%% pq :: {pq, heap(), int()}
%% heap :: nil | {Item, [heap()]}
%% =============================
%% API
%% =============================
@kachayev
kachayev / PairingHeap.py
Last active October 7, 2017 13:28
Pairing Heap implementation in Python
##
## Pairing heap data structure
## More information on wiki:
## http://en.wikipedia.org/wiki/Pairing_heap
##
## Data types
## Heap :: (Elem | None, Subs)
## Subs :: None | (Heap, Subs)
##
@kachayev
kachayev / dijkstra.py
Last active April 14, 2024 06:58
Dijkstra shortest path algorithm based on python heapq heap implementation
from collections import defaultdict
from heapq import *
def dijkstra(edges, f, t):
g = defaultdict(list)
for l,r,c in edges:
g[l].append((c,r))
q, seen, mins = [(0,f,())], set(), {f: 0}
while q: