-
-
Save geofflane/1880313 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
# Rule 1: Farthest from the door | |
# Rule 2: Dont stand next to occupied | |
# Rule 3: Dont stand between 2 dudes | |
# Except: If line => Ignore 2 & 3 | |
# Except: If 2 or 3 is broken, ignore them | |
def which_stall(stalls, line) | |
if line || rules_broken?(stalls) | |
return furthest_empty(stalls) | |
end | |
find_by_rules(stalls) | |
end | |
def find_by_rules(stalls) | |
available = [] | |
stalls.each_with_index do |obj, i| | |
if not stalls[i] and not stalls[i + 1] | |
available << i | |
end | |
end | |
# print available, "\n" | |
# print available.count, "\n" | |
if 0 != available.count | |
return available.last | |
end | |
return -1 | |
end | |
def furthest_empty(stalls) | |
# index of the last element that's false | |
if stalls.any? { |obj| obj == false } | |
return stalls.rindex(false) | |
end | |
return -1 | |
end | |
def rules_broken?(stalls) | |
# validate rules 1 - 3 are true | |
# rule 1 | |
if not stalls.last and stalls[0..-1].any? { |obj| obj == true } | |
return true | |
end | |
# rule 2 and rule 3 | |
0.upto(stalls.length - 2) do |i| | |
if stalls[i] and stalls[i+1] | |
return true | |
end | |
end | |
return false | |
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
print 4, ' = ', which_stall([true, false, true, false, false], false), "\n" | |
print -1, ' = ', which_stall([true, false, true, false, true], false), "\n" | |
print -1, ' = ', which_stall([true, true, true, true, true], false), "\n" | |
print 3, ' = ', which_stall([false, false, true, false, true], true), "\n" | |
print 0, ' = ', which_stall([false, false, true, false, true], false), "\n" | |
print 2, ' = ', which_stall([false, false, false, false, true], false), "\n" | |
print 0, ' = ', which_stall([false, true, true, true], false), "\n" | |
print 0, ' = ', which_stall([false, true, false, true], false), "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment