Skip to content

Instantly share code, notes, and snippets.

View gar's full-sized avatar

Gar Morley gar

View GitHub Profile
class ReportCard
def initialize(print_strategy)
@print_strategy = print_strategy.new
@grades = Hash.new
end
def add_grade(subject, grade)
@grades[subject] = grade
end
@gar
gar / antelopes.rb
Created October 6, 2010 17:18
Which antelope would score the most in Scrabble?
ANTELOPES = [:addax,:antelope,:antilopine,:ariel,:blackbuck,:blaubok,:bluebuck,:bloubok,:blesbok,:blesbuck,:bok,:bongo,:bontebok,:boschbok,:boshbok,:bosbok,:bubal,:bubale,:bubalis,:bubaline,:cabrie,:cabrit,:chamois,:chikara,:chinkara,:chiru,:dikdik,:duiker,:duyker,:duikerbok,:dzeren,:eland,:gazelle,:gemsbok,:gemsbuck,:gerenuk,:gnu,:goa,:gooral,:goral,:grysbok,:hartbees,:hartebeest,:hartbeest,:impala,:inyala,:nyala,:kaama,:klipspringer,:kob,:kongoni,:koodoo,:kudu,:lechwe,:madoqua,:marshbuck,:mhorr,:mohr,:nagor,:nilgai,:nilgau,:nilghai,:nilghau,:nylghai,:nylghau,:oribi,:ourebi,:oryx,:palebuck,:pallah,:prongbuck,:pronghorn,:puku,:pygarg,:reebok,:rhebok,:reedbuck,:reitbok,:saiga,:sasin,:sassaby,:tsessebe,:serow,:shamois,:sitatunga,:situtunga,:springbok,:springbuck,:steenbok,:steinbok,:steinbock,:stembok,:stembuck,:steenbuck,:thar,:topi,:tragelaph,:antelope,:antilopine,:ariel,:blackbuck,:blaubok,:bluebuck,:bloubok,:blesbok,:blesbuck,:bok,:bongo,:bontebok,:boschbok,:boshbok,:bosbok,:bubal,:bubale,:bubalis,:bubaline
; SICP Exercise 1.1
10 => 10
(+ 5 3 4) => 12
(- 9 1) => 8
(/ 6 2) => 3
(+ (* 2 4) (- 4 6)) => 6
(define a 3)
(define b (+ a 1))
(+ a b (* a b)) => 19
@gar
gar / gist:647564
Created October 26, 2010 19:05
exercise1.8.rkt
; SICP Exercise 1.8
#lang racket
(define (cube-root x)
(define (square y) (* y y))
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess)
(/ (+ (square (/ guess x)) (* 2 guess))) 3)
def add(m, n)
total = m
for i in 1..n do
total += 1
end
total
end
def exp(m, n)
total = 1
#include <stdio.h>
#include <string.h>
#define MAXLEN 256
void reverse(char *s, char *r);
main() {
char s[MAXLEN] = "hello reversed world!";
char r[MAXLEN];
@gar
gar / config.ru
Created January 22, 2012 10:17
Streaming example from Sinatra: Up and Running
# based on Heroku's documentation
require './streaming'
run Sinatra::Application
$ pwd
/Users/gar/code/chef-repo
$ knife client list
tangleofwire-validator
$ diff client-config/validation.pem /etc/chef/validation.pem
$ diff client-config/validation.pem .chef/tangleofwire-validator.pem
$ sudo chef-client
[2012-08-11T12:05:26+01:00] INFO: *** Chef 10.12.0 ***
[2012-08-11T12:05:28+01:00] INFO: Client key /etc/chef/client.pem is not present - registering
[2012-08-11T12:05:29+01:00] INFO: HTTP Request Returned 403 Forbidden: Merb::ControllerExceptions::Forbidden
13.591438 seconds on ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
6.887000 seconds on jruby 1.7.12 (1.9.3p392) 2014-04-15 643e292 on OpenJDK 64-Bit Server VM 1.7.0_55-b14 [linux-amd64]
24.593889 seconds on rubinius 2.2.6 (2.1.0 68d916a5 2014-03-10 JI) [x86_64-linux-gnu]
@gar
gar / luhn.clj
Last active August 29, 2015 14:13
(ns luhn)
(defn last-digit [n]
(mod n 10))
(defn drop-last-digit [n]
(long (/ n 10)))
(defn to-rev-digits
([n] (to-rev-digits n []))