#!/usr/bin/ruby class OtherFish def test_combo(officers) # test conditions return false if (officers.include?(0) and officers[0] != 4) return false if (officers.include?(1) and (!officers.include?(2) or (officers.index(1) < officers.index(2)))) return false if (officers.include?(1) and officers.include?(5)) return false if (officers.include?(2) and officers.include?(4) and officers.include?(5)) return false if (officers.include?(2) and (officers[0] == 5 or officers[2] == 1)) return false if (officers.include?(3) and ((officers.include?(2) ? (officers.index(2) < officers.index(3)) : false) or (officers.include?(4) ? (officers.index(4) < officers.index(3)) : false))) return false if (officers[1] == 4) return false if (officers[2] == 4 and officers.include?(3)) return false if (officers.include?(4) and officers.include?(0) and !officers.include?(5)) return false if (officers.include?(5) and (officers[0] != 5 or officers[0] != 2)) # now, check that no person is serving in two positions return false if ((officers[0] == officers[1]) or (officers[0] == officers[2]) or (officers[1] == officers[2])) # well if all is good, then return officers end def initialize puts "\"The Other Fish\" Problem Solving in Ruby\nby AJ Zmudosky\n" candidates = ["Mr. Grouper", "Dr. Trout", "Mr. Ray", "Mr. Mackerel", "The Snapper", "Mr. Bass"] attempts = 0 answers = 0 (0..5).each { |president| (0..5).each { |vice_president| (0..5).each { |secretary| attempts += 1 set = test_combo([president, vice_president, secretary]) if set then answers += 1 puts "True Combination #" + answers.to_s + " on Attempt #" + attempts.to_s + "\n" puts "\tPresident: " + candidates[set[0]] + "\n" puts "\tVice President: " + candidates[set[1]] + "\n" puts "\tSecretary: " + candidates[set[2]] + "\n" puts "##########\n" end } } } puts "Done Evaluating. #{attempts} attempts made. #{answers} successful combinations found.\n" end end OtherFish.new