Skip to content

Instantly share code, notes, and snippets.

@iafisher
iafisher / Twenty Four.cpp
Last active August 29, 2015 14:19
Given a set of four numbers, finds the combination of arithmetic operations (if it exists) that creates 24. Works for arbitrary targets and set sizes (including fractions and negative numbers)
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "rational_number.h"
#define GIVEN_NUMBERS 4
#define DEFAULT_TARGET 24
using namespace std;
@iafisher
iafisher / multi.py
Created November 20, 2017 15:19
Simple multiprocessing example in Python
"""A simple example of multiprocessing for a CPU-bound task in Python.
Adapted from https://www.toptal.com/python/beginners-guide-to-concurrency-and-parallelism-in-python
"""
from multiprocessing.pool import Pool
import time
def fib(n):
"""A (contrived) example of a CPU-intensive function that you may want to parallelize."""
@iafisher
iafisher / myeval.py
Last active March 28, 2024 13:55
A simple implementation of a recursive-descent parser for a language of boolean expressions.
"""A simple implementation of a recursive-descent parser for a language of boolean expressions."""
import readline
def eval_str(expr):
"""Evaluate a boolean expression with the symbols '0', '1', '|', '&', '(' and ')'. All binary
operators must be wrapped in parentheses, even when it would be unambiguous to omit them.
"""
tokens = tokenize(expr)
ast = match_expr(tokens)
@iafisher
iafisher / xkcd1930.py
Created December 19, 2017 01:33
A script that prints random sentences from the template at https://xkcd.com/1930/
#!/usr/bin/env python3
"""Generate random strings from the template at xkcd.com/1930/."""
import random
import re
from collections import namedtuple
XKCD_STRING = '''\
Did you know that (the (fall|spring) equinox|the (summer|winter) solstice|the (Summer|Winter)
Olympics|the (earliest|latest) (sunrise|sunset)|Daylight (Saving|Savings) Time|leap (day|year)|
@iafisher
iafisher / byteecho.c
Created January 15, 2018 14:41
Read a string from stdin and convert it from UTF-8 to an encoding of your choice, and print the byte values
#include <errno.h>
#include <iconv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_bytes(const char* prefix, const char* str, size_t len);
@iafisher
iafisher / crypto.py
Last active April 5, 2018 17:07
Minimal working crypto example with Diffie-Helman
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives.padding import PKCS7
from cryptography.hazmat.primitives.ciphers import Cipher, modes, algorithms
import os
@iafisher
iafisher / testhelper_demo.py
Last active June 23, 2018 16:11
A demo of a testing utility for the Python shell that automatically generates unit tests based on your interactive session
>>> import mylib, testhelper as th
>>> fibonacci = th.register(mylib.fibonacci)
>>> fib(12)
144
[testhelper] Is this the expected result (y[es]/n[o]/c[ancel])? y
>>> fib(-1)
Traceback (most recent call last):
...
ValueError
[testhelper] Is this the expected result (y[es]/n[o]/c[ancel])? y
@iafisher
iafisher / Enclosing.java
Created June 26, 2018 16:21
Minimal example of instantiating and using objects of a static inner class
import lombok.Data;
public class Enclosing {
@Data
public static class Inner {
private String value;
}
public static void main(String[] args) {
Inner inner1 = new Inner();
@iafisher
iafisher / bookmarks.py
Last active March 9, 2019 22:40
DEPRECATED (see my bookmarks_from_sql.py gist): A small Python utility to parse bookmarks exports from Firefox
"""
DEPRECATED: see my bookmarks_from_sql.py gist.
A short script to parse bookmark exports from Firefox so they can be
manipulated with Python.
Author: Ian Fisher ([email protected])
Version: November 2018
"""
from collections import namedtuple
@iafisher
iafisher / pratt.py
Last active April 23, 2020 05:09
Small working example of a Pratt parser for infix expressions
"""Small but complete example of a Pratt recursive-descent parser for the following
grammar:
start := expr
expr := expr op expr | call | LPAREN expr RPAREN | MINUS expr | INT | SYMBOL
call := SYMBOL LPAREN arglist? RPAREN
op := PLUS | ASTERISK | MINUS | SLASH
arglist := (expr COMMA)* expr