-
-
Save agilous/1879742 to your computer and use it in GitHub Desktop.
Test Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ptest | |
def initialize(stalls) | |
@stalls = Array.new(stalls).map { |s| s = false } | |
end | |
def stalls | |
@stalls | |
end | |
def empty? | |
is_occupied = false | |
@stalls.each do |s| | |
is_occupied = is_occupied || s | |
end | |
! is_occupied | |
end | |
def furthest_available | |
return (@stalls.size - 1) if empty? | |
(@stalls.size - 2).downto(0).each do |s| | |
next if @stalls[s] || @stalls[s + 1] | |
return s | |
end | |
end | |
def add_occupant | |
furthest = self.furthest_available | |
@stalls[furthest] = true | |
furthest | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require File.expand_path('.', 'urinal_etiquette') | |
describe Ptest do | |
before do | |
@size = 6 | |
@ptest = Ptest.new(@size) | |
end | |
context '#initialize' do | |
it "should be the right length" do | |
@ptest.stalls.size.should eq(@size) | |
end | |
it "should be 'empty'" do | |
@ptest.empty?.should eq(true) | |
end | |
end | |
context 'rule 1: Furthest from the door' do | |
it "should be furthest from the door" do | |
@ptest.furthest_available.should eq(@size-1) | |
end | |
context "adding one occupant" do | |
it "should place the occupant furtherest from the door" do | |
@ptest.add_occupant.should eq(@size - 1) | |
end | |
end | |
end | |
context "rule 2: Don't stand next to a dude" do | |
before do | |
@ptest.add_occupant | |
end | |
it "should properly place a new dude" do | |
@ptest.add_occupant.should eq(@size - 3) | |
end | |
context "with a full restroom" do | |
it "should return nil" do | |
@ptest.add_occupant | |
@ptest.add_occupant.should eq(nil) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment