Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial golf course with the first eight holes from lrug

replaced hole2 and hole3 with own problems
  • Loading branch information...
commit 70121dd83cbb6aaad5aaf39b0909725e17be70df 1 parent 779c949
Christopher de Bruin authored
View
26 .gitignore
@@ -0,0 +1,26 @@
+.## MAC OS
+.DS_Store
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+
+.bundle
+spec/*.report
+Gemfile.lock
+
+## RMV
+.rvmrc
View
6 Gemfile
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+group :development do
+ gem "rspec", "~> 1.3"
+ gem "rake"
+end
View
12 README
@@ -0,0 +1,12 @@
+= Ruby Golf HH
+
+== Requirements
+ * ruby 1.8.7
+ * RSpec 1.3
+
+== How to play
+Ruby Golf is a game whereby the player is required to solve a number of problems (holes) in the fewest characters possible. The course consists of eight holes that you must implement. The holes are static methods that need to written for the Golf class (lib/golf.rb). The requirements for these methods is specified by a set of RSpec examples for each hole (spec/golf_spec.rb). To run the specs, simply run:
+
+ rake
+
+The first time you run this, all tests will fail. The aim is to get all test to pass. Building will also report the number of characters you took to complete the course. The fewer characters you use, the better.
View
24 Rakefile
@@ -0,0 +1,24 @@
+require 'rubygems'
+require 'rake'
+require 'spec/rake/spectask'
+
+COUNT_WHITESPACE = false
+
+Spec::Rake::SpecTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+task :default => [:spec, :count]
+
+task :count do
+ open(File.dirname(__FILE__) + "/lib/golf.rb") do |file|
+ solution = file.read
+ solution.gsub!(/\s/,"") unless COUNT_WHITESPACE
+ total_characters = solution.length
+ puts "-----------------------------------------------"
+ puts "| Congratulations, you've completed the course."
+ puts "| Total characters: #{total_characters} "
+ puts "-----------------------------------------------"
+ end
+end
View
3  lib/golf.rb
@@ -0,0 +1,3 @@
+class Golf
+
+end
View
124 spec/golf_spec.rb
@@ -0,0 +1,124 @@
+require File.dirname(__FILE__) + "/spec_helper"
+
+describe "Golf" do
+
+ describe ".hole1" do
+ it "should multiply the numbers in an array" do
+ Golf.hole1([1,2,3,4]).should eql 24
+ end
+ it "should be 600 for [5,2,10,6]" do
+ Golf.hole1([5,2,10,6]).should eql 600
+ end
+ it "should be 50 for [2,5,5]" do
+ Golf.hole1([2,5,5]).should eql 50
+ end
+ end
+
+ describe ".hole2" do
+ it "should find all numeric palindromes for a range from 0 to 20" do
+ Golf.hole2([0, 20]).should == [0,1,2,3,4,5,6,7,8,9,11]
+ end
+ it "should find all numeric palindromes for a range from 100 to 200" do
+ Golf.hole2([100, 200]).should == [101,111,121,131,141,151,161,171,181,191]
+ end
+ it "should find all numeric palindromes for a range from 1990 to 1992" do
+ Golf.hole2([1990, 1992]).should == [1991]
+ end
+ end
+
+ describe ".hole3" do
+ it "should recognize string palindromes" do
+ Golf.hole3("ABAB").should == false
+ Golf.hole3("ABBA").should == true
+ end
+ it "should recognize string palindromes with odd length" do
+ Golf.hole3("ABA").should == true
+ end
+ it "should recognize string palindromes ignoring case" do
+ Golf.hole3("AbBa").should == true
+ end
+ end
+
+
+ describe ".hole4" do
+ it "should put a hat on every man" do
+ Golf.hole4(["man(bob)", "man(geoff)", "man(harry)"]).should ==
+ ["hat(man(bob))", "hat(man(geoff))", "hat(man(harry))"]
+ end
+
+ it "should feed each dog a bone" do
+ Golf.hole4(["dog(luna)", "dog(rex)", "dog(spot)"]).should ==
+ ["dog(luna(bone))", "dog(rex(bone))", "dog(spot(bone))"]
+ end
+
+ it "should collapse the probability waveform of each cat" do
+ Golf.hole4(["cat(tiddles)", "cat(miffy)"]).should ==
+ ["dead(tiddles)", "dead(miffy)"]
+ end
+
+ it "should do all the above at once" do
+ Golf.hole4(["man(steve)", "dog(snoopy)", "cat(garfield)"]).should ==
+ ["hat(man(steve))", "dog(snoopy(bone))", "dead(garfield)"]
+ end
+ end
+
+
+ describe ".hole5" do
+ it "should return all sub-lists of the input, sorted by length then numerically" do
+ Golf.hole5([1,2,3,4]).should ==
+ [[1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4], [1, 2, 3, 4]]
+
+ Golf.hole5([4,10,15,23]).should ==
+ [[4], [10], [15], [23], [4, 10], [10, 15], [15, 23], [4, 10, 15], [10, 15, 23], [4, 10, 15, 23]]
+ end
+ end
+
+ describe ".hole6" do
+ it "should play fizzbuzz to 3, where multiples of 3 are 'fizz'" do
+ Golf.hole6(3).should eql [1,2,"fizz"]
+ end
+
+ it "should play fizzbuzz to 5, with rules above and multiples of 5 are 'buzz'" do
+ Golf.hole6(5).should eql [1,2,"fizz",4,"buzz"]
+ end
+
+ it "should play fizzbuzz to 10 with rules above and muliples of 3 and 5 are 'fizzbuzz'" do
+ Golf.hole6(15).should eql [1,2,"fizz",4,"buzz","fizz",7,8,"fizz","buzz",11,"fizz",13,14,"fizzbuzz"]
+ end
+
+ it "should play fizzbuzz to 30" do
+ Golf.hole6(30).should eql [1,2,"fizz",4,"buzz","fizz",7,8,"fizz","buzz",11,"fizz",13,14,"fizzbuzz",16,17,"fizz",19,"buzz","fizz",22,23,"fizz","buzz",26,"fizz",28,29,"fizzbuzz"]
+ end
+ end
+
+ describe ".hole7" do
+ it "should collapse consecutive numbers to a range" do
+ Golf.hole7([1,2,3]).should eql ["1-3"]
+ end
+
+ it "should keep separate ranges distinct" do
+ Golf.hole7([1,2,3,5,6,7,100,101]).should eql ["1-3","5-7","100-101"]
+ end
+
+ it "should show isolated digits on their own" do
+ Golf.hole7([1,2,3,5,10,11,12]).should eql ["1-3","5","10-12"]
+ end
+ end
+
+
+ describe ".hole8" do
+ it "should give the first N numbers of the fibonacci sequence" do
+ Golf.hole8(5).should eql [1,1,2,3,5]
+ end
+
+ it "should give the first 9 numbers" do
+ Golf.hole8(9).should eql [1,1,2,3,5,8,13,21,34]
+ end
+
+ it "should give the first 15 numbers" do
+ Golf.hole8(15).should eql [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
+ end
+ end
+
+end
+
View
6 spec/spec.opts
@@ -0,0 +1,6 @@
+--colour
+--format specdoc:spec/specdoc.report
+--format failing_examples:spec/failing_examples.report
+--format failing_example_groups:spec/failing_example_groups.report
+--format progress:spec/progress.report
+--format specdoc
View
11 spec/spec_helper.rb
@@ -0,0 +1,11 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+require 'golf'
+
+require 'spec'
+require 'spec/autorun'
+
+Spec::Runner.configure do |config|
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.