Решение на Трета задача от Ангел Николов

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

Към профила на Ангел Николов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 10 успешни тест(а)
  • 3 неуспешни тест(а)

Код

class Expr
def Expr.build(expr)
case expr.first
when :number then Number.new expr[1]
when :variable then Variable.new expr[1]
when :+ then Addition.new expr[1..2]
when :* then Multiplication.new expr[1..2]
when :- then Subtraction.new expr[1..2]
when :sin then Sine.new expr[1]
when :cos then Cosine.new expr[1]
end
end
def ==(other)
other.is_a? self.class and equal_operands? other
end
end
class Unary < Expr
def initialize(operand)
@operand = Expr.build operand
end
attr_reader :operand
def equal_operands?(other)
self.operand == other.operand
end
end
class Binary < Expr
def initialize(operands)
@operands = operands.map { |operand| Expr.build operand }
end
attr_reader :operands
def equal_operands?(other)
self.operands == other.operands
end
end
class Number < Unary
def initialize(number)
@operand = number
end
end
class Addition < Binary
end
class Multiplication < Binary
end
class Variable < Unary
def initialize(variable)
@operand = variable
end
end
class Subtraction < Binary
end
class Sine < Unary
end
class Cosine < Unary
end

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

F.FF.........

Failures:

  1) Expressions assignment supports evaluation
     Failure/Error: build(string).evaluate(env)
     NoMethodError:
       undefined method `evaluate' for #<Addition:0xa09bbcc>
     # /tmp/d20130203-23049-n2h1w9/spec.rb:121:in `evaluate'
     # /tmp/d20130203-23049-n2h1w9/spec.rb:134: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 simplification
     Failure/Error: build(string).simplify
     NoMethodError:
       undefined method `simplify' for #<Addition:0xa302d34>
     # /tmp/d20130203-23049-n2h1w9/spec.rb:125:in `simplify'
     # /tmp/d20130203-23049-n2h1w9/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)>'

  3) Expressions assignment can derive expressions
     Failure/Error: build(string).derive(:x)
     NoMethodError:
       undefined method `derive' for #<Variable:0xa301aec @operand=:x>
     # /tmp/d20130203-23049-n2h1w9/spec.rb:129:in `derive'
     # /tmp/d20130203-23049-n2h1w9/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.02542 seconds
13 examples, 3 failures

Failed examples:

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

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

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

+class Expr
+ def Expr.build(expr)
+ case expr.first
+ when :number then Number.new expr[1]
+ when :variable then Variable.new expr[1]
+ when :+ then Addition.new expr[1..2]
+ when :* then Multiplication.new expr[1..2]
+ when :- then Subtraction.new expr[1..2]
+ when :sin then Sine.new expr[1]
+ when :cos then Cosine.new expr[1]
+ end
+ end
+ def ==(other)
+ other.is_a? self.class and equal_operands? other
+ end
+end
+
+class Unary < Expr
+ def initialize(operand)
+ @operand = Expr.build operand
+ end
+ attr_reader :operand
+ def equal_operands?(other)
+ self.operand == other.operand
+ end
+end
+
+class Binary < Expr
+ def initialize(operands)
+ @operands = operands.map { |operand| Expr.build operand }
+ end
+ attr_reader :operands
+ def equal_operands?(other)
+ self.operands == other.operands
+ end
+end
+
+class Number < Unary
+ def initialize(number)
+ @operand = number
+ end
+end
+
+class Addition < Binary
+end
+
+class Multiplication < Binary
+end
+
+class Variable < Unary
+ def initialize(variable)
+ @operand = variable
+ end
+end
+
+class Subtraction < Binary
+end
+
+class Sine < Unary
+end
+
+class Cosine < Unary
+end