Skip to content

Instantly share code, notes, and snippets.

View tedpennings's full-sized avatar
🌱
Nurturing plants and code.

Ted Pennings tedpennings

🌱
Nurturing plants and code.
View GitHub Profile
@tedpennings
tedpennings / gist:1372162
Created November 17, 2011 02:06
Hash table (sort of) in Clojure
(ns algorithms.hashtable)
; This is a programming assignment from my CS algorithms class.
(defn create-table [size]
(let [empty-table (hash-map)]
(loop [current 0
table empty-table]
(if (> size current)
(recur (inc current)
@tedpennings
tedpennings / gist:1325051
Created October 29, 2011 20:33
Finding the first 10001 prime numbers
(def certainty 5)
(defn prime? [n]
(if (= n 1)
true
(.isProbablePrime (BigInteger/valueOf n) certainty)))
(take 10001
(filter prime?
(take-nth 2
@tedpennings
tedpennings / gist:1256545
Created October 1, 2011 19:41
"power of" in Clojure
(defn power-of? [power number]
"Returns true if a number is a power of another number, eg,
(power-of? 5 25) => true
(power-of? 5 26) => false"
(=
(mod
(/
(Math/log number)
(Math/log power) )
1 )
@tedpennings
tedpennings / fail.jspx
Created September 19, 2011 19:55
Whenever you have code that looks like this, you're doing it wrong. Your language has failed you.
<p>Modifying version ${dto.versionToExpire}. Currently, effective
${feature.featureVersions[dto.versionToExpire].effectiveRange.startDate} to
${feature.featureVersions[dto.versionToExpire].effectiveRange.endDate}</p>
@tedpennings
tedpennings / gist:1087981
Created July 17, 2011 19:44
Render Handlebars templates from a server-side resource with caching to session storage
/*
* This decorates Handlebars.js with the ability to load
* templates from an external source, with light caching.
*
* To render a template, pass a closure that will receive the
* template as a function parameter, eg,
* T.render('template-name', function(t) {
* $('#somediv').html( t() );
* });
*/
@tedpennings
tedpennings / greedy_example.rb
Last active August 29, 2015 14:24
greedy greedy algorithm
#!/usr/bin/env ruby
# The challenge:
# Given a dollar amount, determine which bills to dispense, optimizing for fewest bills, using a greedy algorithm
# Ted Pennings, 2015, License: http://choosealicense.com/licenses/unlicense/
AVAILABLE_BILLS = [100, 50, 20, 10, 5, 1] # nobody wants $2 bills
@tedpennings
tedpennings / gist:ad843590a01db440b382
Last active August 29, 2015 14:23
Untappd user's favorite beer styles
// 1. Go to a page like https://untappd.com/user/thesleepyvegan/beers?filter_type=type&filter_id=all&sort=
// 2. Open the Javascript console (Mac Chrome: Apple+Option+J)
// 3. Run the following Javascript snippet:
$('#style_picker option').map(function(i,e) { return $(e).text() }).splice(1).map(function(s) { var item = /([A-z/\(\) ]+) \((\d+)\)/.exec(s); return [item[1], parseInt(item[2])]; }).sort(function(a,b) { return b[1] - a[1]; });
// 4. Explore the resulting array and enjoy!
// Here are comments on what it does. Some browsers may require you to remove the comments for chaining
$('#style_picker option') // select the beer text from the style picker dropdown
.map(function(i,elem) { return $(elem).text() }) // take the text description of each
@tedpennings
tedpennings / gist:a1f670e1e618f3e7ce2f
Created October 17, 2014 06:35
My ~/.profile, minus work-related aliases
export PATH=/usr/local/bin:$PATH:~/bin
eval "$(rbenv init -)"
export GREP_OPTIONS="--color=auto"
BREWDIR=$(brew --prefix)
if [ -f $BREWDIR/etc/bash_completion ]; then
source $BREWDIR/etc/bash_completion
fi
@tedpennings
tedpennings / missing_tests.rb
Last active August 29, 2015 13:56
Find Homebrew Formulas that are missing tests!
#! /usr/bin/env ruby
# Find Homebrew formulas that are missing tests!
# Usage: run from checked out Homebrew repository root
# $ git clone git@github.com:Homebrew/homebrew.git && cd homebrew
# $ curl https://gist.githubusercontent.com/tedpennings/9148810/raw/ > missing_tests.rb
# $ chmod +x missing_tests.rb
# $ ./missing_tests.rb
min_commits = 25