Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / sunrise.cpp
Last active April 12, 2020 22:02
Sunrise calculation
/* source: http://aa.quae.nl/en/reken/zonpositie.html */
// source: http://en.wikipedia.org/wiki/Julian_day
int julian_jdn(int year, int month, int day) { // convert Gregorian calendar date to Julian Day Number
int a = (14 - month) / 12;
int y = year + 4800 - a;
int m = month + 12 * a - 3;
return day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
}
@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 / fourier.js
Last active March 12, 2020 11:37
Discrete Fourier transformation
// Coding Challenge 130.3: Drawing with Fourier Transform and Epicycles
// Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/130.1-fourier-transform-drawing.html
// https://thecodingtrain.com/CodingChallenges/130.2-fourier-transform-drawing.html
// https://thecodingtrain.com/CodingChallenges/130.3-fourier-transform-drawing.html
// https://youtu.be/7_vKzcgpfvU
// https://editor.p5js.org/codingtrain/sketches/ldBlISrsQ
class Complex {
constructor(a, b) {
@hdf
hdf / expNand.txt
Last active December 6, 2019 15:30
Thought experiment. NAND logic gate (and others) with only exponentiation.
//NAND logic gate (and others) with only exponentiation.
/*
Exponentiation: 13
0^0 1
0^1 0
1^0 1
1^1 1
NAND: 7
@hdf
hdf / BigO.js
Created December 23, 2018 14:59
Based on the array in question, and the counter value (you have to put the code for that in the function in question yourself), this function will tell us the time complexity of the function.
function BigO(arr, arr3) { // Second array is optional
this.accumulator = 0; // Can be set here, or when calling ClosestMatch
/* function factorial(n, i) {
if (n > 171) // Overflow prevention
return Infinity;
if (typeof i == 'undefined')
i = 1;
if (n < i)
return 1;
if (n === i)
@hdf
hdf / NthClosest.js
Last active December 22, 2018 20:41
Interview stuff?
// Inspired by:
// https://interviewing.io/recordings/Go-Microsoft-1
function NthClosest(points, match, n) {
if (n < 1 || n > points.length || points[0].length != match.length)
return false;
function distance(p1, p2) { // Works in any dimensions.
let out = 0, d = 0, len = p1.length;
for (let i3 = 0; i3 < len; i3++) {
@hdf
hdf / bubbleSort.js
Last active December 22, 2018 17:18
function bubbleSort(arr, cmp) {
var len = arr.length, tmp, n;
if (typeof cmp !== 'function')
cmp = function(a, b) {
return a > b;
};
do {
n = 0;
for (var i = 1; i < len; i++) {
if (cmp(arr[i], arr[i-1])) continue;
@hdf
hdf / index.html
Last active November 29, 2018 16:56
Google Drive Demo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Google Drive Demo</title>
</head>
<body>
<div style="position: absolute; top: 40%; left: 50%; transform: translate(-50%, -40%); text-align: center;">
<a href="https://drive.google.com/open?id=1v1-ApluL9dCGx4znKVSXGKnBf4dFVGuv">Letöltés</a><br><br>