Skip to content

Instantly share code, notes, and snippets.

@hdf
hdf / combine2len.py
Last active January 5, 2023 14:39
Crossword puzzle solving helpers.
# Usage:
# ---
# python combine2len.py [total_len [num_num_spaces ['words.txt' ['banned.txt']]]]
from itertools import combinations
from pprint import pprint
from sys import argv
def get_combinations(strings, num_strings, total_length):
result = []
@hdf
hdf / crc32_quine.cpp
Last active November 2, 2022 11:33 — forked from praetoriansentry/main.go
Program to find crc32 collision, generates output like: `Hello, my hash is: 1A745BD7! :)`
// To compile:
// nvcc -O3 -Xcompiler -openmp crc32_quine.cpp -o crc32_quine
// Inspired by: https://gist.github.com/praetoriansentry/03b6dc2e68e174ffd8168aef7d85f910
// (Can be timed with: http://www.pc-tools.net/win32/ptime/)
// (Was not able to make it faster than the insiration, golang is impressive.)
#include <stdio.h> // printf, sprintf
#include "Crc32.cpp" // From: https://github.com/stbrumme/crc32
@hdf
hdf / div_hist.py
Created June 8, 2021 18:52
Recursive divisibility histogram
import sys
import numpy as np
import matplotlib.pyplot as plt
## Recursive divisibility histogram
# How many times can we divide a number by an integer, and by how many integers?
# Divisors start from 2 and increase by 1 until we reach half of the number.
# Show 3D histogram.
n = int(sys.argv[1]) if len(sys.argv) > 1 and sys.argv[1].isdigit() else 180
@hdf
hdf / q.txt
Created February 27, 2021 13:11
Dilemma solutions.
Chicken and egg dilemma solution:
---
Description of the dilemma:
What came first, the chicken or the [chicken] egg? (Eggs in general certainly existed before chickens did.)
Statistical approach:
Almost all chickens lay eggs. But most eggs do not hatch to be chickens. (Most are not fertilized, or it is a rooster that hatches.) Thus it is far more likely for an egg to come from a chicken, (thus chicken coming first) than for a chicken to come from an egg.
Nomenclaturical approach:
The egg that the first chicken came out of was laid by a proto-chicken. Since it could not be known ahead of time, what would emerge from the egg, it is logical to say, that a proto-chicken lays proto-chicken eggs, even if it is a chicken that emerges from it. Here as well the chicken came first (as it emerged from a proto-chicken egg).
Meta approach:
@hdf
hdf / index.html
Last active February 5, 2021 23:17 — forked from hynekcer/maxsubstring.py
fast longest common substring - by suffix array
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redundancy checker</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--<script src="lrs.min.js"></script>-->
<style>
html {
font-family: 'Verdana', sans-serif;
@hdf
hdf / conway.py
Last active October 24, 2020 14:42
My solution(s) to John Conway's 10 Digit number puzzle: (https://www.quantamagazine.org/three-math-puzzles-inspired-by-john-horton-conway-20201015/)
import string
from sys import argv, stderr
from time import time
def spinner_f():
while True:
for c in '|/-\\':
yield c
spinner = spinner_f()
@hdf
hdf / bank.py
Created May 29, 2020 13:11
Little Python script to validate bank account numbers
import sys
n = sys.argv[1] if len(sys.argv) > 1 else '12001008-00238600-00100004'
n = n.split('-')
m = [9, 7, 3, 1] * 2
banks = {
'100': 'Magyar Államkincstár',
'101': 'Budapest Bank',
'103': 'MKB Bank',
'104': 'K&H Bank',
@hdf
hdf / replace.py
Last active July 22, 2020 16:45
Replace a marked block part of a file, with the contents of another file. Ex.: replace.py file.html data.txt //begin //end
import sys
orig = sys.argv[1] if len(sys.argv) > 1 else 'kocka.html'
data = sys.argv[2] if len(sys.argv) > 2 else 'L5_A-17_365000napig_bennmarado_3650napig.txt'
start = sys.argv[3] if len(sys.argv) > 3 else '// begin_data'
end = sys.argv[4] if len(sys.argv) > 4 else '// end_data'
no_backup = True if len(sys.argv) > 5 and sys.argv[5] == 'no_backup' else False
clean = True if data == 'clean' else False
with open(orig, 'r+', encoding='utf-8') as f:
@hdf
hdf / wavegen.py
Created April 1, 2020 22:25
Waveform generation example
import pyaudio
import numpy as np
from scipy import signal as sg
from scipy.io.wavfile import write
p = pyaudio.PyAudio()
volume = 1.0 # range [0.0, 1.0]
fs = 44100 # sampling rate, Hz, must be integer
duration = 1.0 # in seconds, may be float
@hdf
hdf / bellard.py
Last active June 4, 2023 13:03
Bellard's formula for calculating PI in Python
import sys
from decimal import Decimal, getcontext
k = int(sys.argv[1]) if len(sys.argv) > 1 else 70
def bellard(n):
getcontext().prec = n + 1
return Decimal(1.0/(2**6)) * sum([Decimal(-1)**k/(1024**k) * (Decimal(256)/(10*k+1) + Decimal(1)/(10*k+9) - Decimal(64)/(10*k+3) - Decimal(32)/(4*k+1) - Decimal(4)/(10*k+5) - Decimal(4)/(10*k+7) - Decimal(1)/(4*k+3)) for k in range(n)])
print(bellard(k))