Skip to content

Commit

Permalink
AV hole
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmcdonough committed May 9, 2011
1 parent 4054926 commit e78d2d3
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 14 deletions.
25 changes: 22 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
**/*.sw*
*.sw*
lib/golf.rb
.## MAC OS
.DS_Store

## TEXTMATE
*.tmproj
tmtags

## EMACS
*~
\#*
.\#*

## VIM
*.swp

## PROJECT::GENERAL
coverage
rdoc
pkg

.bundle
spec/*.report
Gemfile.lock
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "http://rubygems.org"

group :development do
gem "rspec", "~> 1.3"
gem "rake"
end
13 changes: 13 additions & 0 deletions fixtures/av-example1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
blue, yellow, red
blue, yellow, green, red
blue
blue, green, yellow, red
blue, red
blue, green, yellow, red
blue, yellow
yellow, blue, green, red
red, green, yellow, blue
red, green
red, blue, yellow, green
red
red, green
9 changes: 9 additions & 0 deletions fixtures/av-example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
red, blue
red, blue
red, blue
red, blue
blue
blue
blue
blue
green, red
12 changes: 12 additions & 0 deletions fixtures/av-example3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
blue, yellow, red
blue, yellow, green, red
blue
blue, green, yellow, red
yellow, blue, red
green, yellow, blue, red
yellow, blue, green, red
red, green, yellow, blue
red, green
red, blue, yellow, green
red
red, green
9 changes: 9 additions & 0 deletions fixtures/av-example4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
red
red
red
red
blue
blue
blue
blue
green, yellow
40 changes: 29 additions & 11 deletions spec/golf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,68 @@

describe ".hole1" do
it "should multiply the numbers in an array" do
Golf.hole1([1,2,3,4]).should == 24
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 == 600
Golf.hole1([5,2,10,6]).should eql 600
end
end

describe ".hole2" do
it "should sort a sentence by the second letter of each word" do
Golf.hole2("the quick brown fox").should eql "the fox brown quick"
end

end


describe ".hole3" do
it "should calculate the factorial of 4, i.e. 4*3*2*1" do
Golf.hole3(4).should == 24
Golf.hole3(4).should eql 24
end
end


describe ".hole6" do
it "should play fizzbuzz to 3, where multiples of 3 are 'fizz'" do
Golf.hole6(3).should == [1,2,"fizz"]
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 == [1,2,"fizz",4,"buzz"]
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 == [1,2,"fizz",4,"buzz","fizz",7,8,"fizz","buzz",11,"fizz",13,14,"fizzbuzz"]
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 == [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"]
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 ".hole8" do
it "should give the first N numbers of the fibonacci sequence" do
Golf.hole8(5).should == [1,1,2,3,5]
Golf.hole8(5).should eql [1,1,2,3,5]
end
it "should give the first 9 numbers" do
Golf.hole8(9).should == [1,1,2,3,5,8,13,21,34]
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 == [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
Golf.hole8(15).should eql [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
end
end

describe ".hole9" do

it "should return the winner of an AV election where winner has a first round majority" do
Golf.hole9('fixtures/av-example1.txt').should eql "blue"
end
it "should eliminate the last candidate if there is no first round majority and allocate their second choices" do
Golf.hole9('fixtures/av-example2.txt').should eql "red"
end
it "should continue to eliminate the losing candidate until a majority is reached" do
Golf.hole9('fixtures/av-example3.txt').should eql "blue"
end
it "should be the candidate with most votes if there are only two left, even witout a majority" do
Golf.hole9('fixtures/av-example4.txt').should eql "blue"
end
end


Expand Down

0 comments on commit e78d2d3

Please sign in to comment.