Skip to content

Instantly share code, notes, and snippets.

@paulv
Forked from st23am/urinal_etiquette.rb
Created February 22, 2012 01:00
Show Gist options
  • Save paulv/1880312 to your computer and use it in GitHub Desktop.
Save paulv/1880312 to your computer and use it in GitHub Desktop.
Test Problem
# Paul Visscher and Josh Mills
require 'rspec/given'
class BathroomEtiquette
def initialize(stalls, line)
@stalls = stalls
@line_exists = line
@stalls_hash = make_stalls_hash(stalls)
end
def make_stalls_hash(stalls)
stalls_hash = {}
stalls.each_with_index do |stall, index|
stalls_hash[index] = stall
end
return stalls_hash
end
def can_i_pee?
unless @stalls.include? false
return false
end
unless @stalls.include? true
return @stalls.length - 1
end
unless is_rule_2_broken? or @line_exists == true
@stalls_hash.keys.each do |index|
if @stalls_hash[index] == true
return index - 2
end
end
else
@stalls_hash.keys.reverse_each do |index|
if @stalls_hash[index] == false
return index
end
end
return -1
end
end
def is_rule_2_broken?
@stalls_hash.keys.each do |index|
unless (@stalls_hash.length - 1) == index
if @stalls_hash[index] == true and @stalls_hash[index + 1] == true
return true
end
end
end
return false
end
end
describe BathroomEtiquette do
describe "bathroom etiquette" do
Then { BathroomEtiquette.new([false, false, false, false, false], false).can_i_pee?.should == 4 }
Then { BathroomEtiquette.new([false, false, false, false, true], false).can_i_pee?.should == 2 }
Then { BathroomEtiquette.new([false, false, true, false, true], false).can_i_pee?.should == 0 }
# there is a line
Then { BathroomEtiquette.new([true, false, true, false, true], true).can_i_pee?.should == 3 }
Then { BathroomEtiquette.new([true, false, true, true, true], true).can_i_pee?.should == 1 }
Then { BathroomEtiquette.new([true, true, true, true, true], true).can_i_pee?.should == false }
Then { BathroomEtiquette.new([false, false, true, true, false], false).can_i_pee?.should == 4 }
Then { BathroomEtiquette.new([true, true, false, false, false], false).can_i_pee?.should == 4 }
Then { BathroomEtiquette.new([false, false, false, true, true], false).can_i_pee?.should == 2 }
Then { BathroomEtiquette.new([false, false, true, true, true], false).can_i_pee?.should == 1 }
Then { BathroomEtiquette.new([true, false, false, true, true], false).can_i_pee?.should == 2 }
Then { BathroomEtiquette.new([true, false, false, true, true], false).can_i_pee?.should == 2 }
end
# describe "is rule 2 broken?" do
# Then { BathroomEtiquette.is_rule_2_broken?([false, false, false, false, false]).should == false }
# Then { BathroomEtiquette.is_rule_2_broken?([false, false, true, true, false]).should == true }
# Then { BathroomEtiquette.is_rule_2_broken?([false, true, true, false, false]).should == true }
# Then { BathroomEtiquette.is_rule_2_broken?([true, true, false, false, false]).should == true }
# Then { BathroomEtiquette.is_rule_2_broken?([false, false, false, true, true]).should == true }
# Then { BathroomEtiquette.is_rule_2_broken?([false, false, false, false, true]).should == false }
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment