Решение на Трета задача от Атанас Пройчев

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

Към профила на Атанас Пройчев

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 9 успешни тест(а)
  • 4 неуспешни тест(а)

Код

class Expr
def self.razb(tree)
case tree[0]
when :+ then Addition.new razb(tree[1]), razb(tree[2])
when :* then Multiplication.new razb(tree[1]), razb(tree[2])
when :number then Number.new tree[1]
when :variable then Variable.new tree[1]
when :sin then Sine.new tree[1]
when :cos then Cosine.new tree[1]
when :- then Negation.new tree[1]
end
end
def self.build(tree)
self.razb(tree).simplify
end
def derive(variable)
derive(variable).simplify
end
def simplify
simplify
end
end
class Number
def initialize(number)
@number = number
end
def evaluate(value)
@number
end
def derive(variable)
Number.new 0
end
def zero?
self == 0
end
def one?
self == 1
end
end
class Variable
def initialize(name)
@variable = name
end
def same_variable?(other)
other == @variable
end
def evaluate(value)
if value.member? @variable
@variable = value.invert.key @variable
else raise 'Undefined variable!'
end
end
def derive(variable)
if same_variable? variable
Number.new 1
else
Number.new 0
end
end
end
class Negation
def initialize(value)
@value = Expr.razb value
end
def evaluate(value)
-(@value.evaluate value)
end
def derive(variable)
Negation.new derive(variable)
end
end
class Sine
def initialize(value)
@value = Expr.razb value
end
def evaluate(value)
Math.sin @value.evaluate value
end
def derive(variable)
Multiplication.new @value.derive(variable), Cosine.new(@value)
end
def simplify
@value.simplify
end
end
class Cosine
def initialize(value)
if value.kind_of? Expr
@value = value
else
@value = Expr.razb value
end
end
def evaluate(value)
Math.cos @value.evaluate value
end
def derive(variable)
#Multiplication.new @value.derive(variable), Negation.new (Sine.new @value)
end
def simplify
@value.simplify
end
end
class Addition
attr_accessor :left, :right
def initialize(left, right)
@left = left
@right = right
end
def evaluate(value)
@left.evaluate(value) + @right.evaluate(value)
end
def derive(variable)
Addition.new @left.derive(variable), @right.derive(variable)
end
def simplify
if @left == Number and @left.zero?
@right.class.new @right
elsif @right == Number and @right.zero?
@left.class.new @left
end
self
end
end
class Multiplication
attr_accessor :left, :right
def initialize(left, right)
@left = left
@right = right
end
def evaluate(value)
@left.evaluate(value) * @right.evaluate(value)
end
def derive(variable)
Addition.new (Multiplication.new @left.derive(variable), @right),
(Multiplication.new @left, @right.derive(variable))
end
def simplify
if @left == Number and @left.zero? or @right == Number and @right.zero?
Number 0
elsif @left == Number and @left.one?
@right.class.new @right
elsif @right == Number and @right.one?
@left.class.new @left
end
self
end
end

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

FFFF.........

Failures:

  1) Expressions assignment supports evaluation
     Failure/Error: Expr.build parse(string)
     NoMethodError:
       undefined method `simplify' for #<Number:0x9693260 @number=0>
     # /tmp/d20130203-23049-au74cr/solution.rb:102:in `simplify'
     # /tmp/d20130203-23049-au74cr/solution.rb:15:in `build'
     # /tmp/d20130203-23049-au74cr/spec.rb:117:in `build'
     # /tmp/d20130203-23049-au74cr/spec.rb:121:in `evaluate'
     # /tmp/d20130203-23049-au74cr/spec.rb:135: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)>'

  2) Expressions assignment supports comparison
     Failure/Error: build('1 + 2 * 3').should eq build('1 + 2 * 3')
       
       expected: #<Addition:0x96dbda8 @left=#<Number:0x96dbdf8 @number=1>, @right=#<Multiplication:0x96dbdbc @left=#<Number:0x96dbde4 @number=2>, @right=#<Number:0x96dbdd0 @number=3>>>
            got: #<Addition:0x96dd568 @left=#<Number:0x96dd5b8 @number=1>, @right=#<Multiplication:0x96dd57c @left=#<Number:0x96dd5a4 @number=2>, @right=#<Number:0x96dd590 @number=3>>>
       
       (compared using ==)
       
       Diff:
       
       @@ -1,7 +1,7 @@
       -#<Addition:0x96dbda8
       - @left=#<Number:0x96dbdf8 @number=1>,
       +#<Addition:0x96dd568
       + @left=#<Number:0x96dd5b8 @number=1>,
         @right=
       -  #<Multiplication:0x96dbdbc
       -   @left=#<Number:0x96dbde4 @number=2>,
       -   @right=#<Number:0x96dbdd0 @number=3>>>
       +  #<Multiplication:0x96dd57c
       +   @left=#<Number:0x96dd5a4 @number=2>,
       +   @right=#<Number:0x96dd590 @number=3>>>
     # /tmp/d20130203-23049-au74cr/spec.rb:141: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)>'

  3) Expressions assignment supports simplification
     Failure/Error: Expr.build parse(string)
     NoMethodError:
       undefined method `simplify' for #<Variable:0x99ef3c8 @variable=:x>
     # /tmp/d20130203-23049-au74cr/solution.rb:15:in `build'
     # /tmp/d20130203-23049-au74cr/spec.rb:117:in `build'
     # /tmp/d20130203-23049-au74cr/spec.rb:146: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)>'

  4) Expressions assignment can derive expressions
     Failure/Error: Expr.build parse(string)
     NoMethodError:
       undefined method `simplify' for #<Variable:0x99ee1a8 @variable=:x>
     # /tmp/d20130203-23049-au74cr/solution.rb:15:in `build'
     # /tmp/d20130203-23049-au74cr/spec.rb:117:in `build'
     # /tmp/d20130203-23049-au74cr/spec.rb:129:in `derive'
     # /tmp/d20130203-23049-au74cr/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.02749 seconds
13 examples, 4 failures

Failed examples:

rspec /tmp/d20130203-23049-au74cr/spec.rb:133 # Expressions assignment supports evaluation
rspec /tmp/d20130203-23049-au74cr/spec.rb:140 # Expressions assignment supports comparison
rspec /tmp/d20130203-23049-au74cr/spec.rb:145 # Expressions assignment supports simplification
rspec /tmp/d20130203-23049-au74cr/spec.rb:160 # Expressions assignment can derive expressions

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

Атанас обнови решението на 14.11.2012 16:38 (преди около 12 години)

+class Expr
+ def self.razb(tree)
+ case tree[0]
+ when :+ then Addition.new razb(tree[1]), razb(tree[2])
+ when :* then Multiplication.new razb(tree[1]), razb(tree[2])
+ when :number then Number.new tree[1]
+ when :variable then Variable.new tree[1]
+ when :sin then Sine.new tree[1]
+ when :cos then Cosine.new tree[1]
+ when :- then Negation.new tree[1]
+ end
+ end
+
+ def self.build(tree)
+ self.razb(tree).simplify
+ end
+
+ def derive(variable)
+ derive(variable).simplify
+ end
+
+ def simplify
+ simplify
+ end
+end
+
+class Number
+ def initialize(number)
+ @number = number
+ end
+
+ def evaluate(value)
+ @number
+ end
+
+ def derive(variable)
+ Number.new 0
+ end
+
+ def zero?
+ self == 0
+ end
+
+ def one?
+ self == 1
+ end
+end
+
+class Variable
+ def initialize(name)
+ @variable = name
+ end
+
+ def same_variable?(other)
+ other == @variable
+ end
+
+ def evaluate(value)
+ if value.member? @variable
+ @variable = value.invert.key @variable
+ else raise 'Undefined variable!'
+ end
+ end
+
+ def derive(variable)
+ if same_variable? variable
+ Number.new 1
+ else
+ Number.new 0
+ end
+ end
+end
+
+class Negation
+ def initialize(value)
+ @value = Expr.razb value
+ end
+
+ def evaluate(value)
+ -(@value.evaluate value)
+ end
+
+ def derive(variable)
+ Negation.new derive(variable)
+ end
+end
+
+class Sine
+ def initialize(value)
+ @value = Expr.razb value
+ end
+
+ def evaluate(value)
+ Math.sin @value.evaluate value
+ end
+
+ def derive(variable)
+ Multiplication.new @value.derive(variable), Cosine.new(@value)
+ end
+
+ def simplify
+ @value.simplify
+ end
+end
+
+class Cosine
+ def initialize(value)
+ if value.kind_of? Expr
+ @value = value
+ else
+ @value = Expr.razb value
+ end
+ end
+
+ def evaluate(value)
+ Math.cos @value.evaluate value
+ end
+
+ def derive(variable)
+ #Multiplication.new @value.derive(variable), Negation.new (Sine.new @value)
+ end
+
+ def simplify
+ @value.simplify
+ end
+end
+
+class Addition
+ attr_accessor :left, :right
+
+ def initialize(left, right)
+ @left = left
+ @right = right
+ end
+
+ def evaluate(value)
+ @left.evaluate(value) + @right.evaluate(value)
+ end
+
+ def derive(variable)
+ Addition.new @left.derive(variable), @right.derive(variable)
+ end
+
+ def simplify
+ if @left == Number and @left.zero?
+ @right.class.new @right
+ elsif @right == Number and @right.zero?
+ @left.class.new @left
+ end
+ self
+ end
+end
+
+class Multiplication
+ attr_accessor :left, :right
+
+ def initialize(left, right)
+ @left = left
+ @right = right
+ end
+
+ def evaluate(value)
+ @left.evaluate(value) * @right.evaluate(value)
+ end
+
+ def derive(variable)
+ Addition.new (Multiplication.new @left.derive(variable), @right),
+ (Multiplication.new @left, @right.derive(variable))
+ end
+
+ def simplify
+ if @left == Number and @left.zero? or @right == Number and @right.zero?
+ Number 0
+ elsif @left == Number and @left.one?
+ @right.class.new @right
+ elsif @right == Number and @right.one?
+ @left.class.new @left
+ end
+ self
+ end
+end