Skip to content

Instantly share code, notes, and snippets.

@ryw
Forked from st23am/urinal_etiquette.rb
Created February 21, 2012 23:19
Show Gist options
  • Save ryw/1879743 to your computer and use it in GitHub Desktop.
Save ryw/1879743 to your computer and use it in GitHub Desktop.
Test Problem
class Bathroom
# door is on the right
def initialize(params)
@stalls = params[:stalls]
@stall_ratings = rate_stalls
@line = params[:line]
end
def find_stall
return first_empty_stall if line? || (safe_stalls? && rules_broken?)
return best_stall unless rules_broken?
return nil if rules_broken? && no_safe_stalls?
end
private
def line?
@line
end
def rules_broken?
@stall_ratings.index(2)
end
def safe_stalls?
@stall_ratings.index(0)
end
def no_safe_stalls?
!@stall_ratings.index(0)
end
def first_empty_stall
@stalls.index(false) + 1
end
def rate_stalls
ratings = []
@stalls.each_with_index do |stall, index|
rating = 0
rating = 1 if would_be_next_to_a_guy?(index)
rating = 2 if would_be_next_to_two_guys?(index)
rating = 3 if stall
ratings << rating
end
return ratings
end
def would_be_next_to_a_guy?(index)
@stalls[index-1] || @stalls[index+1]
end
def would_be_next_to_two_guys?(index)
(@stalls[index-2] && @stalls[index-1]) || (@stalls[index+1] && @stalls[index+2])
end
def best_stall
@stall_ratings.index(@stall_ratings.min) + 1
end
end
require 'rspec'
require_relative 'bathroom'
describe Bathroom do
describe "#find_stall" do
describe "Rule #1: Farthest from the door" do
describe "when no line" do
it do
@bathroom = Bathroom.new(stalls: [false, false, false, false, false], line: false)
@bathroom.find_stall.should == 1
end
end
describe "when there is a line" do
it do
@bathroom = Bathroom.new(stalls: [false, true, true, false, true], line: true)
@bathroom.find_stall.should == 1
end
end
describe "when guys are standing next to each other" do
it do
@bathroom = Bathroom.new(stalls: [false, true, true, false, false], line: false)
@bathroom.find_stall.should == 1
end
end
end
describe "Rule #2: Don't stand next to a dude" do
describe "when no line" do
it do
@bathroom = Bathroom.new(stalls: [false, true, false, false, false], line: false)
@bathroom.find_stall.should == 4
end
end
describe "when there is a line" do
it do
@bathroom = Bathroom.new(stalls: [false, true, false, false, false], line: true)
@bathroom.find_stall.should == 1
end
end
describe "when guys are standing next to each other" do
it do
@bathroom = Bathroom.new(stalls: [false, true, true, false, true, false], line: false)
@bathroom.find_stall.should == nil
end
end
end
describe "Rule #3: Really don't stand next to 2 dudes" do
describe "when no line" do
it do
@bathroom = Bathroom.new(stalls: [true, true, false, false, true], line: false)
@bathroom.find_stall.should == nil
end
end
describe "when there is a line" do
it do
@bathroom = Bathroom.new(stalls: [true, true, false, false, true], line: true)
@bathroom.find_stall.should == 3
end
end
describe "when guys are standing next to each other" do
it do
@bathroom = Bathroom.new(stalls: [true, true, false, false, true], line: false)
@bathroom.find_stall.should == nil
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment