Skip to content

Instantly share code, notes, and snippets.

@jColeChanged
jColeChanged / Sorting a million 32-bit integers in 2MB of RAM
Created May 20, 2014 17:24
This is my personal attempt at sorting a million 32-bit integers in 2MB of RAM.
from random import randint
from sys import maxint
import tempfile
import array
def write_a_million_ints():
f = file("ints.bin", "wb")
ints = array.array('i', [randint(-maxint + 1, maxint - 1) for i in range(1000000)])
ints.tofile(f)
@jColeChanged
jColeChanged / interview.py
Created May 21, 2014 20:35
Sometimes insertion sort if better than nlngn sorting methods
from operator import itemgetter
def inefficient_most_common(d, n):
"""
Returns the n most common words in a dictionary.
Args:
d: A dictionary of the frequencies or counts.
n: An integer representing the number of words to be returned.
@jColeChanged
jColeChanged / index.html
Last active August 29, 2015 14:25
Naruto Visualization
<html>
<head>
<script type="text/javascript"
src="http://d3js.org/d3.v3.min.js"
charset="utf-8">
</script>
<style type="text/css">
.two-col {
display: -ms-flex;
@jColeChanged
jColeChanged / someone-use-this.clj
Created December 16, 2010 04:36
I hope Clojure is blessed.
(defn find-fn
[inputs outputs]
(doseq [x (ns-publics (the-ns `clojure.core))]
(try
(if (= outputs
(binding [*out* java.io.StringWriter]
(apply
(if (-> (second x) meta :macro)
(macroexpand `(second x))
(second x))
@jColeChanged
jColeChanged / more-clojurey-find.clj
Created December 16, 2010 05:19
God let this be useful.
(defn find-fn
[in out]
(map first (filter
(fn [x]
(try
(= out
(binding [*out* java.io.StringWriter]
(apply
(if (-> (second x) meta :macro)
(macroexpand `(second x))
{:servers ["irc.freenode.net"] ; A list of servers.
:prepends #{"@"} ; The character you want for a prepend. Currently set to @
:bitly-login "" ; Your bit.ly login.
:bitly-key "" ; API key and login above needed for URL shortening.
:wordnik-key "" ; API key needed for dictionary access.
:max-operations 3 ; The maximum number of operations that can be running at any given time.
:pending-ops 0 ; The number of operations running right now
:admin-add? true ; only admins can add help topics
:admin-rm? true ; only admins can remove help topics
:eval-prefixes {:defaults ["->" "." "," ; prefixes in any channel
@jColeChanged
jColeChanged / gist:850531
Created March 2, 2011 05:33
Naive Implementation of rotate left and rotate right.
(defn rotatel
([n col]
(let [n (mod n (count col))]
(concat
(drop n col)
(take n col))))
([col]
(rotatel 1 col)))
(defn rotater
def bayes(a, b_given_a, b_given_not_a):
"""Calculates P(a|b) using bayes theorem.
Bayes theorem states that:
P(b|a) * P(a)
P(a|b) = --------------------------------
P(b|a) * P(a) + P(b|~a) * P(~a)
Intuitively this is saying that the probability of b given a is equivalent
@jColeChanged
jColeChanged / Merge Sort In Javascript
Created March 25, 2011 16:13
You asked for a sorting algorithm or a function which merged two arrays.
function merge(array_one, array_two)
{
var new_array_length = array_one.length + array_two.length;
var array_three = new Array(new_array_length);
var p_one = 0;
var p_two = 0;
for (var p_three = 0; p_three < new_array_length; p_three++)
{
if (array_one.length == p_one)
{
@jColeChanged
jColeChanged / gist:938272
Created April 23, 2011 04:16
A Nifty Clojure Function
(defn fn-and
"Takes a set of functions and returns a fn which returns whether
every item in the juxtaposition of those functions is true."
([& fns]
(let [juxted-fns (apply juxt fns)]
(fn [& args] (every? true? (apply juxted-fns args))))))