Created
February 3, 2015 21:22
-
-
Save neill/e1ec73e880770090e653 to your computer and use it in GitHub Desktop.
If 7 balls, determine which is lightest in the least amount of weighs.
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
ball1, ball2, ball3, ball4, ball5, ball6, ball7 = 2.7, 2.5, 2.7, 2.7, 2.7, 2.7, 2.7 | |
group1 = [ball1, ball2, ball3] | |
group2 = [ball4, ball5, ball6] | |
if group1.inject(:+) == group2.inject(:+) | |
puts "Ball 7 is the lightest." | |
elsif group1.inject(:+) < group2.inject(:+) | |
case group1[0] <=> group1[1] | |
when 0 | |
puts "Ball 3 is the lightest." | |
when 1 | |
puts "Ball 2 is the lightest." | |
when -1 | |
puts "Ball 1 is the lightest." | |
end | |
elsif group2.inject(:+) < group1.inject(:+) | |
case group2[0] <=> group2[1] | |
when 0 | |
puts "Ball 6 is the lightest." | |
when 1 | |
puts "Ball 5 is the lightest." | |
when -1 | |
puts "Ball 4 is the lightest." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment