Решение на Втора задача от Евгений Янев

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

Към профила на Евгений Янев

Резултати

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

Код

require 'set'
class NumberSet
include Enumerable
def initialize()
@numbers = Set.new
end
def <<(n)
@numbers.each do |number|
if number == n
return
end
end
@numbers << n
self
end
def each
if block_given?
@numbers.each { |e| yield(e) }
end
end
def [](filter)
filter.call(@numbers)
end
def size
return @numbers.size
end
def empty?
@numbers.empty?
end
def to_a
@numbers.to_a
end
def self.from_s(set)
new_numbers = NumberSet.new
set.each do |number|
new_numbers << number
end
return new_numbers
end
end
class Command
def execute()
end
end
class BaseFilter < Command
def initialize()
@filters = Array.new
end
def execute(numbers, new_numbers)
set = Set.new(new_numbers.to_a)
return self.call(set)
end
def call(numbers)
new_numbers = NumberSet.from_s(numbers)
while not @filters.empty? do
command = @filters.shift
new_numbers = command.execute(numbers, new_numbers)
if not new_numbers.empty?
return new_numbers
end
end
return new_numbers
end
def add(filter)
@filters.push(filter)
end
def &(filter)
new_filter = BaseFilter.new
new_filter.add(self)
new_filter.add(AndOperation.new)
new_filter.add(filter)
return new_filter
end
def |(filter)
new_filter = BaseFilter.new
new_filter.add(self)
new_filter.add(OrOperation.new)
new_filter.add(filter)
return new_filter
end
end
class Operation < Command
def execute(numbers, new_numbers)
return self.call(numbers, new_numbers)
end
end
class OrOperation < Operation
def call(numbers, new_numbers)
if new_numbers.empty?
return NumberSet.from_s(numbers)
else
return new_numbers
end
end
end
class AndOperation < Operation
def call(numbers, new_numbers)
return new_numbers
end
end
class Filter < BaseFilter
def initialize(&block)
@block = block
end
def call(numbers)
new_numbers = NumberSet.new
numbers.each do |n|
if @block.call(n)
new_numbers << n
end
end
return new_numbers
end
end
class SignFilter < BaseFilter
def initialize(type)
@type = type
end
def call(numbers)
new_numbers = NumberSet.new
case @type
when :positive then get_positive(numbers, new_numbers)
when :negative then get_negative(numbers, new_numbers)
when :non_positive then get_non_positive(numbers, new_numbers)
when :non_negative then get_non_negative(numbers, new_numbers)
end
return new_numbers
end
def get_positive(numbers, new_numbers)
numbers.each do |n|
if n > 0
new_numbers << n
end
end
end
def get_negative(numbers, new_numbers)
numbers.each do |n|
if n < 0
new_numbers << n
end
end
end
def get_non_positive(numbers, new_numbers)
numbers.each do |n|
if n <= 0
new_numbers << n
end
end
end
def get_non_negative(numbers, new_numbers)
numbers.each do |n|
if n >= 0
new_numbers << n
end
end
end
end

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

........FFF.....FFFFFF.F

Failures:

  1) NumberSet can filter by complex type
     Failure/Error: can_filter [0.3+2i, 3.14, 11], using: TypeFilter.new(:complex),
     NameError:
       uninitialized constant TypeFilter
     # /tmp/d20141028-18133-5pnkgf/spec.rb:43: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 filter by integer type
     Failure/Error: can_filter [Rational(5, 2), 8.0, 7, Rational(9, 1)], using: TypeFilter.new(:integer),
     NameError:
       uninitialized constant TypeFilter
     # /tmp/d20141028-18133-5pnkgf/spec.rb:49: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 filter by real type
     Failure/Error: can_filter [Rational(5, 2), 8.3, 7], using: TypeFilter.new(:real),
     NameError:
       uninitialized constant TypeFilter
     # /tmp/d20141028-18133-5pnkgf/spec.rb:55: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 two filters with "and" rule
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20141028-18133-5pnkgf/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-5pnkgf/spec.rb:92: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 two filters with "or" rule
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 7
            got: 5
       
       (compared using ==)
     # /tmp/d20141028-18133-5pnkgf/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-5pnkgf/spec.rb:99: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" rule
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 5
            got: 14
       
       (compared using ==)
     # /tmp/d20141028-18133-5pnkgf/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-5pnkgf/spec.rb:109: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 "or" rule
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 11
            got: 7
       
       (compared using ==)
     # /tmp/d20141028-18133-5pnkgf/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-5pnkgf/spec.rb:119: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 can combine multiple filters with "and" and "or" rules
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 8
            got: 7
       
       (compared using ==)
     # /tmp/d20141028-18133-5pnkgf/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-5pnkgf/spec.rb:129: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)>'

  9) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 4
            got: 7
       
       (compared using ==)
     # /tmp/d20141028-18133-5pnkgf/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-5pnkgf/spec.rb:139: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)>'

  10) 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
       
       expected: 3
            got: 0
       
       (compared using ==)
     # /tmp/d20141028-18133-5pnkgf/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.02877 seconds
24 examples, 10 failures

Failed examples:

rspec /tmp/d20141028-18133-5pnkgf/spec.rb:42 # NumberSet can filter by complex type
rspec /tmp/d20141028-18133-5pnkgf/spec.rb:48 # NumberSet can filter by integer type
rspec /tmp/d20141028-18133-5pnkgf/spec.rb:54 # NumberSet can filter by real type
rspec /tmp/d20141028-18133-5pnkgf/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-5pnkgf/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-5pnkgf/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-5pnkgf/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-5pnkgf/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-5pnkgf/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-5pnkgf/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

Евгений обнови решението на 27.10.2014 15:25 (преди около 10 години)

+require 'set'
+
+class NumberSet
+
+ include Enumerable
+
+ def initialize()
+ @numbers = Set.new
+ end
+
+ def <<(n)
+ @numbers.each do |number|
+ if number == n
+ return
+ end
+ end
+ @numbers << n
+ self
+ end
+
+ def each
+ if block_given?
+ @numbers.each { |e| yield(e) }
+ end
+ end
+
+ def [](filter)
+ filter.call(@numbers)
+ end
+
+ def size
+ return @numbers.size
+ end
+
+ def empty?
+ @numbers.empty?
+ end
+
+ def to_a
+ @numbers.to_a
+ end
+
+ def self.from_s(set)
+ new_numbers = NumberSet.new
+ set.each do |number|
+ new_numbers << number
+ end
+ return new_numbers
+ end
+
+end
+
+class Command
+ def execute()
+
+ end
+end
+
+class BaseFilter < Command
+ def initialize()
+ @filters = Array.new
+ end
+
+ def execute(numbers, new_numbers)
+ set = Set.new(new_numbers.to_a)
+ return self.call(set)
+ end
+
+ def call(numbers)
+ new_numbers = NumberSet.from_s(numbers)
+ while not @filters.empty? do
+ command = @filters.shift
+ new_numbers = command.execute(numbers, new_numbers)
+ if not new_numbers.empty?
+ return new_numbers
+ end
+ end
+ return new_numbers
+ end
+ def add(filter)
+ @filters.push(filter)
+ end
+ def &(filter)
+ new_filter = BaseFilter.new
+ new_filter.add(self)
+ new_filter.add(AndOperation.new)
+ new_filter.add(filter)
+ return new_filter
+ end
+
+ def |(filter)
+ new_filter = BaseFilter.new
+ new_filter.add(self)
+ new_filter.add(OrOperation.new)
+ new_filter.add(filter)
+ return new_filter
+ end
+
+end
+
+class Operation < Command
+
+ def execute(numbers, new_numbers)
+ return self.call(numbers, new_numbers)
+ end
+
+end
+
+class OrOperation < Operation
+
+ def call(numbers, new_numbers)
+ if new_numbers.empty?
+ return NumberSet.from_s(numbers)
+ else
+ return new_numbers
+ end
+ end
+end
+class AndOperation < Operation
+ def call(numbers, new_numbers)
+ return new_numbers
+ end
+end
+class Filter < BaseFilter
+ def initialize(&block)
+ @block = block
+ end
+ def call(numbers)
+ new_numbers = NumberSet.new
+ numbers.each do |n|
+ if @block.call(n)
+ new_numbers << n
+ end
+ end
+ return new_numbers
+ end
+end
+class SignFilter < BaseFilter
+ def initialize(type)
+ @type = type
+ end
+ def call(numbers)
+ new_numbers = NumberSet.new
+ case @type
+ when :positive then get_positive(numbers, new_numbers)
+ when :negative then get_negative(numbers, new_numbers)
+ when :non_positive then get_non_positive(numbers, new_numbers)
+ when :non_negative then get_non_negative(numbers, new_numbers)
+ end
+ return new_numbers
+ end
+ def get_positive(numbers, new_numbers)
+ numbers.each do |n|
+ if n > 0
+ new_numbers << n
+ end
+ end
+ end
+ def get_negative(numbers, new_numbers)
+ numbers.each do |n|
+ if n < 0
+ new_numbers << n
+ end
+ end
+ end
+ def get_non_positive(numbers, new_numbers)
+ numbers.each do |n|
+ if n <= 0
+ new_numbers << n
+ end
+ end
+ end
+ def get_non_negative(numbers, new_numbers)
+ numbers.each do |n|
+ if n >= 0
+ new_numbers << n
+ end
+ end
+ end
+end