Решение на Втора задача от Васил Бунарджиев

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

Към профила на Васил Бунарджиев

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 16 успешни тест(а)
  • 8 неуспешни тест(а)

Код

class NumberSet
include Enumerable
def initialize
@numbers = []
end
def <<(element)
if not @numbers.include? element
@numbers << element
end
end
def size
return @numbers.size
end
def empty?
return true if @numbers.size == 0
return false
end
def each
@numbers.each {|elements| yield elements}
end
def [](argument)
argument.call(@numbers)
end
end
class SignFilter
def initialize(argument)
@compare_value = argument
end
def call(number_set)
if @compare_value == :positive
return number_set.select { |element| (element > 0)}
end
if @compare_value == :negative
return number_set.select { |element| (element < 0)}
end
if @compare_value == :non_positive
return number_set.select { |element| (element <= 0)}
end
if @compare_value == :non_negative
return number_set.select { |element| (element >= 0)}
end
end
end
class TypeFilter
def initialize(argument)
@value_type = argument
end
def filtered
@filtered
end
def call(number_set)
if @value_type == :integer
return filtered = number_set.select { |element| element.is_a? Integer }
end
if @value_type == :real
return filtered = (number_set.select { |element| element.is_a? Float } +
number_set.select { |element| element.is_a? Rational })
end
if @value_type == :complex
return filtered = number_set.select { |element| element.is_a? Complex }
end
end
def &(argument)
self.call(filtered) + argument.call(filtered)
end
end
#Моля дайте някакъв feedback, дори и след срока за домашното. Трудно
#ми е да вляза толкова бързо в материала и бих се радвал на съвети

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

...........F....FFFFFF.F

Failures:

  1) NumberSet can filter by custom filter
     Failure/Error: can_filter [Rational(5, 2), 7.6, 5], using: Filter.new { |number| number > 4 },
     NameError:
       uninitialized constant Filter
     # /tmp/d20141028-18133-1cxkua5/spec.rb:61:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) NumberSet can combine two filters with "and" rule
     Failure/Error: filter = SignFilter.new(:non_negative) & Filter.new { |number| number != 0 }
     NameError:
       uninitialized constant Filter
     # /tmp/d20141028-18133-1cxkua5/spec.rb:91:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) NumberSet can combine two filters with "or" rule
     Failure/Error: filter = Filter.new { |number| number % 2 == 0 } | Filter.new { |number| number > 5 }
     NameError:
       uninitialized constant Filter
     # /tmp/d20141028-18133-1cxkua5/spec.rb:98:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) NumberSet can combine multiple filters with "and" rule
     Failure/Error: non_zero      = Filter.new { |number| number != 0 }
     NameError:
       uninitialized constant Filter
     # /tmp/d20141028-18133-1cxkua5/spec.rb:106:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) NumberSet can combine multiple filters with "or" rule
     Failure/Error: even          = Filter.new(&:even?)
     NameError:
       uninitialized constant Filter
     # /tmp/d20141028-18133-1cxkua5/spec.rb:115:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: even          = Filter.new(&:even?)
     NameError:
       uninitialized constant Filter
     # /tmp/d20141028-18133-1cxkua5/spec.rb:125:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: even          = Filter.new(&:even?)
     NameError:
       uninitialized constant Filter
     # /tmp/d20141028-18133-1cxkua5/spec.rb:135:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) NumberSet returns enumerable of set's contents if no block is given to each
     Failure/Error: expect(numbers.each.to_a.size).to eq [1, 3, 5].size
     LocalJumpError:
       no block given (yield)
     # /tmp/d20141028-18133-1cxkua5/solution.rb:23:in `block in each'
     # /tmp/d20141028-18133-1cxkua5/solution.rb:23:in `each'
     # /tmp/d20141028-18133-1cxkua5/solution.rb:23:in `each'
     # /tmp/d20141028-18133-1cxkua5/spec.rb:164:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.03054 seconds
24 examples, 8 failures

Failed examples:

rspec /tmp/d20141028-18133-1cxkua5/spec.rb:60 # NumberSet can filter by custom filter
rspec /tmp/d20141028-18133-1cxkua5/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-1cxkua5/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-1cxkua5/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-1cxkua5/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-1cxkua5/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-1cxkua5/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-1cxkua5/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

Васил обнови решението на 27.10.2014 14:54 (преди около 10 години)

+class NumberSet
+ include Enumerable
+ def initialize
+ @numbers = []
+ end
+
+ def <<(element)
+ if not @numbers.include? element
+ @numbers << element
+ end
+ end
+
+ def size
+ return @numbers.size
+ end
+
+ def empty?
+ return true if @numbers.size == 0
+ return false
+ end
+
+ def each
+ @numbers.each {|elements| yield elements}
+ end
+
+ def [](argument)
+ argument.call(@numbers)
+ end
+end
+
+class SignFilter
+ def initialize(argument)
+ @compare_value = argument
+ end
+
+ def call(number_set)
+ if @compare_value == :positive
+ return number_set.select { |element| (element > 0)}
+ end
+ if @compare_value == :negative
+ return number_set.select { |element| (element < 0)}
+ end
+ if @compare_value == :non_positive
+ return number_set.select { |element| (element <= 0)}
+ end
+ if @compare_value == :non_negative
+ return number_set.select { |element| (element >= 0)}
+ end
+ end
+end
+
+class TypeFilter
+ def initialize(argument)
+ @value_type = argument
+ end
+
+ def filtered
+ @filtered
+ end
+
+ def call(number_set)
+ if @value_type == :integer
+ return filtered = number_set.select { |element| element.is_a? Integer }
+ end
+ if @value_type == :real
+ return filtered = (number_set.select { |element| element.is_a? Float } +
+ number_set.select { |element| element.is_a? Rational })
+ end
+ if @value_type == :complex
+ return filtered = number_set.select { |element| element.is_a? Complex }
+ end
+ end
+
+ def &(argument)
+ self.call(filtered) + argument.call(filtered)
+ end
+end

Васил обнови решението на 27.10.2014 14:55 (преди около 10 години)

class NumberSet
include Enumerable
def initialize
@numbers = []
end
def <<(element)
if not @numbers.include? element
@numbers << element
end
end
def size
return @numbers.size
end
def empty?
return true if @numbers.size == 0
return false
end
def each
@numbers.each {|elements| yield elements}
end
def [](argument)
argument.call(@numbers)
end
end
class SignFilter
def initialize(argument)
@compare_value = argument
end
def call(number_set)
if @compare_value == :positive
return number_set.select { |element| (element > 0)}
end
if @compare_value == :negative
return number_set.select { |element| (element < 0)}
end
if @compare_value == :non_positive
return number_set.select { |element| (element <= 0)}
end
if @compare_value == :non_negative
return number_set.select { |element| (element >= 0)}
end
end
end
class TypeFilter
def initialize(argument)
@value_type = argument
end
def filtered
@filtered
end
def call(number_set)
if @value_type == :integer
return filtered = number_set.select { |element| element.is_a? Integer }
end
if @value_type == :real
return filtered = (number_set.select { |element| element.is_a? Float } +
number_set.select { |element| element.is_a? Rational })
end
if @value_type == :complex
return filtered = number_set.select { |element| element.is_a? Complex }
end
end
def &(argument)
self.call(filtered) + argument.call(filtered)
end
-end
+end
+#Моля дайте някакъв feedback, дори и след срока за домашното. Трудно
+#ми е да вляза толкова бързо в материала и бих се радвал на съвети