Skip to content

Instantly share code, notes, and snippets.

View cky's full-sized avatar

C. K. Young cky

View GitHub Profile
@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.

@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;
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 / 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)
@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.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 / gist:2984979
Created June 24, 2012 21:03
Modular exponentiation
(define (modexp a b q)
(define (modmul a b)
(modulo (* a b) q))
(do ((a a (modmul a a))
(b b (quotient b 2))
(r 1 (if (odd? b) (modmul r a) r)))
((zero? b) r)))
@cky
cky / GSString.java
Created August 19, 2012 05:32
Ruby string parser (minus interpolation) using Java enums
package nz.kiwi.chris.j7gs.types;
import com.google.common.base.Preconditions;
public class GSString implements Comparable<GSString> {
private final String value;
public GSString(String value) {
this.value = value;
}
@cky
cky / gist:3611062
Created September 3, 2012 17:22
Simple program for testing out MACs
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <boost/format.hpp>
#include <boost/iostreams/categories.hpp>
#include <boost/iostreams/copy.hpp>
@cky
cky / primes.rkt
Last active December 11, 2015 02:48
Sieve of Eratosthenes
#lang racket
(define (primes-less-than n)
(define size (sub1 (quotient n 2)))
(define table (build-vector size (lambda (i) (+ i i 3))))
(for ((x (in-vector table 0 (integer-sqrt size)))
#:when x
(y (in-range (quotient (- (* x x) 3) 2) size x)))
(vector-set! table y #f))
(cons 2 (filter values (vector->list table))))