Skip to content

Instantly share code, notes, and snippets.

View drbr's full-sized avatar

Andrew Brandon drbr

View GitHub Profile
@drbr
drbr / sortArguments.c
Last active December 22, 2016 00:29
C program to sort items given as input arguments
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
/*
* Returns a string containing each argument separated by a newline.
*/
char* parse_args(int argc, char** argv) {
int char_count = 0;
for (int i = 1; i < argc; i++) {
@drbr
drbr / extract_pages.sh
Last active April 28, 2016 01:12
A shell script that uses pdftk to break a PDF file into smaller files, each with an equal number of pages
#!/bin/zsh
if (($# != 2)); then
echo
echo "This script breaks the given PDF file into a set of PDf files,"
echo "each with block_size pages."
echo
echo "Usage:"
echo " extract_pages.sh filename block_size"
echo
@drbr
drbr / infinite_product_to_pi.py
Last active August 9, 2016 06:47
Python script that computes successive iterations of an infinite product that converges to π
# Computes the value of pi by iteratively evaluating the infinite product:
# 4 * (8/9) * (24/25) * (48/49) * (80/81) * (120/121) * (168/169) * ...
# Each line of output shows the last denominator considered so far, and
# the value of the finite product at that point.
#
# As featured by Matt Parker: https://www.youtube.com/watch?v=8pj8_zjelDo
from decimal import *
def converge():
@drbr
drbr / repeat.zsh
Created October 15, 2016 01:53
When a command is specified as arguments, runs the command multiple times, printing out the number of the iteration, and waits for the user to press Enter between each run.
#!/bin/zsh
i=0
while { true }; do
echo $((++i)):
$@
read
done
@drbr
drbr / treeTraversal.js
Created December 20, 2016 06:46
A simple algorithm for traversing a binary search tree in-order without using recursion.
function traverseTreeInOrder(tree) {
// In the real world, we'd need a more robust solution of storing
// and looking up object IDs. But JavaScript doesn't make that easy.
const visitedNodes = {};
const stack = [ tree ];
while (stack.length > 0) {
let node = stack.pop();
if (!node) {
@drbr
drbr / shuffleList.c
Created November 29, 2017 07:15
Sometimes I write things in C so I remember how.
#include <stdio.h>
#define ARR_SIZE 9
void swap(int* a, int* b) {
if (a == b) {
return;
}
*a = *a ^ *b;
@drbr
drbr / isPrime.js
Created September 1, 2018 17:26
Given a positive integer given as a command line argument, determines if that number is prime.
#!/usr/bin/env node
const process = require('process');
const num = parseInt(process.argv[2], 10);
const CompositeRegex = /^(..+)\1+$/;
function isPrime(num) {
const str = new Array(num + 1).join(' ');
return !CompositeRegex.test(str);
@drbr
drbr / XStateViz.tsx
Created November 26, 2020 07:26
XState Visualizer (XState Inspector) as a React component with a standalone state machine
import { useEffect, useState } from 'react';
import { StateMachine } from 'xstate';
import { useMachine } from '@xstate/react';
import { inspect } from '@xstate/inspect';
let iFrameElement: HTMLIFrameElement | null = null;
function createIFrame() {
if (!iFrameElement) {
const iframe = document.createElement('iframe');
@drbr
drbr / KeyOn.ts
Last active December 16, 2020 01:39
React HOC to render a wrapped component with a given key
/**
* Higher Order Component that renders the wrapped component with the given key (derived from its
* props), which allows us to easily "reset" a component if certain props change.
* @param component
* @param getKey
*/
export function KeyOn<P>(component: React.ComponentType<P>, getKey: (props: P) => string) {
const displayName = `KeyOn(${component.displayName})`;
const hoc = (props: P) => {
const key = getKey(props);
@drbr
drbr / useAsyncFetch.ts
Last active December 19, 2020 03:35
Hook to fetch async data, a cheap knockoff of react-query
import * as React from 'react';
export type AsyncFetchState<T, E = unknown> = {
data: T | null;
loading: boolean;
error: E | undefined;
};
/**
* This hook performs an async operation and stores its result in component state. It protects