Skip to content

Instantly share code, notes, and snippets.

View lelandbatey's full-sized avatar

Leland Batey lelandbatey

View GitHub Profile
@lelandbatey
lelandbatey / index.html
Last active January 9, 2017 02:21
Draw a fractal tree using javascript on an HTML5 canvas.
<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<body style="margin: 0;">
<canvas id='canvas' width='4000' height='2000'></canvas>
</body>
<script>
// To run, download this file and serve it via some http server; I prefer
@lelandbatey
lelandbatey / tac.py
Created March 25, 2016 02:39
Tic tac toe bot beginnings
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
class player:
X = 'X'
O = 'O'
class Board(object):
@lelandbatey
lelandbatey / persistant_checklist.html
Last active October 30, 2016 23:27
Turn html lists into checklists; great for markdown
<script>
var checkboxes = [];
function getCheckboxStates() { return checkboxes.map(bx => !!bx.checked); };
function saveCheckboxStates() {
localStorage['boxStates'] = JSON.stringify(getCheckboxStates());
}
// Add a checkbox in front of every list item, save reference to each
[].slice.call(document.getElementsByTagName('li')).forEach((li, index) => {
var chkbx = document.createElement('input');
@lelandbatey
lelandbatey / jpg_recompresser.py
Last active August 31, 2015 00:28
Recompresses a jpeg many times over to destroy quality
#! /usr/bin/env python
from __future__ import print_function
import random
import sys
import os
# This code reads like a basketball (it doesn't). In my defense, I wrote this a
# long time ago, and I wrote it in one hazy evening out of rage just to spite
# people who re-compress jpegs so aggressively. I publish this only because
# there's some stuff I need to not forget (e.g. how to add grain to an image
@lelandbatey
lelandbatey / prime-sieve.cpp
Last active February 7, 2017 19:59
Sieve of Eratosthenes in C++ based on Python implementation written by David Eppstein.
#include <iostream>
#include <map>
#include <cmath>
#include <vector>
// Sieve of Eratosthenes
//
// This is a simple implementation of the Sieve of Eratosthenes, implemented in
// C++. This is a port of the excellent and very terse implementation by David
// Eppstein[0]. This port is meant to be a straightforward naive port of the
@lelandbatey
lelandbatey / remove_lf.ps1
Last active August 29, 2015 14:25
Remove errant line-feed characters in Windows csv
# To run this file, change the value below where it says ".\sample.csv" to the
# path to the CSV file you want to change. Then, to have this script write to
# a new csv file, pipe it into the Out-File cmdlet. For example, you might run
# this file (AFTER MODIFYING THE PATH TO THE INPUT FILE DOWN BELOW) with an
# invocation like this:
#
# .\remove_lf.ps1 | Out-File .\fixed_version.csv -encoding ASCII
#
# Note the option `-encoding ASCII` above. Leaving this option off might not
# cause any problems, but it may also break the output in some way. See what
@lelandbatey
lelandbatey / emscripten_problem.md
Last active August 29, 2015 14:18
Emscripten problem

Problem

When compiling this[0] C++ file with Emscripten, the compiled javascript file produces incorrect output when run with node (using node v0.12.0 on all platforms). This is not "serious" code, it is a toy minified raytracer, as found here[1]. I have tried to find any information on the Emscripten website regarding less-than-compatible behavior this source file might be doing which makes it run incorrectly, but I have had no luck.

In attempting to narrow the problem, I also constructed a small program which seems to break in a similar way, which can be found here[6].

I come here hoping that it is simply an oversight by me in how this code is written or how I'm compiling it.

Steps to reproduce:

@lelandbatey
lelandbatey / inplace_cd.sh
Last active October 15, 2021 23:59
In place cd; cd won't create a newline and prompt
# Original
# function cd { command cd $@ > /dev/null; echo -ne "\r\e[1A\e[J"; }
# Written by Kate Adams: https://github.com/KateAdams
function cd {
command cd $1 > /dev/null # pipe stdout, but not stderr
local ret=$?
if [[ $ret == 0 ]]; then
@lelandbatey
lelandbatey / ping_speaker.sh
Created January 14, 2015 20:49
Have your computer speak when a ping works, like in this classic story: http://ftp.arl.mil/mike/ping.html
#!/bin/bash
# Ping-speaker oneliner
# This shell oneliner will ping a host once twice every second, then call my
# custom text-to-speech program to speak that output. This is meant to act
# similarly to the classic story of a use for ping found here:
# http://ftp.arl.mil/mike/ping.html
# Be aware, I wrote this to work on Cygwin on my system specifically. It also
@lelandbatey
lelandbatey / context.py
Last active August 29, 2015 14:13
A minimal example of a problem with the Python static analyzer.
# This is the pseudocode for the actual code where I found this error
def big_function_to_handle_many_different_cases(varA, varB):
temporary_buffer = ""
def do_the_boilerplate():
SOME_GLOBAL_ARRAY.append(temporary_buffer)
temporary_buffer = "" # Reset the temporary_buffer
for thing in varA: