Skip to content

Instantly share code, notes, and snippets.

View CTimmerman's full-sized avatar

Cees Timmerman CTimmerman

View GitHub Profile
@CTimmerman
CTimmerman / rainbow.py
Last active March 27, 2024 02:43
Rainbow terminal console text and/or background
"""Rainbow print loop using ANSI escape sequences. Press Ctrl+C to stop.
By Cees Timmerman
2020-03-06 First version.
2024-03-27 Fixed on Windows 11."""
import time
CSI = "\x1b[" # Control Sequence Introducer.
# https://en.wikipedia.org/wiki/ANSI_escape_code#Fe_Escape_sequences
# On Windows 11, C1 control code 155 "\x9b" only works the same in VS Code.
# https://github.com/microsoft/terminal/issues/10310
@CTimmerman
CTimmerman / radix_sort.py
Last active February 2, 2025 11:17
LSD radix sort in Python with speed- and doctest test.
"""Ported from https://www.java67.com/2018/03/how-to-implement-radix-sort-in-java.html
Sort an integer list in-place using least significant digit radix sort in Python.
Usage:
>>> x = [180, 50, 10, 30, 10, 29, 60, 0, 17, 24, 12]; radix_sort(x); x
[0, 10, 10, 12, 17, 24, 29, 30, 50, 60, 180]
Time Complexity of Solution:
Best Case O(k*n); Average Case O(k*n); Worst Case O(k*n),
where k is the length of the longest number and n is the
size of the input array.
@CTimmerman
CTimmerman / Flask_CRUD_demo.py
Last active May 9, 2022 10:00
Flask CRUD demo
# pylint: disable=broad-except, invalid-name, redefined-builtin
"""CRUD API demo in Python with Flask.
2019-08-29 https://blog.rapidapi.com/how-to-build-an-api-in-python/ simplified by Cees Timmerman.
2022-05-09 Polished.
"""
import random
from flask import Flask, request
@CTimmerman
CTimmerman / revert-a-commit.md
Created June 19, 2019 15:32 — forked from gunjanpatel/revert-a-commit.md
Git HowTo: revert a commit already pushed to a remote repository

Revert the full commit

Sometimes you may want to undo a whole commit with all changes. Instead of going through all the changes manually, you can simply tell git to revert a commit, which does not even have to be the last one. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit. Just like above, the bad commit remains there, but it no longer affects the the current master and any future commits on top of it.

git revert {commit_id}'

About History Rewriting

Delete the last commit

Deleting the last commit is the easiest case. Let's say we have a remote origin with branch master that currently points to commit dd61ab32. We want to remove the top commit. Translated to git terminology, we want to force the master branch of the origin remote repository to the parent of dd61ab32:

@CTimmerman
CTimmerman / benchmark_fib.sh
Created May 28, 2019 18:16 — forked from deeplook/benchmark_fib.sh
Benchmark using Fibonacci numbers with Python, Cython and PyPy.
#!/usr/bin/env bash
echo "Benchmark for Fibonacci numbers with Python3, Cython and PyPy"
echo '
def fib(n):
"Return the n-th Fibonacci number."
i = 0
a, b = 0, 1
if n < 2:
@CTimmerman
CTimmerman / bottle_hello.py
Created May 28, 2019 17:51 — forked from drgarcia1986/bottle_hello.py
Python HelloWorld (WebFrameworks) Collection
# -*- coding: utf-8 -*-
from bottle import route, run
@route('/')
def index():
return '<h1>Hello World/h1>'
run(host='localhost', port=8000)
@CTimmerman
CTimmerman / benchmark_iteration.js
Created May 14, 2019 13:40
Benchmark iteration in JS.
/* Calculate the sum of upVotes, https://hackernoon.com/javascript-performance-test-for-vs-for-each-vs-map-reduce-filter-find-32c1113f19d7
Node v10.15.3 on Win10:
reduce: 0.171ms
101
for loop: 0.005ms
101
for each: 0.018ms
101
map: 0.062ms
101
/*
HTML patcher by Cees Timmerman, 2019-05-13, 2019-05-17, 2019-06-21.
TODO: Use all HTML entities of https://brajeshwar.me/entities/
*/
"use strict"
const fs = require("fs")
const path = require("path")
const $ = require("cheerio")
@CTimmerman
CTimmerman / noise_formatter.py
Last active April 12, 2019 23:56
Generate code similar to that joke image of uniquely formatted Java.
"""Noise formatter, by Cees Timmerman.
Aligns code noise on the specified column.
2019-04-13 v1.0 inspired by https://www.facebook.com/groups/majordomo/permalink/10157128814034522/
2019-04-13 v1.0.1 fixed column padding.
"""
NOISE = ";{}"
noise_column = 55
noise = None; line_noise = ''
with open("Permutations.java") as f:
for line in f:
@CTimmerman
CTimmerman / python-es6-comparison.md
Created March 31, 2019 00:16 — forked from revolunet/python-es6-comparison.md
# Python VS ES6 syntax comparison

Python VS ES6 syntax comparison

Python syntax here : 2.7 - online REPL

Javascript ES6 via Babel transpilation - online REPL

Imports

import math