Skip to content

Instantly share code, notes, and snippets.

View ecmendenhall's full-sized avatar

Connor Mendenhall ecmendenhall

View GitHub Profile
@ecmendenhall
ecmendenhall / ntaps.ipynb
Last active December 15, 2015 08:19
The n-taps problem: how many beers should you try before switching to your favorite so far?
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ecmendenhall
ecmendenhall / py_oneliners.md
Created March 26, 2013 06:10
Handy Python one-liners I sometimes forget.

Serve files in the current directory over HTTP:

python -m SimpleHTTPServer 8000

Start a simple SMTP server that prints messages to stdout:

python -m smtpd -n -c DebuggingServer localhost:25
@ecmendenhall
ecmendenhall / TerminalViewTest.java
Last active December 17, 2015 00:39
Testing printed terminal output with JUnit 4.
@RunWith(JUnit4.class)
public class TerminalViewTest extends TicTacToeTest {
private final PrintStream stdout = System.out;
private final ByteArrayOutputStream output = new ByteArrayOutputStream();
private TerminalView terminalview;
@Before
public void setUp() throws UnsupportedEncodingException {
terminalview = new TerminalView();
@ecmendenhall
ecmendenhall / key-listener.clj
Created May 9, 2013 02:50
A keypress listener using Jline and Clojure futures.
(ns keylistener.core
(:require [clojure.java.io :as io])
(:import [jline.console ConsoleReader])
(:gen-class))
(def console (ConsoleReader.))
(defn keypress-loop []
(let [char (.readCharacter console)]
(if (not (nil? char))
@ecmendenhall
ecmendenhall / on-lisp-chapter-6.clj
Last active December 17, 2015 17:29
Network closures in Clojure, from Chapter 6 of "On Lisp."
;; Figure 6.2 – Representation and definition of nodes
(def nodes (atom {}))
(defn defnode [name contents & [yes no]]
(swap! nodes assoc name {:contents contents :yes yes :no no}))
;; Figure 6.3 – Sample network
(defn make-nodes [node-maker]
(node-maker :people "Is the person a man?" :male :female)
(node-maker :male "Is he living?" :live :dead)
@ecmendenhall
ecmendenhall / quiet-queries.clj
Created June 20, 2013 00:21
Quiet cascalog queries
(require '[cascalog.io :refer [with-log-level])
(defmacro ?<-- [& forms] `(with-log-level :fatal (?<- ~@forms)))
#lang racket/base
(require rackunit)
(define simple-q '((1 2 3) (6 5 4)))
(define empty-queue '(()()))
(check-equal? empty-queue '(() ())
"An empty queue is a list containing two empty lists.")

Breadth-first numbering with a pure functional queue

My solution to Chris Okasaki's functional pearl: given a tree, reproduce a tree with the same structure with nodes numbered in breadth-first order (using only immutable data structures, of course).

For more on streams and lazy lists, check out chapter 3 of SICP. For more on pure functional queues, see this other paper by Chris Okasaki.

I'm not completely happy with my solution. On the plus side, it generalizes to non-binary trees. But performing a breadth-first search to calculate node numbers and a depth-first map to apply them is inefficient. I tried to construct my solution from the

(describe "Converting hex to base64"
(it "parses hex values from two-character strings"
(should= 0x2f (parse-hex "2f")))
(it "splits hex strings to sequences of strings"
(should= ["00" "1a" "2b" "88"]
(split-hex-string "001a2b88")))
(it "converts hex strings into sequences of hex values"
(ns crypto-challenges.core
(:require [clojure.edn :refer [read-string]]
[clojure.string :refer [lower-case]]))
(def alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
(def base64-values (str alphabet
(lower-case alphabet)
"0123456789+/"))