Решение на Трета задача от Ростислав Градинаров

Обратно към всички решения

Към профила на Ростислав Градинаров

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 12 успешни тест(а)
  • 1 неуспешни тест(а)

Код

class Expr
attr_accessor :tree
def self.build(tree)
@tree = Tree.new(tree)
end
def evaluate(variables)
case @tree[0]
when :number then @tree[1]
when :variable then variables[@tree[1]]
when :- then -Expr.build(@tree[1])
when :+ then Expr.build(@tree[1]) + Expr.build(@tree[2])
when :* then Expr.build(@tree[1]) * Expr.build(@tree[2])
end
end
def ==(other)
@tree == other.tree
end
end
class Tree
attr_accessor :tree
def initialize(tree)
@tree = tree
end
def evaluate(variables)
case @tree[0]
when :number then @tree[1]
when :variable
raise 'VariableNotSetException' if variables[@tree[1]] == nil
variables[@tree[1]]
when :- then -Tree.new(@tree[1]).evaluate(variables)
when :+ then Tree.new(@tree[1]).evaluate(variables) + Tree.new(@tree[2]).evaluate(variables)
when :* then Tree.new(@tree[1]).evaluate(variables) * Tree.new(@tree[2]).evaluate(variables)
when :sin then Math.sin(Tree.new(@tree[1]).evaluate(variables))
when :cos then Math.cos(Tree.new(@tree[1]).evaluate(variables))
end
end
def ==(other)
@tree == other.tree
end
def simplify
simplify_array(@tree)
@tree = @tree[2] if @tree[0] == :+ && @tree[1][0] == :number && @tree[1][1] == 0
@tree = @tree[1] if @tree[0] == :+ && @tree[2][0] == :number && @tree[2][1] == 0
@tree = @tree[2] if @tree[0] == :* && @tree[1][0] == :number && @tree[1][1] == 1
@tree = @tree[1] if @tree[0] == :* && @tree[2][0] == :number && @tree[2][1] == 1
@tree = @tree[1] if @tree[0] == :* && @tree[1][0] == :number && @tree[1][1] == 0
@tree = @tree[2] if @tree[0] == :* && @tree[2][0] == :number && @tree[2][1] == 0
@tree = @tree[1] if @tree[0] == :sin && @tree[1][1] == 0
self
end
def simplify_array(array)
@tree.each_with_index do |node, index|
@tree[index] = node[2] if node[0] == :+ && node[1][0] == :number && node[1][1] == 0
@tree[index] = node[1] if node[0] == :+ && node[2][0] == :number && node[2][1] == 0
@tree[index] = node[2] if node[0] == :* && node[1][0] == :number && node[1][1] == 1
@tree[index] = node[1] if node[0] == :* && node[2][0] == :number && node[2][1] == 1
@tree[index] = node[1] if node[0] == :* && node[1][0] == :number && node[1][1] == 0
@tree[index] = node[2] if node[0] == :* && node[2][0] == :number && node[2][1] == 0
@tree[index] = node[1] if node[0] == :sin && node[1][1] == 0
simplify_array(node[1]) if node[1].class == Array
simplify_array(node[2]) if node[2].class == Array
end
end
def derive(argument)
end
end

Лог от изпълнението

...F.........

Failures:

  1) Expressions assignment can derive expressions
     Failure/Error: derive('x').should eq build('1')
       
       expected: #<Tree:0xa1c9530 @tree=[:number, 1]>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-jcg6j0/spec.rb:161:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.04636 seconds
13 examples, 1 failure

Failed examples:

rspec /tmp/d20130203-23049-jcg6j0/spec.rb:160 # Expressions assignment can derive expressions

История (1 версия и 0 коментара)

Ростислав обнови решението на 14.11.2012 01:40 (преди около 12 години)

+class Expr
+ attr_accessor :tree
+
+ def self.build(tree)
+ @tree = Tree.new(tree)
+ end
+
+ def evaluate(variables)
+ case @tree[0]
+ when :number then @tree[1]
+ when :variable then variables[@tree[1]]
+ when :- then -Expr.build(@tree[1])
+ when :+ then Expr.build(@tree[1]) + Expr.build(@tree[2])
+ when :* then Expr.build(@tree[1]) * Expr.build(@tree[2])
+ end
+ end
+
+ def ==(other)
+ @tree == other.tree
+ end
+end
+
+class Tree
+ attr_accessor :tree
+
+ def initialize(tree)
+ @tree = tree
+ end
+
+ def evaluate(variables)
+ case @tree[0]
+ when :number then @tree[1]
+ when :variable
+ raise 'VariableNotSetException' if variables[@tree[1]] == nil
+ variables[@tree[1]]
+ when :- then -Tree.new(@tree[1]).evaluate(variables)
+ when :+ then Tree.new(@tree[1]).evaluate(variables) + Tree.new(@tree[2]).evaluate(variables)
+ when :* then Tree.new(@tree[1]).evaluate(variables) * Tree.new(@tree[2]).evaluate(variables)
+ when :sin then Math.sin(Tree.new(@tree[1]).evaluate(variables))
+ when :cos then Math.cos(Tree.new(@tree[1]).evaluate(variables))
+ end
+ end
+
+ def ==(other)
+ @tree == other.tree
+ end
+
+ def simplify
+ simplify_array(@tree)
+ @tree = @tree[2] if @tree[0] == :+ && @tree[1][0] == :number && @tree[1][1] == 0
+ @tree = @tree[1] if @tree[0] == :+ && @tree[2][0] == :number && @tree[2][1] == 0
+ @tree = @tree[2] if @tree[0] == :* && @tree[1][0] == :number && @tree[1][1] == 1
+ @tree = @tree[1] if @tree[0] == :* && @tree[2][0] == :number && @tree[2][1] == 1
+ @tree = @tree[1] if @tree[0] == :* && @tree[1][0] == :number && @tree[1][1] == 0
+ @tree = @tree[2] if @tree[0] == :* && @tree[2][0] == :number && @tree[2][1] == 0
+ @tree = @tree[1] if @tree[0] == :sin && @tree[1][1] == 0
+ self
+ end
+
+ def simplify_array(array)
+ @tree.each_with_index do |node, index|
+ @tree[index] = node[2] if node[0] == :+ && node[1][0] == :number && node[1][1] == 0
+ @tree[index] = node[1] if node[0] == :+ && node[2][0] == :number && node[2][1] == 0
+ @tree[index] = node[2] if node[0] == :* && node[1][0] == :number && node[1][1] == 1
+ @tree[index] = node[1] if node[0] == :* && node[2][0] == :number && node[2][1] == 1
+ @tree[index] = node[1] if node[0] == :* && node[1][0] == :number && node[1][1] == 0
+ @tree[index] = node[2] if node[0] == :* && node[2][0] == :number && node[2][1] == 0
+ @tree[index] = node[1] if node[0] == :sin && node[1][1] == 0
+ simplify_array(node[1]) if node[1].class == Array
+ simplify_array(node[2]) if node[2].class == Array
+ end
+ end
+
+ def derive(argument)
+
+ end
+end