Skip to content

Instantly share code, notes, and snippets.

@morganp
Created May 23, 2011 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save morganp/986917 to your computer and use it in GitHub Desktop.
Save morganp/986917 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] }*' '
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
x = []
a.size.times { |i| x += a.each_cons(i+1).to_a }
x
end
def hole6 i
f="fizz"
b="buzz"
(1..i).map { |s| s%15 < 1 ? f+b : s%3 < 1 ? f : s%5 < 1 ? b : s }
end
def hole8 i
a=[0,1]
i.times { a << a[-1]+a[-2] }
a[1..-2]
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 (c,d = g.minmax_by {|e,s| s.size})[0][1].size*2 < b.size
b.select {|e| e[0]==c[0]}.map &:shift
b -= [[]]
g = z b
end
d[0]
end
end
end
@morganp
Copy link
Author

morganp commented May 23, 2011

@morganp
Copy link
Author

morganp commented May 23, 2011

602
Adding some optimisations from https://gist.github.com/987222

Hole 2 .join ' ' => _' ' (4 char)
Hole 5 (1..a.size).each => a.size.times i has to change to i+1 (2 char)
Hole 9 x < y/2.0 => x_2 < y (2 char)

@morganp
Copy link
Author

morganp commented May 23, 2011

600
Hole 3 (1..i) => 1..i (2 chars)

@morganp
Copy link
Author

morganp commented May 23, 2011

566

From https://gist.github.com/987222

 while (c,d = g.minmax_by {|e,s| s.size})[0][1].size*2 < b.size
        b.select {|e| e[0]==c[0]}.map &:shift
        b.reject! &:empty?
        g = z b
      end

Hole 9: array -= [[]] is more efficient than array.reject! &:empty? (10 chars)

x<1 is 1 less than x==0 Used 3 times (3 chars)
parenthesis not required (6 chars)
( s%15 == 0 ? f+b : ( s%3 == 0 ? f : ( s%5 == 0 ? b : s ) ) ) => s%15 < 1 ? f+b : s%3 < 1 ? f : s%5 < 1 ? b : s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment