Skip to content

Instantly share code, notes, and snippets.

View linuskmr's full-sized avatar

Linus linuskmr

  • Germany
  • 20:15 (UTC +02:00)
View GitHub Profile
@linuskmr
linuskmr / code_window.html
Last active February 11, 2021 13:03
A window in the design of macOS showing some source code. Written in HTML and CSS
<html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Fira+Code&display=swap" rel="stylesheet">
<style>
pre {
font-family: 'Fira Code';
margin: 0;
}
#outer {
background-color: #abb8c3;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Must be a multiply of 8
const int TOTAL = 1000000;
int main() {
struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
@linuskmr
linuskmr / bin_decrement.s
Last active May 4, 2021 13:50
This program decrements the binary coded number on the tape and prints the contents of the tape (byte for byte)
# GNU Assembler, Intel syntax, x86_64 Linux
# This program decrements the binary coded number on the tape and prints the
# contents of the tape (byte for byte).
# Compile: clang -nostdlib -fno-integrated-as -Wa,-msyntax=intel,-mnaked-reg -s bin_decrement.s -o bin_decrement
# Run: ./bin_decrement | od -t d1
.data
@linuskmr
linuskmr / turing-machine-to-bits.js
Last active May 10, 2021 16:44
Converts transitions of a turing machine into a bitstream
transitions = [
// ['state', 'symbol', 'new_state', 'new_symbol', 'direction']
['q0', 'a', 'q1', 'b', 'r'],
['q1', 'b', 'q2', 'c', 'l']
]
console.log(
// Map each transition to its bits started by ##
transitions.map(transition => '##' + transition
// Map each parameter in the transition to its bits
@linuskmr
linuskmr / generic_lambda.cpp
Created June 30, 2021 15:40
This example shows the usage of generic lambdas in C++17.
// Compile: g++ -std=c++17 generic_lambda.cpp
#include <iostream>
using namespace std;
// This function prints a value with a generic type.
template<typename T>
void printFunction(T value) {
cout << value << endl;
@linuskmr
linuskmr / eta.py
Last active July 15, 2021 16:10
Simple ETA (estimated time of availability) calculation
import dataclasses
import json
import sys
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Any, Tuple, TypeVar, Optional
T = TypeVar('T')
@linuskmr
linuskmr / float_inaccuracy.cpp
Last active July 5, 2021 13:55
Here you can see how inaccuracies occur with floats. When adding a large float to a small one, you don't get the expected result. Therefore, always add floats with the same order of magnitude only.
#include <iostream>
using namespace std;
int main() {
// A big number
float big = 123456.0f;
// A medium number
float medium = 1234.0f;
// A small number
@linuskmr
linuskmr / assign_twice.cpp
Last active September 9, 2022 18:32
Funny/Strange C++ Code Examples
#include <iostream>
#include <string>
// Assume we have a class 'MyClass` and the following code:
//
// 1. MyClass a;
// 2. MyClass b = a;
// 3. b = a;
//
@linuskmr
linuskmr / .htaccess
Last active July 22, 2021 09:11
Reverse proxy in PHP for HTTP/HTTPS. This is especially useful for hobby projects where you have a simple HTTP server and it should/must be reachable via HTTPS.
# https://stackoverflow.com/questions/39712599/redirect-all-requests-to-single-page
# Redirect all requests to reverse_proxy.php and append the requested path.
# Note: You need to activate the RewriteEngine with `a2enmod rewrite`.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /reverse_proxy.php?path=$1 [NC,L,QSA]
@linuskmr
linuskmr / base64.py
Last active July 29, 2021 08:47
Implementation of base64 in Python. Prefer using base64 from the standard library.
import string
from itertools import zip_longest
from typing import TypeVar, Iterable, Optional, Iterator, List, Union, Any
T = TypeVar('T')
def grouper(iterable: Iterable[T], chunk_size: int, fill_value: Optional[T] = None) -> Iterator[List[T]]:
"""Collect data into fixed-length chunks or blocks.