Skip to content

Instantly share code, notes, and snippets.

View noahlz's full-sized avatar
🕵️‍♂️
Secret Stuff for SEI Novus. I might take 1 - 3 days to respond.

Noah Zucker noahlz

🕵️‍♂️
Secret Stuff for SEI Novus. I might take 1 - 3 days to respond.
View GitHub Profile
@noahlz
noahlz / fix-clj-dev-unit-tests.clj
Created November 14, 2011 00:21
Running fix-clj unit tests in the repl without using lein test - see http://nakkaya.com/2009/11/18/unit-testing-in-clojure/
; SLIME
user>
user> (load-file "c:/projects/github/fix-clj/src/fix_clj/core.clj")
#'fix-clj.core/next-value-and-rest
user> (load-file "c:/projects/github/fix-clj/test/fix_clj/test_core.clj")
nil
user> (use ['fix-clj.core 'fix-clj.test_core] :reload-all :verbose)
; Evaluation aborted.
user> (use '[fix-clj.core fix-clj.test_core] :reload-all :verbose)
; Evaluation aborted.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* Implementation of http://en.wikipedia.org/wiki/Quicksort#Simple_version.
*/
public class NotSoQuickSort {
@noahlz
noahlz / SimpleServer.java
Created December 1, 2011 05:36
A simple server with which you can interact over a raw socket.
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
/**
* A simple server with which you can interact over a raw socket.
*/
public class SimpleServer {
@noahlz
noahlz / AqsSample.java
Created December 2, 2011 14:43
Example code for AbstractQueuedSychronizer. Something I wrote a few years back while reading Java Concurrency in Practice.
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
public class AqsSample {
public static void main(String[] args) {
Restroom restroom = new Restroom();
Thread larry = new Thread(new Patron("Larry",restroom));
Thread moe = new Thread(new Patron("Moe", restroom));
@noahlz
noahlz / QuickSort.java
Created December 3, 2011 23:47
Java implementation of in-place Quick Sort. See http://en.wikipedia.org/wiki/Quick_sort#In-place_version
import java.util.Arrays;
import java.util.Random;
/**
* See http://en.wikipedia.org/wiki/Quick_sort#In-place_version
*/
public class QuickSort {
private static final Random random = new Random();
private static final int RANDOM_INT_RANGE = 100;
@noahlz
noahlz / reverse-recursively.clj
Created December 4, 2011 00:58
Recursive version of clojure reverse function
(defn reverse-recursively [coll]
(loop [r (rest coll)
acc (conj () (first coll))]
(if (= (count r) 0)
acc
(recur (rest r) (conj acc (first r))))))
@noahlz
noahlz / FindTwoSumToTarget.java
Created December 4, 2011 03:22
Given an array of integers, determine if any two given elements sum to a given target.
import java.util.Arrays;
import java.util.TreeMap;
import java.util.Map;
import java.util.Random;
/**
* Given an array of integers, determine if any two given elements sum to a given target.
*/
public class FindTwoSumToTarget {
@noahlz
noahlz / any-pair-make-sum.clj
Created December 4, 2011 04:47
(Clojure version) Given an array of integers, determine if any two given elements sum to a given target.
(defn any-pair-make-sum? [arr target]
(let [m (frequencies arr)]
(loop [f (first arr)
x (- target f)
r (rest arr)]
(let [entry (find m x)
only-needs-one-count (not (= x f))]
(if (and entry (or only-needs-one-count (> 1 (val entry))))
true
(if (empty? r)
@noahlz
noahlz / map-invert-preserve-dups.clj
Created December 10, 2011 02:11
Implementation of map-invert that preserves duplicates by cons entries into a sequence.
;; See: http://codereview.stackexchange.com/q/6682/9032
(defn map-invert-preserve-dups [m]
(apply merge-with into
(for [[k v] m]
{v [k]})))
(->> "http://www.weeklyscript.com/Pulp%20Fiction.txt"
(slurp)
(re-seq #"\w{5,}")
(frequencies)
@noahlz
noahlz / labrepl-looping.clj
Created December 23, 2011 23:02
LabRepl looping exercises
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; LabREPL min-3 exercise
;; "Implement min-3 which reduces over a function that returns
;; the min of two arguments"
;; My attempt
(defn min-3 [x & more]
(let [s (cons x (seq more))]
reduce (fn findmin [x y] (if (< x y) x y)) s)))