Class: Rypto::Solution

Inherits:
Object
  • Object
show all
Defined in:
lib/rypto/solution.rb

Overview

Solution returned by Hand#solve

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Solution

Returns a new instance of Solution.



7
8
9
10
# File 'lib/rypto/solution.rb', line 7

def initialize(target)
  @target    = target
  @solutions = []
end

Instance Method Details

#infixArray<String>

Array of all solutions in infix notation.

Returns:

  • (Array<String>)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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 s
      else
        b = stack.pop
        a = stack.pop
        stack.push left: a, op: s, right: b
      end
    end

    '%s = %d' % [expr_tree_to_infix(stack.pop), @target]
  end
end

#postfixArray<String>

Array of all solutions in postfix notation.

Returns:

  • (Array<String>)


14
15
16
17
18
# File 'lib/rypto/solution.rb', line 14

def postfix
  @solutions.map do |solution|
    '%s = %d' % [solution.join(" "), @target]
  end
end

#push(solution) ⇒ Object

Add solution to list of possible solutions



42
43
44
# File 'lib/rypto/solution.rb', line 42

def push(solution)
  @solutions << solution
end