Class: DigitsSolver::Solution
- Inherits:
-
Object
- Object
- DigitsSolver::Solution
- Includes:
- DigitsSolver::Strategies::Base
- Defined in:
- lib/digits_solver/solution.rb
Overview
This is the class that represents a solution to the problem.
Constant Summary
Constants included from DigitsSolver::Strategies::Base
DigitsSolver::Strategies::Base::OPERATIONS
Instance Attribute Summary collapse
-
#operands ⇒ Object
readonly
Returns the value of attribute operands.
-
#operations_to_apply ⇒ Object
readonly
Returns the value of attribute operations_to_apply.
-
#problem_statement ⇒ Object
readonly
Returns the value of attribute problem_statement.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(problem_statement, operands, operations_to_apply) ⇒ Solution
constructor
A new instance of Solution.
- #pretty_print(pp) ⇒ Object
- #to_evaluable_code ⇒ Object
- #to_operation_lines ⇒ Object
- #to_s ⇒ Object
Methods included from DigitsSolver::Strategies::Base
Constructor Details
#initialize(problem_statement, operands, operations_to_apply) ⇒ Solution
Returns a new instance of Solution.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/digits_solver/solution.rb', line 11 def initialize(problem_statement, operands, operations_to_apply) raise DigitsSolver::Error, 'Invalid problem statement' unless problem_statement.is_a? DigitsSolver::ProblemStatement unless operands.size == operations_to_apply.size + 1 raise DigitsSolver::Error, "Invalid solution #{operands.inspect} => #{operations_to_apply.inspect}" end @problem_statement = problem_statement @operands = operands @operations_to_apply = operations_to_apply end |
Instance Attribute Details
#operands ⇒ Object (readonly)
Returns the value of attribute operands.
9 10 11 |
# File 'lib/digits_solver/solution.rb', line 9 def operands @operands end |
#operations_to_apply ⇒ Object (readonly)
Returns the value of attribute operations_to_apply.
9 10 11 |
# File 'lib/digits_solver/solution.rb', line 9 def operations_to_apply @operations_to_apply end |
#problem_statement ⇒ Object (readonly)
Returns the value of attribute problem_statement.
9 10 11 |
# File 'lib/digits_solver/solution.rb', line 9 def problem_statement @problem_statement end |
Instance Method Details
#==(other) ⇒ Object
22 23 24 |
# File 'lib/digits_solver/solution.rb', line 22 def ==(other) (operations_to_apply == other.operations_to_apply) && (operands == other.operands) end |
#pretty_print(pp) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/digits_solver/solution.rb', line 46 def pretty_print(pp) pp.object_address_group(self) do pp.breakable pp.text "Solution: '#{to_evaluable_code} = #{problem_statement.target_number}'" pp.text ',' pp.breakable pp.seplist(instance_variables) do |v| pp.text "#{v}=" pp.pp instance_variable_get v end end end |
#to_evaluable_code ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/digits_solver/solution.rb', line 59 def to_evaluable_code # parenthesis management is ... meh, but better than nothing, and at least mathematically correct. evaluable_code = operations_to_apply.each.with_index.reduce(operands.first) do |res, (operation, idx)| op1 = res op2 = operands[idx + 1] format_string = operation == :multiply ? '%s %s %u' : '(%s %s %u)' format(format_string, op1, DigitsSolver::Strategies::Base::OPERATIONS[operation], op2) end evaluable_code[-1] == ')' ? evaluable_code[1...-1] : evaluable_code end |
#to_operation_lines ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/digits_solver/solution.rb', line 32 def to_operation_lines res = [] operations_to_apply.each.with_index.reduce(operands.first) do |acc, (operation, idx)| computed_result = apply_operation_to_operands operation, acc, operands[idx + 1] res << format(' => %u %s %u = %u', acc, DigitsSolver::Strategies::Base::OPERATIONS[operation], operands[idx + 1], computed_result) computed_result end res end |
#to_s ⇒ Object
26 27 28 29 30 |
# File 'lib/digits_solver/solution.rb', line 26 def to_s res = ["Solved in #{operations_to_apply.size} operation#{operations_to_apply.size <= 1 ? "" : "s"}:"] res.concat to_operation_lines res.join "\n" end |