Skip to content

Instantly share code, notes, and snippets.

@morganp
Created May 23, 2011 15:40
Show Gist options
  • Save morganp/986902 to your computer and use it in GitHub Desktop.
Save morganp/986902 to your computer and use it in GitHub Desktop.
class Golf
class << self
def hole1 a
a.reduce :*
end
def hole2 s
s.split.sort_by{|i| i[1] }.join ' '
end
def hole3 i
hole1 (1..i)
end
def hole4 a
a.map { |i| i.
sub(/(man.*)/,'hat(\1)').
sub(/(dog.*)\)/,'\1(bone))').
sub('cat','dead')
}
end
def hole5 a
r = []
s = a.size
(1..s).each { |x|
(0..s-x).each { |y|
r << a[y,x]
}
}
r
end
def hole6 i
f="fizz"
b="buzz"
(1..i).map { |s| ( s%15 == 0 ? f+b : (s%3 == 0 ? f : (s%5 == 0 ? b : s) ) ) }
end
def hole8 i
a = [1,1]
(1..i-2).each { |i| a << a[i-1] + a[i] }
a
end
def z a
a.group_by {|e| e[0]}
end
def hole9 f
b = open(f).map {|e| e.chomp.split ', '}
g = z b
while (a = g.max_by {|e,s| s.size})[1].size < b.size/2.0
r = g.min_by {|e,s| s.size}[0]
b.select {|e| e[0]==r}.map &:shift
b.reject! &:empty?
g = z b
end
a[0]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment