Skip to content

Instantly share code, notes, and snippets.

View davepkennedy's full-sized avatar

Dave Kennedy davepkennedy

View GitHub Profile
@davepkennedy
davepkennedy / Brute Force Search
Created July 31, 2014 09:25
Variations on a 3-sum
@davepkennedy
davepkennedy / gist:b9d45ddbd6c5c669c23f
Created October 29, 2014 10:36
Functional FizzBuzz
def from (i: Int): Stream[Int] = i #:: from(i+1)
val nats = from(0)
val fb = nats map {
n =>
if (n % 15 == 0) "fizzbuzz"
else if (n % 3 == 0) "fizz"
else if (n % 5 == 0) "buzz"
else n.toString
}
fb.take(20) foreach println
@davepkennedy
davepkennedy / Shuffle in Scala
Created August 14, 2015 14:15
Shuffle some numbers using scala - possibly not the most efficient!
import scala.annotation.tailrec
import scala.util.Random
def shuffle (values: List[Int]): List[Int] = {
val r = new Random()
@tailrec
def shuffle0 (values: List[Int], accum: List[Int] = List.empty): List[Int] = values match {
case List(x) => x :: accum
case h :: t =>
@davepkennedy
davepkennedy / gist:f9a8be8a1053bd098519
Created August 14, 2015 16:33
Shuffling in C/C++
#include <iostream>
#include <cstdlib>
using namespace std;
// From http://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range
long random_at_most(long max) {
unsigned long
// max <= RAND_MAX < ULONG_MAX, so this is okay.
num_bins = (unsigned long) max + 1,
@davepkennedy
davepkennedy / gist:ab3c9d5739fbe2ee99e1
Created August 26, 2015 13:00
NSImage <-> CGContextRef
CGContextRef fromImage(NSImage* image)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSSize size = [image size];
CGContextRef contextRef = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
NSGraphicsContext *graphicsContext = [NSGraphicsContext graphicsContextWithCGContext:contextRef flipped:NO];
NSGraphicsContext* currentContext = [NSGraphicsContext currentContext];
[NSGraphicsContext setCurrentContext:graphicsContext];
[image drawInRect:NSMakeRect(0, 0, size.width, size.height)];
@davepkennedy
davepkennedy / sort_find.c
Created October 13, 2015 09:09
Sorting and searching in C. Slightly meh recursion in maxHeap…
#include <stdio.h>
#define LEFT(x) (2*(x)+1)
#define RIGHT(x) (2*(x)+2)
#define PARENT(x) (((x)-1)/2)
void swap (int* a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
@davepkennedy
davepkennedy / roughly.scala
Created October 14, 2015 10:40
Invoke some block roughly some proportion of the time
def sumProbabilities[U] (options: Seq[(Int, () => U)]): Int = {
options.map {case (p, f) => p}.sum
}
def roughly [U] (chance: Int) (f: => U) = chance -> (() => f)
def chancesOf [U] (options: (Int, () => U)*): () => Option[U] = {
type Chance = PartialFunction[Int,Option[U]]
val fallback: Chance = {
case x => None
}
@davepkennedy
davepkennedy / Explanation!
Last active October 26, 2015 15:56
Swap two numbers without an intermediate variable. Still takes as many statement as with one though…
Swap two numbers without an intermediate variable. Still takes as many statement as with one though…
Consider a = 5, b = 10
a = b0101
b = b1010
a = a ^ b -> b0101 ^ b1010 -> a = b1111 (15)
b = a ^ b -> b1111 ^ b1010 -> b = b0101 (5)
a = a ^ b -> b1111 ^ b0101 -> a = b1010 (10)
@davepkennedy
davepkennedy / max.py
Created September 19, 2016 15:10
Maximum of two numbers without a comparison
def max (a, b):
c = a-b
k = (c >> 31) & 1
return a - k * c
//
// GLView.h
//
// Created by Dave Kennedy on 11/09/2016.
//
//
#import <Cocoa/Cocoa.h>
#import <GLKit/GLKit.h>