From: James Koppel <darmaniiii@yahoo.com>
Date: April 6, 2007 9:10:12 PM CDT
To: submission@rubyquiz.com, submission@rubyquiz.com
Subject: Please Forward: Ruby Quiz SubmissionHere is my solution to RubyQuiz #119. It implements both the basic criteria and the extra credit.
require 'enumerator'
class InsertedOperatorExpression
attr_accessor :value, :expressiondef initialize(numbers, operators, operatorPlacement)
@numbers = numbers
@operators = .fill(nil, 0, numbers.length - 1)
operatorPlacement.each_index {|x| @operators[operatorPlacement] = operators}
@expression = @numbers.zip(@operators).flatten.join
@value = eval(@expression.gsub(/(\d)([+-\/*%^])/, '\1.0\2').gsub(/\^/,'**')) # first gsub needed to prevent integer division
enddef to_s
@expression + "=" + @value.to_s
enddef hash #Needed for uniq
@expression.hash
enddef eql?(other) #Needed for uniq
@expression == other.expression
end
enddef findExpression(nums, ops, target)
opPlacements = allPlacements(ops.length, (0...(nums.length - 1)).to_a)
allExpressions = opPlacements.map {|placement| InsertedOperatorExpression.new(nums,ops, placement)}.uniq
allExpressions.each do |expression|
puts "******************************" if expression.value == target
puts expression
puts "******************************" if expression.value == target
end
puts "%d possible equations tested" % allExpressions.length
enddef allPlacements(remaining, avail, already=)
return [already] if remaining == 0
avail.enum_for.inject() {|placements, spot|
placements + allPlacements( remaining - 1, avail - [spot], already + [spot])}
endputs "Enter number sequence"
nums = gets.chomp.split(//).map {|c| c.to_i}
puts "Enter allowable operators (+-*/%^)"
ops = gets.chomp.split(//).map {|c| c.to_sym}
puts "Enter target value"
target = gets.chomp.to_f
findExpression(nums, ops, target)TV dinner still cooling?
Check out "Tonight's Picks" on Yahoo! TV.
···
Begin forwarded message: