Skip to content

Instantly share code, notes, and snippets.

@dan5
dan5 / gist:230557
Created November 10, 2009 02:23 — forked from makoto/gist:229861
require 'yaml'
# Retains as a constant
CONFIG = YAML.load_file('config.yml')
# or
# Retains as an instance variable.
class Config
def initialize(name)
@dan5
dan5 / fizzbuzz0.hs
Created February 5, 2011 10:42
Haskellでプログラムを書く勉強会#0の成果
main = putStrLn $ concat $ map addln $ map fizzbuzz [1..20]
addln str = concat [str, "\n"]
fizzbuzz a = if (a `mod` 3) == 0 then "fizz"
else if (a `mod` 5) == 0 then "buzz"
else show a
# 1 ---
def score(pins)
pins.inject(&:+)
end
pins = [1, 4, 2, 8, 5, 0, 10, 0, 0, 4, 5, 5, 2, 0, 6, 1, 10, 0, 10, 5, 5]
p score(pins)
# 2 ---
def strike?(pins, idx)
def strike?(pins, idx)
pins[idx * 2] == 10
end
def spare?(pins, idx)
pins[idx * 2] + pins[idx * 2 + 1] == 10
end
def score_strike_spare(pins)
score = 0
def score(pins)
frames = pins.join(',').scan(/(\d+),?(\d+)?/).map{|e| e.map{|i| i.to_i}}
ss = frames.map{|s1, s2| s1 == 10 ? :strike : (s1 + s2 == 10 ? :spare : nil)}
sum = frames.size > 10 ? frames.pop[0] : 0
frames.zip(ss.unshift(nil)).zip(ss.unshift(nil)).map{|e1, e2| [e1[0], [e1[1], e2]]}.each do |score, hist|
sum += score[0] if hist[0] == :strike and hist[1] == :strike
case hist[0]
when :strike
sum += score[0] + score[1]
when :spare
class UnoClient
def initialize(uno)
@uno = uno
@cards = []
end
def draw
@cards << @uno.draw
end