Skip to content

Instantly share code, notes, and snippets.

public class Main {
// runs in O(n) time
// exercise: Why might this algorithm run slower than we'd like for large n?
// Write an iterative solution that does not suffer from
// the same problem.
public static void computeTicksIterative(int[] ruler, int n) {
for (int tickHeight = 1; tickHeight <= n; tickHeight++) {
int rulerLength = 1<<tickHeight;
for (int i = (rulerLength)/2; i < ruler.length; i += rulerLength) {
(define-syntax or
(syntax-rules ()
((_) #f)
((_ a rest ...) (let ((tmp a)) (if tmp tmp (or rest ...))))))
(define-syntax or
(syntax-rules ()
((_) #f)
((_ a rest ...) (let ((tmp a)) (if tmp tmp (_ rest ...))))))
@cammckinnon
cammckinnon / gist:3151430
Created July 20, 2012 15:40
'smart' pointer 'implementation'
#include <iostream>
#include <string>
using namespace std;
/* START AUTO POINTER IMPLEMENTATION */
class super_wrapped_object {
public:
virtual void refCountDown() = 0;
virtual void refCountUp() = 0;
#lang racket
; box functions
(define (box x)
(lambda (m)
(m x (lambda (new) (set! x new)))))
(define (unbox b)
(b (lambda (x set) x)))
@cammckinnon
cammckinnon / gist:3041169
Created July 3, 2012 17:23
alpha/beta MLE calculation for stat231 assignment
points = [(447, 13), (460, 21),
(481, 24), (498, 16),
(513, 24), (512, 20),
(526, 15), (559, 34),
(585, 33), (614, 33),
(645, 39), (675, 43),
(711, 50), (719, 47)]
def mean(i):
s = 0
@cammckinnon
cammckinnon / gist:2911416
Created June 11, 2012 17:22
implementation of the algorithm that proves a combo question
from itertools import *
from random import *
from collections import defaultdict
shuffled = range(1, 101)
shuffle(shuffled)
shuffled = shuffled[:10]
sums = defaultdict(list)
print "random list of numbers from 1 to 100:\n" + str(shuffled)
#include <iostream>
#include <vector>
std::vector<bool> v;
decltype(v[0]) first() {
return v[0];
}
int main() {
#include <iostream>
#include <vector>
std::vector<bool> v;
int main() {
v.push_back(true);
auto r = v[0];
#include <iostream>
#include <vector>
std::vector<bool> v;
bool& first() {
return v[0];
}
int main() {