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

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

Към профила на Нели Хатева

Резултати

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

Код

class Expr
attr_reader :sexpression, :environment
def self.build(sexpression)
new sexpression
end
def initialize(sexpression)
@sexpression = sexpression
end
def evaluate(environment)
if sexpression.length == 2
Unary.build(sexpression).evaluate(environment)
elsif sexpression.length == 3
Binary.build(sexpression).evaluate(environment)
end
end
def ==(other)
@sexpression == other.sexpression
end
end
class Unary < Expr
def self.build(sexpression)
case sexpression[0]
when :-
Negation.new(sexpression[1])
when :sin
Sine.new(sexpression[1])
when :cos
Cosine.new(sexpression[1])
when :variable
Variable.new(sexpression[1])
when :number
Number.new(sexpression[1])
end
end
end
class Number < Unary
def initialize(number)
@number = number
end
def evaluate(environment = {})
@number
end
end
class Variable < Unary
def initialize(name)
@name = name
end
def evaluate(environment = {})
environment[@name]
end
end
class Negation < Unary
def initialize(operand)
@operand = Expr.build(operand)
end
def evaluate(environment = {})
- @operand.evaluate(environment)
end
end
class Sine < Unary
def initialize(operand)
@operand = Expr.build(operand)
end
def evaluate(environment = {})
Math.sin(@operand.evaluate(environment))
end
end
class Cosine < Unary
def initialize(operand)
@operand = Expr.build(operand)
end
def evaluate(environment = {})
Math.cos(@operand.evaluate(environment))
end
end
class Binary < Expr
attr_reader :operation, :left_operand, :right_operand
def self.build(sexpression)
case sexpression[0]
when :+
Addition.new(Expr.build(sexpression[1]), Expr.build(sexpression[2]))
when :*
Multiplication.new(Expr.build(sexpression[1]), Expr.build(sexpression[2]))
end
end
end
class Addition < Binary
def initialize(left_operand, right_operand)
@left_operand, @right_operand = left_operand, right_operand
end
def evaluate(environment = {})
@left_operand.evaluate(environment) + @right_operand.evaluate(environment)
end
end
class Multiplication < Binary
def initialize(left_operand, right_operand)
@left_operand, @right_operand = left_operand, right_operand
end
def evaluate(environment = {})
@left_operand.evaluate(environment) * @right_operand.evaluate(environment)
end
end

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

..FF.........

Failures:

  1) Expressions assignment supports simplification
     Failure/Error: build(string).simplify
     NoMethodError:
       undefined method `simplify' for #<Expr:0x927af70>
     # /tmp/d20130203-23049-1yeonm8/spec.rb:125:in `simplify'
     # /tmp/d20130203-23049-1yeonm8/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)>'

  2) Expressions assignment can derive expressions
     Failure/Error: build(string).derive(:x)
     NoMethodError:
       undefined method `derive' for #<Expr:0x903c614 @sexpression=[:variable, :x]>
     # /tmp/d20130203-23049-1yeonm8/spec.rb:129:in `derive'
     # /tmp/d20130203-23049-1yeonm8/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.04032 seconds
13 examples, 2 failures

Failed examples:

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

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

Нели обнови решението на 11.11.2012 21:57 (преди около 12 години)

+class Expr
+ attr_reader :sexpression, :environment
+
+ def self.build(sexpression)
+ if sexpression.length == 2
+ Unary.build(sexpression)
+ elsif sexpression.length == 3
+ Binary.build(sexpression)
+ end
+ end
+
+ def initialize(sexpression)
+ @sexpression = sexpression
+ end
+
+ def ==(other)
+ @sexpression == other.sexpression
+ end
+end
+
+class Unary < Expr
+ def self.build(sexpression)
+ case sexpression[0]
+ when :-
+ Negation.new(sexpression[1])
+ when :sin
+ Sine.new(sexpression[1])
+ when :cos
+ Cosine.new(sexpression[1])
+ when :variable
+ Variable.new(sexpression[1])
+ when :number
+ Number.new(sexpression[1])
+ end
+ end
+end
+
+class Number < Unary
+ def initialize(number)
+ @number = number
+ end
+
+ def evaluate(environment = {})
+ @number
+ end
+end
+
+class Variable < Unary
+ def initialize(name)
+ @name = name
+ end
+
+ def evaluate(environment = {})
+ environment[@name]
+ end
+end
+
+class Negation < Unary
+ def initialize(operand)
+ @operand = Expr.build(operand)
+ end
+
+ def evaluate(environment = {})
+ - @operand.evaluate(environment)
+ end
+end
+
+class Sine < Unary
+ def initialize(operand)
+ @operand = Expr.build(operand)
+ end
+
+ def evaluate(environment = {})
+ Math.sin(@operand.evaluate(environment))
+ end
+end
+
+class Cosine < Unary
+ def initialize(argument)
+ @operand = Expr.build(operand)
+ end
+
+ def evaluate(environment = {})
+ Math.cos(@operand.evaluate(environment))
+ end
+end
+
+class Binary < Expr
+ attr_reader :operation, :left_operand, :right_operand
+
+ def self.build(sexpression)
+ case sexpression[0]
+ when :+
+ Addition.new(Expr.build(sexpression[1]), Expr.build(sexpression[2]))
+ when :*
+ Multiplication.new(Expr.build(sexpression[1]), Expr.build(sexpression[2]))
+ end
+ end
+end
+
+class Addition < Binary
+ def initialize(left_operand, right_operand)
+ @left_operand, @right_operand = left_operand, right_operand
+ end
+
+ def evaluate(environment = {})
+ @left_operand.evaluate(environment) + @right_operand.evaluate(environment)
+ end
+end
+
+class Multiplication < Binary
+ def initialize(left_operand, right_operand)
+ @left_operand, @right_operand = left_operand, right_operand
+ end
+
+ def evaluate(environment = {})
+ @left_operand.evaluate(environment) * @right_operand.evaluate(environment)
+ end
+end

Нели обнови решението на 11.11.2012 22:03 (преди около 12 години)

class Expr
attr_reader :sexpression, :environment
def self.build(sexpression)
if sexpression.length == 2
Unary.build(sexpression)
elsif sexpression.length == 3
Binary.build(sexpression)
end
end
def initialize(sexpression)
@sexpression = sexpression
end
def ==(other)
@sexpression == other.sexpression
end
end
class Unary < Expr
def self.build(sexpression)
case sexpression[0]
when :-
Negation.new(sexpression[1])
when :sin
Sine.new(sexpression[1])
when :cos
Cosine.new(sexpression[1])
when :variable
Variable.new(sexpression[1])
when :number
Number.new(sexpression[1])
end
end
end
class Number < Unary
def initialize(number)
@number = number
end
def evaluate(environment = {})
@number
end
end
class Variable < Unary
def initialize(name)
@name = name
end
def evaluate(environment = {})
environment[@name]
end
end
class Negation < Unary
def initialize(operand)
@operand = Expr.build(operand)
end
def evaluate(environment = {})
- @operand.evaluate(environment)
end
end
class Sine < Unary
def initialize(operand)
@operand = Expr.build(operand)
end
def evaluate(environment = {})
Math.sin(@operand.evaluate(environment))
end
end
class Cosine < Unary
- def initialize(argument)
+ def initialize(operand)
@operand = Expr.build(operand)
end
def evaluate(environment = {})
Math.cos(@operand.evaluate(environment))
end
end
class Binary < Expr
attr_reader :operation, :left_operand, :right_operand
def self.build(sexpression)
case sexpression[0]
when :+
Addition.new(Expr.build(sexpression[1]), Expr.build(sexpression[2]))
when :*
Multiplication.new(Expr.build(sexpression[1]), Expr.build(sexpression[2]))
end
end
end
class Addition < Binary
def initialize(left_operand, right_operand)
@left_operand, @right_operand = left_operand, right_operand
end
def evaluate(environment = {})
@left_operand.evaluate(environment) + @right_operand.evaluate(environment)
end
end
class Multiplication < Binary
def initialize(left_operand, right_operand)
@left_operand, @right_operand = left_operand, right_operand
end
def evaluate(environment = {})
@left_operand.evaluate(environment) * @right_operand.evaluate(environment)
end
end

Нели обнови решението на 11.11.2012 22:47 (преди около 12 години)

class Expr
attr_reader :sexpression, :environment
def self.build(sexpression)
- if sexpression.length == 2
- Unary.build(sexpression)
- elsif sexpression.length == 3
- Binary.build(sexpression)
- end
+ new sexpression
end
def initialize(sexpression)
@sexpression = sexpression
+ end
+
+ def evaluate(environment)
+ if sexpression.length == 2
+ Unary.build(sexpression).evaluate(environment)
+ elsif sexpression.length == 3
+ Binary.build(sexpression).evaluate(environment)
+ end
end
def ==(other)
@sexpression == other.sexpression
end
end
class Unary < Expr
def self.build(sexpression)
case sexpression[0]
when :-
Negation.new(sexpression[1])
when :sin
Sine.new(sexpression[1])
when :cos
Cosine.new(sexpression[1])
when :variable
Variable.new(sexpression[1])
when :number
Number.new(sexpression[1])
end
end
end
class Number < Unary
def initialize(number)
@number = number
end
def evaluate(environment = {})
@number
end
end
class Variable < Unary
def initialize(name)
@name = name
end
def evaluate(environment = {})
environment[@name]
end
end
class Negation < Unary
def initialize(operand)
@operand = Expr.build(operand)
end
def evaluate(environment = {})
- @operand.evaluate(environment)
end
end
class Sine < Unary
def initialize(operand)
@operand = Expr.build(operand)
end
def evaluate(environment = {})
Math.sin(@operand.evaluate(environment))
end
end
class Cosine < Unary
def initialize(operand)
@operand = Expr.build(operand)
end
def evaluate(environment = {})
Math.cos(@operand.evaluate(environment))
end
end
class Binary < Expr
attr_reader :operation, :left_operand, :right_operand
def self.build(sexpression)
case sexpression[0]
when :+
Addition.new(Expr.build(sexpression[1]), Expr.build(sexpression[2]))
when :*
Multiplication.new(Expr.build(sexpression[1]), Expr.build(sexpression[2]))
end
end
end
class Addition < Binary
def initialize(left_operand, right_operand)
@left_operand, @right_operand = left_operand, right_operand
end
def evaluate(environment = {})
@left_operand.evaluate(environment) + @right_operand.evaluate(environment)
end
end
class Multiplication < Binary
def initialize(left_operand, right_operand)
@left_operand, @right_operand = left_operand, right_operand
end
def evaluate(environment = {})
@left_operand.evaluate(environment) * @right_operand.evaluate(environment)
end
end