Skip to content

Instantly share code, notes, and snippets.

View cky's full-sized avatar

C. K. Young cky

View GitHub Profile
@cky
cky / Comm.java
Created April 29, 2012 14:48
Java comm(1) implementation
import java.util.List;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.PeekingIterator;
public class Comm {
public static class Result {
public final ImmutableList<Integer> only1;
public final ImmutableList<Integer> only2;
public final ImmutableList<Integer> both;
@cky
cky / Evil.java
Created March 24, 2012 21:34
Test your JVM's string interning behaviour under string mutation!
import java.lang.reflect.Field;
public class Evil {
private static final Field valueField;
static {
try {
valueField = String.class.getDeclaredField("value");
valueField.setAccessible(true);
} catch (NoSuchFieldException exc) {
@cky
cky / comm.rkt
Created March 2, 2012 00:47
Scheme comm(1) implementation
(require srfi/1)
(define (comm list1 list2 (lt? <))
(define (reverse-values . lists)
(apply values (map reverse lists)))
(let loop ((list1 list1) ;; Assumed to be sorted
(list2 list2) ;; Assumed to be sorted
(only1 '())
(only2 '())
(both '()))
(cond ((null? list1)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StudentId {
private static final Pattern REPR_PATTERN = Pattern.compile("S(\\d{1,4})");
private final int studentId;
public StudentId(String repr) {
Matcher m = REPR_PATTERN.matcher(repr);
if (!m.matches())
@cky
cky / Rpn7.java
Created March 15, 2011 19:41
RPN calculators in various languages
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.EmptyStackException;
import java.util.Map;
@cky
cky / gist:666001
Created November 7, 2010 07:19
CipherSaber code golf

Find the shortest way to write CipherSaber. There are several parts to this puzzle:

RC4/Arcfour

Arcfour is fully specified elsewhere, but for completeness, I'll describe it here.

Key setup

Set up two arrays, S and S2, both of length 256, where k_1 is the first byte of the key, and k_n is the last.