Skip to content

Instantly share code, notes, and snippets.

View amakukha's full-sized avatar

Andriy Makukha amakukha

  • Toronto
View GitHub Profile
#!/usr/bin/env python3
import os, sys, hashlib
MD5 = True
def md5sum(fn):
hasher = hashlib.md5()
with open(fn, 'rb') as f:
hasher.update(f.read())
#!/usr/bin/env python3
'''
A script to recursively compare two directories (including file size and file hash changes)
Usage: python3 compare_dirs.py DIR1 DIR2
'''
import os, sys, hashlib, unicodedata
COMPARE_FILES = True # should file sizes be compared if their names are the same?
@amakukha
amakukha / circle_overlap.py
Last active August 18, 2018 08:30
Percentage area overlap between circles in Python.
#!/usr/bin/python
## Percentage area overlap between circles in Python.
## Inspired by PHP code by JP Hastings-Spital:
## https://gist.github.com/jphastings/316058
import math
def circle_overlap(centers_distance, radius1, radius2):
# Make sure the larger circle is left-most (for convenience)
@amakukha
amakukha / Play2Tones.ino
Last active May 2, 2024 16:18
Play duo melody with Arduino
/*
* Play duo melody with Arduino
* ============================
*
* Description:
* This sketch simultaneously plays two separate melodies on two (passive)
* speakers or buzzers. This creates effect of a "choir".
* For simplicity, it uses function play2Tones(), which only plays two notes
* of equal duration simultaneosly.
* The actual melody here is the Anthem of Ukraine.
@amakukha
amakukha / ring_buff.h
Created December 27, 2018 10:56
Circular buffer in C++
#ifndef _RING_BUFFER_
#define _RING_BUFFER_
// Source: https://embeddedartistry.com/blog/2017/4/6/circular-buffers-in-cc
#include <memory>
#include <mutex>
template <class T>
class RingBuffer {
@amakukha
amakukha / hash_djb2.py
Last active March 19, 2023 08:55 — forked from mengzhuo/hash_djb2.py
DJB2 Hash in Python
# Proper (fast) Python implementations of Dan Bernstein's DJB2 32-bit hashing function
#
# DJB2 has terrible avalanching performance, though.
# For example, it returns the same hash values for these strings: "xy", "yX", "z7".
# I recommend using Murmur3 hash. Or, at least, FNV-1a or SDBM hashes below.
import functools
djb2 = lambda x: functools.reduce(lambda x,c: 0xFFFFFFFF & (x*33 + c), x, 5381)
sdbm = lambda x: functools.reduce(lambda x,c: 0xFFFFFFFF & (x*65599 + c), x, 0)
fnv1a_32 = lambda x: functools.reduce(lambda x,c: 0xFFFFFFFF & ((x^c)*0x1000193), x, 0x811c9dc5)
@amakukha
amakukha / mostfreq.cpp
Last active August 7, 2022 18:01
Find k most frequent words in a file (fast implementation)
/*
* Solution of the Jon Bentley's k most frequent words problem using a prefix tree and
* a heap of size k. Worst case time complexity is O(N log k), where N is the total
* number of words.
*
* The problem is formulated in the Communications of the ACM 29,5 (May 1986), 364-369:
* "Given a text file and an integer k, you are to print the k
* most common words in the file (and the number of their
* occurrences) in decreasing frequency."
*
@amakukha
amakukha / giganovel.py
Last active November 3, 2023 23:19
Generates a huge artificial text
#!/usr/bin/env python3
'''
A script to generate text files that look like a novel in TXT form.
Words are completely made up, but vaguely resemble the Finnish language.
The resulting text uses ASCII encoding with only printable characters.
Distribution of words follows Zipf's law.
Standard parameters generate 1 GB text with 148391 distinct words.
@amakukha
amakukha / .config
Created February 20, 2020 21:46
Config for compiling toybox on MacOS
# .config file for compiling toybox on MacOS
# 1) make defconfig
# 2) replace the .config file
# 3) make
# Also: don't forget to install gsed (GNU sed)
# Adding CPUS=1 in scripts/make.sh might also be needed
#
# -----------------------------------------------
#
# Automatically generated make config: don't edit
@amakukha
amakukha / predicting_movie_reviews_with_bert_on_tf_hub.py
Created May 4, 2020 10:48
BERT (2018) script for movie comment sentiment analysis
#!/usr/bin/env python
# Copyright 2019 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0