Решение на Втора задача от Любомир Папазов

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

Към профила на Любомир Папазов

Резултати

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

Код

class NumberSet
include Enumerable
def initialize
@added_numbers = []
end
def <<(number)
@added_numbers << number unless @added_numbers.include?(number)
end
def each(&block)
@added_numbers.each(&block)
end
def size
@added_numbers.length
end
def empty?
@added_numbers.empty?
end
def [](block)
@added_numbers.select!(&block.lambda_function)
self
end
end
class Filter
def initialize(&expression)
@lambda_function = expression
end
attr_accessor :lambda_function
def &(next_filter)
combined = Filter.new
first = lambda_function
second = next_filter.lambda_function
combined.lambda_function = lambda { |x| first.call x and second.call x}
combined
end
def |(next_filter)
combined = Filter.new
first = lambda_function
second = next_filter.lambda_function
combined.lambda_function = lambda { |x| first.call x or second.call x}
combined
end
end
class TypeFilter < Filter
def initialize(type)
real_type = -> (x) { x.class == Rational or x.class == Float }
case type
when :integer then @lambda_function = -> (x) { x.class == Fixnum }
when :complex then @lambda_function = -> (x) { x.class == Complex }
when :real then @lambda_function = real_type
end
end
end
class SignFilter < Filter
def initialize(sign)
case sign
when :non_negative then @lambda_function = -> (x) { x >= 0 }
when :non_positive then @lambda_function = -> (x) { x <= 0 }
when :negative then @lambda_function = -> (x) { x < 0 }
when :positive then @lambda_function = -> (x) { x > 0 }
end
end
end

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

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

Finished in 0.02216 seconds
24 examples, 0 failures

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

Любомир обнови решението на 26.10.2014 12:20 (преди над 9 години)

+class NumberSet
+ include Enumerable
+
+ def initialize
+ @results_array = []
+ end
+
+ def <<(val)
+ already_have_this = false
+ @results_array.each do |element|
+ already_have_this = true if element == val
+ end
+ @results_array << val unless already_have_this
+ end
+
+ def each(&block)
+ @results_array.each(&block)
+ end
+
+ def size
+ counter = 0
+ @results_array.each { |element| counter += 1 }
+ counter
+ end
+
+ def empty?
+ @results_array.empty?
+ end
+
+ def [](var)
+ @results_array.select(&var.lambda_function)
+ end
+end
+
+class AbstractFilter
+ attr_accessor :lambda_function
+
+ def &(next_filter)
+ res = AbstractFilter.new
+ first = self.lambda_function
+ second = next_filter.lambda_function
+ res.lambda_function = lambda { |x| first.call x and second.call x}
+ res
+ end
+
+ def |(next_filter)
+ res = AbstractFilter.new
+ first = self.lambda_function
+ second = next_filter.lambda_function
+ res.lambda_function = lambda { |x| first.call x or second.call x}
+ res
+ end
+
+end
+
+class TypeFilter < AbstractFilter
+ def initialize(type)
+ real_type = lambda { |x| x.class == Rational or x.class == Float }
+ case type
+ when :integer then @lambda_function = lambda { |x| x.class == Fixnum }
+ when :complex then @lambda_function = lambda { |x| x.class == Complex }
+ when :real then @lambda_function = real_type
+ end
+ end
+end
+
+
+class SignFilter < AbstractFilter
+ def initialize(sign)
+ case sign
+ when :non_negative then @lambda_function = lambda { |x| x >= 0 }
+ when :non_positive then @lambda_function = lambda { |x| x <= 0 }
+ when :negative then @lambda_function = lambda { |x| x < 0 }
+ when :positive then @lambda_function = lambda { |x| x > 0 }
+ end
+ end
+end
+
+class Filter < AbstractFilter
+ def initialize(&expression)
+ @lambda_function = expression
+ end
+end

Здрасти,

Това са нещата, които виждам като проблеми в кода ти.

  • Дай малко по-добри имена от results_array и val. В контекста на числа и множества работим, ще намериш нещо по-подходящо.
  • За начина, по който изчисляваш already_have_this вече има метод в Array. :)
  • За начина, по който изчисляваш size вече има метод в Array.
  • Връщаш Array обект от NumberSet. Ние очакваме NumberSet обратно.
  • Защо ти е този AbstractFilter? Не може ли да се слее с Filter?
  • Залагам, че можеш да реализираш Filter#& и Filter#| с анонимна функция и ще стане по-елегантно. Помисли си как можеш да ползваш Filter класа за целта.

Любомир обнови решението на 27.10.2014 01:32 (преди над 9 години)

class NumberSet
include Enumerable
def initialize
- @results_array = []
+ @added_numbers = []
end
- def <<(val)
- already_have_this = false
- @results_array.each do |element|
- already_have_this = true if element == val
- end
- @results_array << val unless already_have_this
+ def <<(number)
+ @added_numbers << number unless @added_numbers.include?(number)
end
def each(&block)
- @results_array.each(&block)
+ @added_numbers.each(&block)
end
def size
- counter = 0
- @results_array.each { |element| counter += 1 }
- counter
+ @added_numbers.length
end
def empty?
- @results_array.empty?
+ @added_numbers.empty?
end
- def [](var)
- @results_array.select(&var.lambda_function)
+ def [](block)
+ @added_numbers.select!(&block.lambda_function)
+ self
end
end
-class AbstractFilter
+class Filter
+ def initialize(&expression)
+ @lambda_function = expression
+ end
+
attr_accessor :lambda_function
def &(next_filter)
- res = AbstractFilter.new
+ res = Filter.new
first = self.lambda_function
second = next_filter.lambda_function
res.lambda_function = lambda { |x| first.call x and second.call x}
res
end
def |(next_filter)
- res = AbstractFilter.new
+ res = Filter.new
first = self.lambda_function
second = next_filter.lambda_function
res.lambda_function = lambda { |x| first.call x or second.call x}
res
end
-
end
-class TypeFilter < AbstractFilter
+
+class TypeFilter < Filter
def initialize(type)
- real_type = lambda { |x| x.class == Rational or x.class == Float }
+ real_type = -> (x) { x.class == Rational or x.class == Float }
case type
- when :integer then @lambda_function = lambda { |x| x.class == Fixnum }
- when :complex then @lambda_function = lambda { |x| x.class == Complex }
+ when :integer then @lambda_function = -> (x) { x.class == Fixnum }
+ when :complex then @lambda_function = -> (x) { x.class == Complex }
when :real then @lambda_function = real_type
end
end
end
-class SignFilter < AbstractFilter
+class SignFilter < Filter
def initialize(sign)
case sign
- when :non_negative then @lambda_function = lambda { |x| x >= 0 }
- when :non_positive then @lambda_function = lambda { |x| x <= 0 }
- when :negative then @lambda_function = lambda { |x| x < 0 }
- when :positive then @lambda_function = lambda { |x| x > 0 }
+ when :non_negative then @lambda_function = -> (x) { x >= 0 }
+ when :non_positive then @lambda_function = -> (x) { x <= 0 }
+ when :negative then @lambda_function = -> (x) { x < 0 }
+ when :positive then @lambda_function = -> (x) { x > 0 }
end
- end
-end
-
-class Filter < AbstractFilter
- def initialize(&expression)
- @lambda_function = expression
end
end

  • results_array и val ги смених, надявам се към по-добро :D
  • методите size и already_has_this си ги смених
  • Връщаш Array обект от NumberSet. Ние очакваме NumberSet обратно. - Връщам ви го тогава NumberSet-а, щом си го искате :)
  • Абстрактния филтър го махнах
  • С Filter#& и Filter#|, обаче не мога нещо да го измисля по-хитро. first и second променливите ги създавам с цел да не превишавам 80те символа, а иначе ще го помисля пак следобяда, ако ли не - ще прегледам чуждите решения през седмицата :)

Любомир обнови решението на 27.10.2014 16:53 (преди над 9 години)

class NumberSet
include Enumerable
def initialize
@added_numbers = []
end
def <<(number)
@added_numbers << number unless @added_numbers.include?(number)
end
def each(&block)
@added_numbers.each(&block)
end
def size
@added_numbers.length
end
def empty?
@added_numbers.empty?
end
def [](block)
@added_numbers.select!(&block.lambda_function)
self
end
end
class Filter
def initialize(&expression)
@lambda_function = expression
end
attr_accessor :lambda_function
def &(next_filter)
- res = Filter.new
- first = self.lambda_function
+ combined = Filter.new
+ first = lambda_function
second = next_filter.lambda_function
- res.lambda_function = lambda { |x| first.call x and second.call x}
- res
+ combined.lambda_function = lambda { |x| first.call x and second.call x}
+ combined
end
def |(next_filter)
- res = Filter.new
- first = self.lambda_function
+ combined = Filter.new
+ first = lambda_function
second = next_filter.lambda_function
- res.lambda_function = lambda { |x| first.call x or second.call x}
- res
+ combined.lambda_function = lambda { |x| first.call x or second.call x}
+ combined
end
end
class TypeFilter < Filter
def initialize(type)
real_type = -> (x) { x.class == Rational or x.class == Float }
case type
when :integer then @lambda_function = -> (x) { x.class == Fixnum }
when :complex then @lambda_function = -> (x) { x.class == Complex }
when :real then @lambda_function = real_type
end
end
end
class SignFilter < Filter
def initialize(sign)
case sign
when :non_negative then @lambda_function = -> (x) { x >= 0 }
when :non_positive then @lambda_function = -> (x) { x <= 0 }
when :negative then @lambda_function = -> (x) { x < 0 }
when :positive then @lambda_function = -> (x) { x > 0 }
end
end
end