Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created December 11, 2011 18:47
Show Gist options
  • Save JoshCheek/1462060 to your computer and use it in GitHub Desktop.
Save JoshCheek/1462060 to your computer and use it in GitHub Desktop.
Ruby Code Golf solutions
puts Solution.new('1. Fizz Buzz', <<SOLUTION)
def fizzbuzz(n)(n%15==0?'FizzBuzz':n%3==0?'Fizz':n%5==0?'Buzz':n).to_s end
SOLUTION
.test { fizzbuzz 3 }.expect { |result| result == "Fizz" }
.test { fizzbuzz 10 }.expect { |result| result == "Buzz" }
.test { fizzbuzz 45 }.expect { |result| result == "FizzBuzz" }
.test { fizzbuzz 31 }.expect { |result| result == "31" }
puts Solution.new('2. Caesar Cipher', <<SOLUTION, <<COMMENT)
def caeser(s,n)s.gsub(/./){|c|(c.ord+n).chr}end
SOLUTION
This one seems like it probably has lots of edge cases that should be specified,
but this satisfies the given example and works both forwards and backwards.
COMMENT
.test { caeser "hello", 3 }.expect { |result| result == "khoor" }
.test { caeser "khoor", -3 }.expect { |result| result == "hello" }
def seed_for(environment, result)
environment.define_singleton_method :rand do |*|
[:rock, :paper, :scissors].index result
end
end
puts Solution.new('3. Rock Paper Scissors Game', <<SOLUTION)
def play(s)o=%w(Rock Paper Scissors Rock);m=rand 3;"\#{o[m]},\#{o[m+1]==s ?:Win:o[m]==s ?:Draw: :Lose}"end
SOLUTION
.test {
seed_for self, :rock
play "Rock"
}.expect { |result| result == "Rock,Draw" }
.test {
seed_for self, :rock
play "Paper"
}.expect { |result| result == "Rock,Win" }
.test {
seed_for self, :rock
play "Scissors"
}.expect { |result| result == "Rock,Lose" }
.test {
seed_for self, :paper
play "Soap"
}.expect { |result| result == "Paper,Lose" }
puts Solution.new('4. String Counter', <<SOLUTION)
def count(h,n)h.upcase.scan(n.upcase).size end
SOLUTION
.test { count "Banana", "a" }.expect { |result| result == 3 }
.test {
count "RubySource provides advice, tutorials, commentary, and insight into the Ruby and Rails ecosystem", "ruby"
}.expect { |result| result == 2}
pairs = [["Homer","Marge"],["Micky","Minnie"],["Fred","Wilma"],["Peter","Lois"],["George","Judy"]]
men_are_in_same_order = lambda do |result|
pairs.map(&:first) == result.map(&:first)
end
no_woman_is_paired_with_the_man_she_came_with = lambda do |result|
pairs.each_with_index.all? do |(man, woman), index|
woman != result[index].last
end
end
puts Solution.new('1. Swingers Function', <<SOLUTION, <<COMMENT)
def swingers(s)f,l=s.transpose;f.zip l.rotate end
SOLUTION
The directions didnt specify randomness as a requirement,
this algorithm is simple and deterministic but easy to understand.
COMMENT
.test {
swingers pairs
}.expect { |result|
men_are_in_same_order[result] &&
no_woman_is_paired_with_the_man_she_came_with[result]
}

Solutions for Ruby Golf

$ ./_runner.sh 
1. Fizz Buzz: 74 characters
  def fizzbuzz(n)(n%15==0?'FizzBuzz':n%3==0?'Fizz':n%5==0?'Buzz':n).to_s end
  4 tests: Pass, Pass, Pass, Pass

2. Caesar Cipher: 47 characters
  def caeser(s,n)s.gsub(/./){|c|(c.ord+n).chr}end
  2 tests: Pass, Pass

  This one seems like it probably has lots of edge cases that should be specified,
  but this satisfies the given example and works both forwards and backwards.

3. Rock Paper Scissors Game: 104 characters
  def play(s)o=%w(Rock Paper Scissors Rock);m=rand 3;"#{o[m]},#{o[m+1]==s ?:Win:o[m]==s ?:Draw: :Lose}"end
  4 tests: Pass, Pass, Pass, Pass

4. String Counter: 46 characters
  def count(h,n)h.upcase.scan(n.upcase).size end
  2 tests: Pass, Pass

1. Swingers Function: 49 characters
  def swingers(s)f,l=s.transpose;f.zip l.rotate end
  1 tests: Pass

  The directions didnt specify randomness as a requirement,
  this algorithm is simple and deterministic but easy to understand.
cat {1,2,3,4,5}* | ruby -r./helper
class Solution
TestCase = Struct.new :test, :expectation do
def result(environment)
result = environment.instance_eval &test
expectation[result] ? :Pass : :Fail
end
end
attr_accessor :name, :solution, :comment
def initialize(name, solution, comment=nil)
self.name = name
self.solution = solution.strip
self.comment = comment
end
def test_cases
@test_cases ||= []
end
def test(&block)
test_case = TestCase.new
test_case.test = block
test_cases << test_case
self
end
def expect(&block)
test_cases.last.expectation = block
self
end
def to_s
"#{name}: #{size} characters\n" <<
" #{solution}\n" <<
" #{test_description}\n" <<
formatted_comment <<
"\n"
end
def formatted_comment
return '' unless comment
"\n #{comment.strip}\n"
end
def test_description
"#{test_cases.size} tests: #{test_results.join ', '}"
end
def test_results
test_cases.map { |test_case| test_case.result fresh_environment }
end
def fresh_environment
environment = Object.new
environment.singleton_class.class_eval solution
environment
end
def size
solution.size
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment