Skip to content

Instantly share code, notes, and snippets.

@jimwhite
jimwhite / midgard-php.ini
Created September 22, 2011 06:55
My Midgard PHP info
extension=midgard2.so
extension=gettext.so
extension=mbstring.so
extension=posix.so
extension=pcntl.so
extension=xdebug.so
display_errors=On
@jimwhite
jimwhite / gist:3607071
Created September 3, 2012 05:52
Groovy implementation (untested) for Cedric's SlidingWindowMap
import java.util.concurrent.PriorityBlockingQueue
import java.util.concurrent.ArrayBlockingQueue
public class SlidingWindowMap
{
def queue
def periodMs
public SlidingWindowMap(Set<String> keys, int maxCount, long periodMs)
{
@jimwhite
jimwhite / gist:3613264
Created September 3, 2012 20:40
Improved solution for Cedric's sliding window key problem.
// http://beust.com/weblog/2012/09/02/coding-challenge-a-sliding-window-map/
import java.util.concurrent.PriorityBlockingQueue
public class SlidingWindowMap
{
def queue
def windowWidthMillis
public SlidingWindowMap(Set<String> keys, int maxCount, long periodMs)
@jimwhite
jimwhite / School
Last active December 14, 2015 10:39
A tweak to the API gets the School challenge right.
import java.util.HashSet;
import java.util.Set;
public class School {
private final String name;
private final String nickname; // Nicks are not unique. How many Aggies are there?
public School(String name, String nickname) {
this.name = name;
this.nickname = nickname;
@jimwhite
jimwhite / MultiKeyObject.java
Last active December 14, 2015 17:38
A general solution to Cedric' School Challenge. Also the particular one that I did initially. http://beust.com/weblog/2013/02/13/coding-challenge-light-edition/
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
// Here's a generalized answer to Cedric's School Challenge.
// Object identity is the transitive closure of all objects where any key is equal.
@jimwhite
jimwhite / Treasure.clj
Last active December 16, 2015 22:29
A Groovy and a Clojure solution for Google Code Jam practice Treasure [https://code.google.com/codejam/contest/2270488/dashboard#s=p3].
;; My first bit of Clojure.
;; https://code.google.com/codejam/contest/2270488/dashboard#s=p3
(def test_chests '({:name 1 :opener 1 :contents ()} {:name 2 :opener 1 :contents (1 3)} {:name 3 :opener 2 :contents ()} {:name 4 :opener 3 :contents (2)}))
(defn remove-first [pred lst]
(if (pred (first lst)) (rest lst) (cons (first lst) (remove-first pred (rest lst)))))
;; Return sequence of chest names to open all the chests starting with the given list (multiset) of keys.
;; Return nil if there is no combination that opens all the chests.
class Something extends BuilderSupport
{
// Convenience build method.
// Alternatively you can construct the builder then use it:
// def something = new Something().something { ... }
static def build(Closure cl) {
cl.delegate = new Something()
cl.call()
}
@jimwhite
jimwhite / GroupAndSum.groovy
Last active December 20, 2015 04:58
Group and total a list of values and display it in a Swing table.
import groovy.swing.SwingBuilder
import javax.swing.JTable
import javax.swing.table.AbstractTableModel
import javax.swing.table.TableModel
class MyTableModel extends AbstractTableModel {
List<List> queryResult
int getRowCount() { queryResult.size() }
int getColumnCount() { queryResult[0].size() }
import org.junit.Test
import javax.script.Bindings
class Tests
{
@Test
def void testSP()
{
def p = new Foo()
@jimwhite
jimwhite / whats_new.groovy
Last active December 29, 2015 03:39
A script that reports what lines of the input file(s) are new since the last time the script was run. Uses Lucene to maintain an index of the lines. This code just indexes the entire line, but of course if there are key fields then they can be extracted using whatever method you like (including using a Lucene Analyzer).
#!/usr/bin/env groovy
@Grab(group='org.apache.lucene', module='lucene-analyzers-common', version='4.5.1')
import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.apache.lucene.document.Document
import org.apache.lucene.document.Field
import org.apache.lucene.index.DirectoryReader
import org.apache.lucene.index.IndexWriter
import org.apache.lucene.index.IndexWriterConfig