Skip to content

Instantly share code, notes, and snippets.

@neill
Created February 3, 2015 21:22
Show Gist options
  • Save neill/e1ec73e880770090e653 to your computer and use it in GitHub Desktop.
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.
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