class Rypto::Solution
Solution
returned by {Rypto::Hand#solve}
Public Class Methods
new(target)
click to toggle source
@private
# File lib/rypto/solution.rb, line 7 def initialize(target) @target = target @solutions = [] end
Public Instance Methods
infix()
click to toggle source
Array of all solutions in infix notation. @return [Array<String>]
# File lib/rypto/solution.rb, line 22 def infix @solutions.map do |solution| stack = [] solution.each do |s| if s.is_a? Fixnum stack.push expr: s, precedence: 3 else b = stack.pop a = stack.pop p = {'-' => 1, '+' => 1, '*' => 2, '/' => 2}[s] a[:expr] = "(#{a[:expr]})" if a[:precedence] < p b[:expr] = "(#{b[:expr]})" if b[:precedence] <= p stack.push expr: '%s %s %s' % [a[:expr], s, b[:expr]], precedence: p end end '%s = %d' % [stack.pop[:expr], @target] end end
postfix()
click to toggle source
Array of all solutions in postfix notation. @return [Array<String>]
# File lib/rypto/solution.rb, line 14 def postfix @solutions.map do |solution| '%s = %d' % [solution.join(" "), @target] end end
push(solution)
click to toggle source
Add solution to list of possible solutions @private
# File lib/rypto/solution.rb, line 47 def push(solution) @solutions << solution end