Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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;