Решение на Трета задача от Петко Борджуков

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

Към профила на Петко Борджуков

Резултати

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

Код

class Expr
def self.build(sexp)
case sexp.shift
when :number then Number.new sexp.shift
when :variable then Variable.new sexp.shift
when :+ then Expr.build(sexp.shift) + Expr.build(sexp.shift)
when :* then Expr.build(sexp.shift) * Expr.build(sexp.shift)
when :- then -Expr.build(sexp.shift)
when :sin then Sine.new Expr.build(sexp.shift)
when :cos then Cosine.new Expr.build(sexp.shift)
end
end
def +(other)
Addition.new self, other
end
def -@
Negation.new self
end
def *(other)
Multiplication.new self, other
end
end
class Unary < Expr
attr_accessor :value
def initialize(value)
@value = value
end
def ==(other)
return false unless self.is_a? other.class
@value == other.value
end
def simplify
self
end
end
class Binary < Expr
attr_accessor :left, :right
def initialize(left, right)
@left, @right = left, right
end
def ==(other)
return false unless self.is_a? other.class
@left == other.left and @right == other.right
end
end
class Number < Unary
def evaluate(environment)
@value
end
def derive(variable)
Number.new(0)
end
end
class Addition < Binary
def evaluate(environment)
@left.evaluate(environment) + @right.evaluate(environment)
end
def simplify
sides = [@left.simplify, @right.simplify]
return Number.new evaluate({}) if sides.all? { |side| side.is_a? Number }
sides.delete_if { |side| side == Number.new(0) }
sides.size > 0 ? sides.inject(:+) : Number.new(0)
end
def derive(variable)
(@left.derive(variable) + @right.derive(variable)).simplify
end
end
class Multiplication < Binary
def evaluate(environment)
@left.evaluate(environment) * @right.evaluate(environment)
end
def simplify
sides = [@left.simplify, @right.simplify]
return Number.new(0) if sides.any? { |side| side == Number.new(0) }
return Number.new evaluate({}) if sides.all? { |side| side.is_a? Number }
sides.delete_if { |side| side == Number.new(1) }
sides.size > 0 ? sides.inject(:*) : Number.new(1)
end
def derive(variable)
(@left.derive(variable) * @right + @left * @right.derive(variable)).simplify
end
end
class Variable < Unary
def evaluate(environment)
environment[value] or raise_exception 'Undefined variable'
end
def derive(variable)
variable == @value ? Number.new(1) : Number.new(0)
end
end
class Negation < Unary
def evaluate(environment)
-@value.evaluate(environment)
end
def simplify
simplified = @value.simplify
return Number.new(0) if simplified == Number.new(0)
-simplified
end
def derive(variable)
(-@value.derive(variable)).simplify
end
end
class Sine < Unary
def evaluate(environment)
Math.sin @value.evaluate(environment)
end
def simplify
simplified = @value.simplify
return Number.new(0) if simplified == Number.new(0)
Sine.new simplified
end
def derive(variable)
(@value.derive(variable) * Cosine.new(value)).simplify
end
end
class Cosine < Unary
def evaluate(environment)
Math.cos @value.evaluate(environment)
end
def simplify
simplified = @value.simplify
return Number.new(1) if simplified == Number.new(0)
Cosine.new simplified
end
def derive(variable)
(@value.derive(variable) * (-Sine.new(value))).simplify
end
end

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

.............

Finished in 0.05823 seconds
13 examples, 0 failures

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

Петко обнови решението на 14.11.2012 16:13 (преди над 11 години)

+class Expr
+ def self.build(sexp)
+ case sexp.shift
+ when :number then Number.new sexp.shift
+ when :variable then Variable.new sexp.shift
+ when :+ then Expr.build(sexp.shift) + Expr.build(sexp.shift)
+ when :* then Expr.build(sexp.shift) * Expr.build(sexp.shift)
+ when :- then -Expr.build(sexp.shift)
+ when :sin then Sine.new Expr.build(sexp.shift)
+ when :cos then Cosine.new Expr.build(sexp.shift)
+ end
+ end
+
+ def +(other)
+ Addition.new self, other
+ end
+
+ def -@
+ Negation.new self
+ end
+
+ def *(other)
+ Multiplication.new self, other
+ end
+end
+
+class Unary < Expr
+ attr_accessor :value
+
+ def initialize(value)
+ @value = value
+ end
+
+ def ==(other)
+ return false unless self.is_a? other.class
+ @value == other.value
+ end
+
+ def simplify
+ self
+ end
+end
+
+class Binary < Expr
+ attr_accessor :left, :right
+
+ def initialize(left, right)
+ @left, @right = left, right
+ end
+
+ def ==(other)
+ return false unless self.is_a? other.class
+ @left == other.left and @right == other.right
+ end
+end
+
+class Number < Unary
+ def evaluate(environment)
+ @value
+ end
+end
+
+class Addition < Binary
+ def evaluate(environment)
+ @left.evaluate(environment) + @right.evaluate(environment)
+ end
+
+ def simplify
+ sides = [@left.simplify, @right.simplify]
+
+ sides.delete_if { |side| side == Number.new(0) }
+ sides.size > 0 ? sides.inject(:+) : Number.new(0)
+ end
+end
+
+class Multiplication < Binary
+ def evaluate(environment)
+ @left.evaluate(environment) * @right.evaluate(environment)
+ end
+
+ def simplify
+ sides = [@left.simplify, @right.simplify]
+
+ return Number.new(0) if sides.any? { |side| side == Number.new(0) }
+ sides.delete_if { |side| side == Number.new(1) }
+ sides.size > 0 ? sides.inject(:*) : Number.new(1)
+ end
+end
+
+class Variable < Unary
+ def evaluate(environment)
+ environment[value] or raise_exception 'Undefined variable'
+ end
+end
+
+class Negation < Unary
+ def evaluate(environment)
+ -@value.evaluate(environment)
+ end
+
+ def simplify
+ simplified = @value.simplify
+ return Number.new(0) if simplified == Number.new(0)
+ -simplified
+ end
+end
+
+class Sine < Unary
+ def evaluate(environment)
+ Math.sin @value.evaluate(environment)
+ end
+
+ def simplify
+ simplified = @value.simplify
+ return Number.new(0) if simplified == Number.new(0)
+ Sine.new simplified
+ end
+end
+
+class Cosine < Unary
+ def evaluate(environment)
+ Math.cos @value.evaluate(environment)
+ end
+
+ def simplify
+ simplified = @value.simplify
+ return Number.new(1) if simplified == Number.new(0)
+ Cosine.new simplified
+ end
+end

Петко обнови решението на 14.11.2012 16:45 (преди над 11 години)

class Expr
def self.build(sexp)
case sexp.shift
when :number then Number.new sexp.shift
when :variable then Variable.new sexp.shift
when :+ then Expr.build(sexp.shift) + Expr.build(sexp.shift)
when :* then Expr.build(sexp.shift) * Expr.build(sexp.shift)
when :- then -Expr.build(sexp.shift)
when :sin then Sine.new Expr.build(sexp.shift)
when :cos then Cosine.new Expr.build(sexp.shift)
end
end
def +(other)
Addition.new self, other
end
def -@
Negation.new self
end
def *(other)
Multiplication.new self, other
end
end
class Unary < Expr
attr_accessor :value
def initialize(value)
@value = value
end
def ==(other)
return false unless self.is_a? other.class
@value == other.value
end
def simplify
self
end
end
class Binary < Expr
attr_accessor :left, :right
def initialize(left, right)
@left, @right = left, right
end
def ==(other)
return false unless self.is_a? other.class
@left == other.left and @right == other.right
end
end
class Number < Unary
def evaluate(environment)
@value
end
+
+ def derive(variable)
+ Number.new(0)
+ end
end
class Addition < Binary
def evaluate(environment)
@left.evaluate(environment) + @right.evaluate(environment)
end
def simplify
sides = [@left.simplify, @right.simplify]
+ return Number.new evaluate({}) if sides.all? { |side| side.is_a? Number }
+
sides.delete_if { |side| side == Number.new(0) }
sides.size > 0 ? sides.inject(:+) : Number.new(0)
end
+
+ def derive(variable)
+ (@left.derive(variable) + @right.derive(variable)).simplify
+ end
end
class Multiplication < Binary
def evaluate(environment)
@left.evaluate(environment) * @right.evaluate(environment)
end
def simplify
sides = [@left.simplify, @right.simplify]
return Number.new(0) if sides.any? { |side| side == Number.new(0) }
+ return Number.new evaluate({}) if sides.all? { |side| side.is_a? Number }
+
sides.delete_if { |side| side == Number.new(1) }
sides.size > 0 ? sides.inject(:*) : Number.new(1)
end
+
+ def derive(variable)
+ (@left.derive(variable) * @right + @left * @right.derive(variable)).simplify
+ end
end
class Variable < Unary
def evaluate(environment)
environment[value] or raise_exception 'Undefined variable'
end
+
+ def derive(variable)
+ variable == @value ? Number.new(1) : Number.new(0)
+ end
end
class Negation < Unary
def evaluate(environment)
-@value.evaluate(environment)
end
def simplify
simplified = @value.simplify
return Number.new(0) if simplified == Number.new(0)
-simplified
end
+
+ def derive(variable)
+ -@value.derive(variable)
+ end
end
class Sine < Unary
def evaluate(environment)
Math.sin @value.evaluate(environment)
end
def simplify
simplified = @value.simplify
return Number.new(0) if simplified == Number.new(0)
Sine.new simplified
end
+
+ def derive(variable)
+ @value.derive(variable) * Cosine.new(value)
+ end
end
class Cosine < Unary
def evaluate(environment)
Math.cos @value.evaluate(environment)
end
def simplify
simplified = @value.simplify
return Number.new(1) if simplified == Number.new(0)
Cosine.new simplified
end
-end
+
+ def derive(variable)
+ @value.derive(variable) * (-Sine.new(value))
+ end
+end

Петко обнови решението на 14.11.2012 16:47 (преди над 11 години)

class Expr
def self.build(sexp)
case sexp.shift
when :number then Number.new sexp.shift
when :variable then Variable.new sexp.shift
when :+ then Expr.build(sexp.shift) + Expr.build(sexp.shift)
when :* then Expr.build(sexp.shift) * Expr.build(sexp.shift)
when :- then -Expr.build(sexp.shift)
when :sin then Sine.new Expr.build(sexp.shift)
when :cos then Cosine.new Expr.build(sexp.shift)
end
end
def +(other)
Addition.new self, other
end
def -@
Negation.new self
end
def *(other)
Multiplication.new self, other
end
end
class Unary < Expr
attr_accessor :value
def initialize(value)
@value = value
end
def ==(other)
return false unless self.is_a? other.class
@value == other.value
end
def simplify
self
end
end
class Binary < Expr
attr_accessor :left, :right
def initialize(left, right)
@left, @right = left, right
end
def ==(other)
return false unless self.is_a? other.class
@left == other.left and @right == other.right
end
end
class Number < Unary
def evaluate(environment)
@value
end
def derive(variable)
Number.new(0)
end
end
class Addition < Binary
def evaluate(environment)
@left.evaluate(environment) + @right.evaluate(environment)
end
def simplify
sides = [@left.simplify, @right.simplify]
return Number.new evaluate({}) if sides.all? { |side| side.is_a? Number }
sides.delete_if { |side| side == Number.new(0) }
sides.size > 0 ? sides.inject(:+) : Number.new(0)
end
def derive(variable)
(@left.derive(variable) + @right.derive(variable)).simplify
end
end
class Multiplication < Binary
def evaluate(environment)
@left.evaluate(environment) * @right.evaluate(environment)
end
def simplify
sides = [@left.simplify, @right.simplify]
return Number.new(0) if sides.any? { |side| side == Number.new(0) }
return Number.new evaluate({}) if sides.all? { |side| side.is_a? Number }
sides.delete_if { |side| side == Number.new(1) }
sides.size > 0 ? sides.inject(:*) : Number.new(1)
end
def derive(variable)
(@left.derive(variable) * @right + @left * @right.derive(variable)).simplify
end
end
class Variable < Unary
def evaluate(environment)
environment[value] or raise_exception 'Undefined variable'
end
def derive(variable)
variable == @value ? Number.new(1) : Number.new(0)
end
end
class Negation < Unary
def evaluate(environment)
-@value.evaluate(environment)
end
def simplify
simplified = @value.simplify
return Number.new(0) if simplified == Number.new(0)
-simplified
end
def derive(variable)
- -@value.derive(variable)
+ (-@value.derive(variable)).simplify
end
end
class Sine < Unary
def evaluate(environment)
Math.sin @value.evaluate(environment)
end
def simplify
simplified = @value.simplify
return Number.new(0) if simplified == Number.new(0)
Sine.new simplified
end
def derive(variable)
- @value.derive(variable) * Cosine.new(value)
+ (@value.derive(variable) * Cosine.new(value)).simplify
end
end
class Cosine < Unary
def evaluate(environment)
Math.cos @value.evaluate(environment)
end
def simplify
simplified = @value.simplify
return Number.new(1) if simplified == Number.new(0)
Cosine.new simplified
end
def derive(variable)
- @value.derive(variable) * (-Sine.new(value))
+ (@value.derive(variable) * (-Sine.new(value))).simplify
end
end