Решение на Втора задача от Александър Александров

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

Към профила на Александър Александров

Резултати

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

Код

class Filter
def initialize (&block)
@data_filter = block
end
def filter (number)
@data_filter.call(number)
end
def & (other)
Filter.new { |number| filter(number) && other.filter(number)}
end
def | (other)
Filter.new { |number| filter(number) || other.filter(number)}
end
end
class SignFilter < Filter
def initialize (sign)
@sign = sign
end
def filter (number)
case @sign
when :negative then number < 0
when :positive then number > 0
when :non_negative then number >= 0
when :non_positive then number <= 0
end
end
end
class TypeFilter < Filter
def initialize (sign)
@sign = sign
end
def filter (number)
case @sign
when :complex
number.is_a?(Complex)
when :integer
number.is_a?(Integer)
when :real
number.is_a?(Rational) || number.is_a?(Float)
end
end
end
class NumberSet
include Enumerable
attr_accessor :array
def initialize
@array = []
end
def each
if (block_given?)
@array.each do |number|
yield(number)
end
else
@array.each
end
end
def << (number)
@array << number unless @array.include?(number)
end
def size
@array.size
end
def empty?
@array.empty?
end
def [] (function)
new_obj = NumberSet.new
new_obj.array = Array.new(@array.select{ |number| function.filter(number)})
new_obj
end
end

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

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

Finished in 0.02207 seconds
24 examples, 0 failures

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

Александър обнови решението на 23.10.2014 01:45 (преди около 10 години)

+class Filter
+
+ attr_reader :data_filter
+
+ def initialize &block
+ @data_filter = block
+ end
+
+ def filter number
+ @data_filter.call(number)
+ end
+
+ def & big_filter
+ Filter.new { |number| filter(number) && big_filter.filter(number)}
+ end
+
+ def | big_filter
+ Filter.new { |number| filter(number) || big_filter.filter(number)}
+ end
+end
+
+class SignFilter
+
+ def initialize(sign)
+ @sign = sign
+ end
+
+ def filter number
+ return true if (@sign == :negative && number < 0)
+ return true if (@sign == :positive && number > 0)
+ return true if (@sign == :non_negative && number >= 0)
+ true if (@sign == :non_positive && number <= 0)
+ end
+
+ def & big_filter
+ Filter.new { |number| filter(number) && big_filter.filter(number)}
+ end
+
+ def | big_filter
+ Filter.new { |number| filter(number) || big_filter.filter(number)}
+ end
+end
+
+class TypeFilter
+
+ def initialize(sign)
+ @sign = sign
+ end
+
+ def filter number
+ return true if (@sign == :complex && number.is_a?(Complex) == true)
+ return true if (@sign == :integer && number.is_a?(Integer) == true)
+ return true if (@sign == :real && number.is_a?(Rational) == true)
+ true if (@sign == :real && number.is_a?(Float) == true)
+ end
+
+ def & big_filter
+ Filter.new { |number| filter(number) && big_filter.filter(number)}
+ end
+
+ def | big_filter
+ Filter.new { |number| filter(number) || big_filter.filter(number)}
+ end
+end
+
+class NumberSet
+
+ include Enumerable
+
+ def initialize
+ @array = []
+ end
+
+ def each &block
+ @array.each do |number|
+ block.call (number)
+ end
+ end
+
+ def << number
+ @array << number unless @array.include?(number)
+ end
+
+ def size
+ @array.size
+ end
+
+ def empty?
+ @array.empty?
+ end
+
+ def [] big_filter
+ @@to_return = NumberSet.new
+ @@to_return = @array.select{ |number| big_filter.filter(number) }
+ @@to_return
+ end
+end

Александър обнови решението на 23.10.2014 01:51 (преди около 10 години)

class Filter
attr_reader :data_filter
def initialize &block
@data_filter = block
end
def filter number
@data_filter.call(number)
end
def & big_filter
Filter.new { |number| filter(number) && big_filter.filter(number)}
end
def | big_filter
Filter.new { |number| filter(number) || big_filter.filter(number)}
end
end
class SignFilter
def initialize(sign)
@sign = sign
end
def filter number
return true if (@sign == :negative && number < 0)
return true if (@sign == :positive && number > 0)
return true if (@sign == :non_negative && number >= 0)
true if (@sign == :non_positive && number <= 0)
end
def & big_filter
Filter.new { |number| filter(number) && big_filter.filter(number)}
end
def | big_filter
Filter.new { |number| filter(number) || big_filter.filter(number)}
end
end
class TypeFilter
def initialize(sign)
@sign = sign
end
def filter number
return true if (@sign == :complex && number.is_a?(Complex) == true)
return true if (@sign == :integer && number.is_a?(Integer) == true)
return true if (@sign == :real && number.is_a?(Rational) == true)
true if (@sign == :real && number.is_a?(Float) == true)
end
def & big_filter
Filter.new { |number| filter(number) && big_filter.filter(number)}
end
def | big_filter
Filter.new { |number| filter(number) || big_filter.filter(number)}
end
end
class NumberSet
- include Enumerable
+ include Enumerable
- def initialize
- @array = []
+ def initialize
+ @array = []
end
- def each &block
- @array.each do |number|
- block.call (number)
- end
- end
+ def each &block
+ @array.each do |number|
+ block.call (number)
+ end
+ end
- def << number
- @array << number unless @array.include?(number)
- end
+ def << number
+ @array << number unless @array.include?(number)
+ end
- def size
- @array.size
- end
+ def size
+ @array.size
+ end
- def empty?
- @array.empty?
- end
+ def empty?
+ @array.empty?
+ end
- def [] big_filter
+ def [] big_filter
@@to_return = NumberSet.new
- @@to_return = @array.select{ |number| big_filter.filter(number) }
+ @@to_return = @array.select{ |number| big_filter.filter(number) }
@@to_return
- end
+ end
end

Александър обнови решението на 23.10.2014 01:56 (преди около 10 години)

class Filter
attr_reader :data_filter
def initialize &block
@data_filter = block
end
def filter number
@data_filter.call(number)
end
def & big_filter
Filter.new { |number| filter(number) && big_filter.filter(number)}
end
def | big_filter
Filter.new { |number| filter(number) || big_filter.filter(number)}
end
end
class SignFilter
def initialize(sign)
@sign = sign
end
def filter number
return true if (@sign == :negative && number < 0)
return true if (@sign == :positive && number > 0)
return true if (@sign == :non_negative && number >= 0)
true if (@sign == :non_positive && number <= 0)
end
def & big_filter
Filter.new { |number| filter(number) && big_filter.filter(number)}
end
def | big_filter
Filter.new { |number| filter(number) || big_filter.filter(number)}
end
end
class TypeFilter
def initialize(sign)
@sign = sign
end
def filter number
return true if (@sign == :complex && number.is_a?(Complex) == true)
return true if (@sign == :integer && number.is_a?(Integer) == true)
return true if (@sign == :real && number.is_a?(Rational) == true)
true if (@sign == :real && number.is_a?(Float) == true)
end
def & big_filter
Filter.new { |number| filter(number) && big_filter.filter(number)}
end
def | big_filter
Filter.new { |number| filter(number) || big_filter.filter(number)}
end
end
class NumberSet
include Enumerable
def initialize
- @array = []
- end
+ @array = []
+ end
def each &block
@array.each do |number|
block.call (number)
end
end
def << number
@array << number unless @array.include?(number)
end
def size
@array.size
end
def empty?
- @array.empty?
+ @array.empty?
end
def [] big_filter
@@to_return = NumberSet.new
@@to_return = @array.select{ |number| big_filter.filter(number) }
@@to_return
end
end

Здрасти.

Имам няколко коментара към решението ти:

  • На места кодът ти не е идентиран правилно.
  • Не оставяй празен ред преди първия метод в клас. Същото важи за include-а на Enumerable.
  • Слагай скоби около параметрите при дефиниране метод, когато има такива.
  • Защо ти е reader-а за data_filter? Според мен не ти трябва.
  • Filter#&, TypeFilter#& и SignFilter#& адски много си приличат. Според мен ти трябва точно един от тях. Сещаш ли се как можеш да го постигнеш? Същото за |.
  • Защо си кръщаваш параметрите big_filter? Не намирам никаква логика в това име. Когато става въпрос за параметри на бинарен оператор (каквито са & и |) има конвенция. Погледни style guide-а.
  • Провери как работи Array#each, когато не му подадеш блок. Очакваме NumberSet#each да работи по същия начин.

Помисли как можеш да разпишеш тази конструкция, така че да нямаш нужда от return:

return true if (@sign == :negative && number < 0)
return true if (@sign == :positive && number > 0)
return true if (@sign == :non_negative && number >= 0)
true if (@sign == :non_positive && number <= 0)

Тук не знам, защо си решил да префикснеш името на резултата с @@. Това прави променливата ти клас променлива. Не сме говорили за тях, не са ти необходими тук.

@@to_return = NumberSet.new
@@to_return = @array.select{ |number| big_filter.filter(number) }
@@to_return

Също така първия ред не е необходим, защото втория ред зачиства постигнатото от първия. Тоест, от гледна точка на резултата от извикване на метода, тези три реда са еквивалентни на това:

def [](big_filter)
  @array.select{ |number| big_filter.filter(number) }
end

Тук трябва да коригираш малко поведението на метода. Ние очакваме NumberSet#[] да връща NumberSet обект, а ти връщаш Array обект.

Успех! :)

Александър обнови решението на 24.10.2014 21:28 (преди около 10 години)

class Filter
-
- attr_reader :data_filter
-
- def initialize &block
+ def initialize (&block)
@data_filter = block
end
- def filter number
+ def filter (number)
@data_filter.call(number)
end
- def & big_filter
- Filter.new { |number| filter(number) && big_filter.filter(number)}
+ def & (other)
+ Filter.new { |number| filter(number) && other.filter(number)}
end
- def | big_filter
- Filter.new { |number| filter(number) || big_filter.filter(number)}
+ def | (other)
+ Filter.new { |number| filter(number) || other.filter(number)}
end
end
-class SignFilter
-
- def initialize(sign)
+class SignFilter < Filter
+ def initialize (sign)
@sign = sign
end
- def filter number
- return true if (@sign == :negative && number < 0)
- return true if (@sign == :positive && number > 0)
- return true if (@sign == :non_negative && number >= 0)
- true if (@sign == :non_positive && number <= 0)
+ def filter (number)
+ case @sign
+ when :negative then number < 0
+ when :positive then number > 0
+ when :non_negative then number >= 0
+ when :non_positive then number <= 0
+ end
end
-
- def & big_filter
- Filter.new { |number| filter(number) && big_filter.filter(number)}
- end
-
- def | big_filter
- Filter.new { |number| filter(number) || big_filter.filter(number)}
- end
end
-class TypeFilter
-
- def initialize(sign)
+class TypeFilter < Filter
+ def initialize (sign)
@sign = sign
end
- def filter number
- return true if (@sign == :complex && number.is_a?(Complex) == true)
- return true if (@sign == :integer && number.is_a?(Integer) == true)
- return true if (@sign == :real && number.is_a?(Rational) == true)
- true if (@sign == :real && number.is_a?(Float) == true)
+ def filter (number)
+ case @sign
+ when :complex
+ number.is_a?(Complex)
+ when :integer
+ number.is_a?(Integer)
+ when :real
+ number.is_a?(Rational) || number.is_a?(Float)
+ end
end
-
- def & big_filter
- Filter.new { |number| filter(number) && big_filter.filter(number)}
- end
-
- def | big_filter
- Filter.new { |number| filter(number) || big_filter.filter(number)}
- end
end
class NumberSet
-
include Enumerable
-
+ attr_accessor :array
def initialize
@array = []
end
- def each &block
- @array.each do |number|
- block.call (number)
+ def each
+ if (block_given?)
+ @array.each do |number|
+ yield(number)
+ end
+ else
+ @array.each
end
end
- def << number
+ def << (number)
@array << number unless @array.include?(number)
end
def size
@array.size
end
def empty?
@array.empty?
end
- def [] big_filter
- @@to_return = NumberSet.new
- @@to_return = @array.select{ |number| big_filter.filter(number) }
- @@to_return
+ def [] (function)
+ new_obj = NumberSet.new
+ new_obj.array = Array.new(@array.select{ |number| function.filter(number)})
+ new_obj
end
end