Skip to content

Instantly share code, notes, and snippets.

@mguterl
Forked from st23am/urinal_etiquette.rb
Created February 21, 2012 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mguterl/1879741 to your computer and use it in GitHub Desktop.
Save mguterl/1879741 to your computer and use it in GitHub Desktop.
Test Problem
#!/usr/bin/env bash
# This is an RVM Project .rvmrc file, used to automatically load the ruby
# development environment upon cd'ing into the directory
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
# Only full ruby name is supported here, for short names use:
# echo "rvm use 1.9.3" > .rvmrc
environment_id="ruby-1.9.3-p0@cincirb_katas"
#
# Uncomment the following lines if you want to verify rvm version per project
#
# rvmrc_rvm_version="1.10.2" # 1.10.1 seams as a safe start
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
# return 1
# }
#
#
# Uncomment following line if you want options to be set only for given project.
#
# PROJECT_JRUBY_OPTS=( --1.9 )
#
# The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
#
# chmod +x ${rvm_path}/hooks/after_use_jruby_opts
#
#
# First we attempt to load the desired environment directly from the environment
# file. This is very fast and efficient compared to running through the entire
# CLI and selector. If you want feedback on which environment was used then
# insert the word 'use' after --create as this triggers verbose mode.
#
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
then
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
fi
else
# If the environment file has not yet been created, use the RVM CLI to select.
if ! rvm --create "$environment_id"
then
echo "Failed to create RVM environment '${environment_id}'."
return 1
fi
fi
#
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
# it be automatically loaded. Uncomment the following and adjust the filename if
# necessary.
#
# filename=".gems"
# if [[ -s "$filename" ]]
# then
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
# fi
# If you use bundler, this might be useful to you:
# if [[ -s Gemfile ]] && ! command -v bundle >/dev/null
# then
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
# gem install bundler
# fi
# if [[ -s Gemfile ]] && command -v bundle
# then
# bundle install
# fi
if [[ $- == *i* ]] # check for interactive shells
then
echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
else
echo "Using: $GEM_HOME" # don't use colors in interactive shells
fi
# Post your solution here
class UrinalEttiquette
def self.destination_stall(stalls, line)
unoccupied_stalls = []
stalls.each_with_index do |stall_occupied, index|
next_to_a_dude = stalls[index - 1]
if !stall_occupied
unoccupied_stalls << index
end
unless stall_occupied || next_to_a_dude
return index
end
end
if line || rules_broken?(stalls)
return unoccupied_stalls.min
else
return nil
end
raise "should not get here"
end
def self.rules_broken?(stalls)
rules_broken = false
stalls.each_with_index do |stall_occupied, index|
next_to_a_dude = stalls[index - 1]
next_to_another_dude = stalls[index + 1]
if index == 0
next_to_a_dude = false
end
if next_to_another_dude && next_to_a_dude
rules_broken = true
break
end
end
rules_broken
end
end
require_relative './urinal_etiquette'
require 'test/unit'
class UrinalEtiquetteTest < Test::Unit::TestCase
def test_destination_stall_all_empty
stalls = [false, false, false, false, false]
assert_equal 0, UrinalEttiquette.destination_stall(stalls, false)
end
def test_destination_stall_furthest_occupied
stalls = [true, false, false, false, false]
assert_equal 2, UrinalEttiquette.destination_stall(stalls, false)
end
def test_destination_stall_furthest_and_middle_occupied
stalls = [true, false, true, false, false]
assert_equal 4, UrinalEttiquette.destination_stall(stalls, false)
end
def test_destination_stall_furthest_and_middle_and_end_occupied_with_line
stalls = [true, false, true, false, true]
assert_equal 1, UrinalEttiquette.destination_stall(stalls, true)
end
def test_destination_stall_with_even_with_line
stalls = [true, false, true, false]
assert_equal 1, UrinalEttiquette.destination_stall(stalls, true)
end
def test_destination_stall_with_even_more_occupied
stalls = [true, true, true, false]
assert_equal 3, UrinalEttiquette.destination_stall(stalls, false)
end
def test_destination_stall_with_even_more_occupied_with_line
stalls = [true, true, true, false]
assert_equal 3, UrinalEttiquette.destination_stall(stalls, true)
end
def test_destination_stall_furthest_and_middle_and_end_occupied
stalls = [true, false, true, false, true]
assert_equal false, UrinalEttiquette.rules_broken?(stalls)
assert_equal nil, UrinalEttiquette.destination_stall(stalls, false)
end
def test_destination_stall_with_even
stalls = [true, false, true, false]
assert_equal nil, UrinalEttiquette.destination_stall(stalls, false)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment