Решение на Втора задача от Божидар Григоров

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

Към профила на Божидар Григоров

Резултати

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

Код

require 'set'
class NumberSet
include Enumerable
attr_reader :size
def initialize
@size = 0
@elements = Set.new
end
def each
@elements.each { |element| element}
end
def << number
contains_flag = false
@elements.each do |x|
if x.to_c == number.to_c
contains_flag = true
end
end
if empty? || contains_flag == false
@elements.add number
@size += 1
end
end
def empty?
size == 0
end
def [](filter)
result = NumberSet.new
@elements.each do |item|
result << item if filter.call(item)
end
result
end
end
class Filter
def initialize(&block)
@filter = block
end
def call(*arguments)
p *arguments
@filter.(*arguments)
end
end
class TypeFilter
def initialize(type)
case type
when :integer then @block = lambda { |n| n.is_a? Integer }
when :complex then @block = lambda { |n| n.is_a? Complex }
when :real then @block = lambda { |n| n.is_a? Float || Rational }
end
end
def call(*arguments)
@block.(*arguments)
end
end
class SignFilter
def initialize(sign_filter)
case sign_filter
when :positive then @block = lambda { |n| n > 0 }
when :non_positive then @block = lambda { |n| n <= 0 }
when :negative then @block = lambda { |n| n < 0 }
when :non_negative then @block = lambda { |n| n >= 0 }
end
end
def call(*arguments)
@block.(*arguments)
end
end

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

FFFFF...FFF(5/2)
7.6
5
FFFFFFFFFFFF.

Failures:

  1) NumberSet can store integers
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet:0xb8da1ae8 @size=1, @elements=#<Set: {42}>> to include 42
     # /tmp/d20141028-18133-1ns6qix/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-1ns6qix/spec.rb:3: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 store floating point numbers
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet:0xb8d7decc @size=1, @elements=#<Set: {3.14}>> to include 3.14
     # /tmp/d20141028-18133-1ns6qix/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-1ns6qix/spec.rb:7: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 store complex numbers
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet:0xb8d7ce28 @size=1, @elements=#<Set: {(0.3+2i)}>> to include (0.3+2i)
     # /tmp/d20141028-18133-1ns6qix/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-1ns6qix/spec.rb:11: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 store rational numbers
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet:0xb8d77dc4 @size=1, @elements=#<Set: {(22/7)}>> to include (22/7)
     # /tmp/d20141028-18133-1ns6qix/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-1ns6qix/spec.rb:15: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 compare numbers of different types
     Failure/Error: expect(numbers).to include and_get_it_as
       expected #<NumberSet:0xb8d76d34 @size=1, @elements=#<Set: {(2/1)}>> to include 2
     # /tmp/d20141028-18133-1ns6qix/spec.rb:171:in `can_store'
     # /tmp/d20141028-18133-1ns6qix/spec.rb:19: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 filter by complex type
     Failure/Error: expect(filtered_numbers).to include *expecting
       expected #<NumberSet:0xb8e9e658 @size=1, @elements=#<Set: {(0.3+2i)}>> to include (0.3+2i)
     # /tmp/d20141028-18133-1ns6qix/spec.rb:181:in `can_filter'
     # /tmp/d20141028-18133-1ns6qix/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)>'

  7) NumberSet can filter by integer type
     Failure/Error: expect(filtered_numbers).to include *expecting
       expected #<NumberSet:0xb8e9d118 @size=1, @elements=#<Set: {7}>> to include 7
     # /tmp/d20141028-18133-1ns6qix/spec.rb:181:in `can_filter'
     # /tmp/d20141028-18133-1ns6qix/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)>'

  8) NumberSet can filter by real type
     Failure/Error: expect(filtered_numbers.size).to eq expecting.size
       
       expected: 2
            got: 1
       
       (compared using ==)
     # /tmp/d20141028-18133-1ns6qix/spec.rb:180:in `can_filter'
     # /tmp/d20141028-18133-1ns6qix/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)>'

  9) NumberSet can filter by custom filter
     Failure/Error: expect(filtered_numbers).to include *expecting
       expected #<NumberSet:0xb8e4623c @size=2, @elements=#<Set: {7.6, 5}>> to include 7.6 and 5
     # /tmp/d20141028-18133-1ns6qix/spec.rb:181:in `can_filter'
     # /tmp/d20141028-18133-1ns6qix/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)>'

  10) NumberSet can filter positive numbers
     Failure/Error: expect(filtered_numbers).to include *expecting
       expected #<NumberSet:0xb8e44af4 @size=1, @elements=#<Set: {7.6}>> to include 7.6
     # /tmp/d20141028-18133-1ns6qix/spec.rb:181:in `can_filter'
     # /tmp/d20141028-18133-1ns6qix/spec.rb:67: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)>'

  11) NumberSet can filter non-positive numbers
     Failure/Error: expect(filtered_numbers).to include *expecting
       expected #<NumberSet:0xb8e3f8ec @size=2, @elements=#<Set: {(-5/2), 0}>> to include -2.5 and 0
     # /tmp/d20141028-18133-1ns6qix/spec.rb:181:in `can_filter'
     # /tmp/d20141028-18133-1ns6qix/spec.rb:73: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)>'

  12) NumberSet can filter negative numbers
     Failure/Error: expect(filtered_numbers).to include *expecting
       expected #<NumberSet:0xb8e3e500 @size=1, @elements=#<Set: {(-5/2)}>> to include -2.5
     # /tmp/d20141028-18133-1ns6qix/spec.rb:181:in `can_filter'
     # /tmp/d20141028-18133-1ns6qix/spec.rb:79: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)>'

  13) NumberSet can filter non-negative numbers
     Failure/Error: expect(filtered_numbers).to include *expecting
       expected #<NumberSet:0xb8e3d178 @size=2, @elements=#<Set: {7.6, 0}>> to include 7.6 and 0
     # /tmp/d20141028-18133-1ns6qix/spec.rb:181:in `can_filter'
     # /tmp/d20141028-18133-1ns6qix/spec.rb:85: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)>'

  14) NumberSet can combine two filters with "and" rule
     Failure/Error: filter = SignFilter.new(:non_negative) & Filter.new { |number| number != 0 }
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb8e0bd80>
     # /tmp/d20141028-18133-1ns6qix/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)>'

  15) NumberSet can combine two filters with "or" rule
     Failure/Error: filter = Filter.new { |number| number % 2 == 0 } | Filter.new { |number| number > 5 }
     NoMethodError:
       undefined method `|' for #<Filter:0xb8e0b268>
     # /tmp/d20141028-18133-1ns6qix/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)>'

  16) NumberSet can combine multiple filters with "and" rule
     Failure/Error: filter        = non_negative & non_zero & mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<SignFilter:0xb8e0a4a8>
     # /tmp/d20141028-18133-1ns6qix/spec.rb:108: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)>'

  17) NumberSet can combine multiple filters with "or" rule
     Failure/Error: filter        = even | negative | more_than_100
     NoMethodError:
       undefined method `|' for #<Filter:0xb8e09954 @filter=#<Proc:0xb8e09990>>
     # /tmp/d20141028-18133-1ns6qix/spec.rb:118: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)>'

  18) NumberSet can combine multiple filters with "and" and "or" rules
     Failure/Error: filter        = even & negative | mod_3_is_zero
     NoMethodError:
       undefined method `&' for #<Filter:0xb8e08dec @filter=#<Proc:0xb8e09990>>
     # /tmp/d20141028-18133-1ns6qix/spec.rb:128: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)>'

  19) NumberSet can combine multiple filters with "and", "or" and parenthesis
     Failure/Error: filter        = even & (negative | mod_3_is_zero)
     NoMethodError:
       undefined method `|' for #<SignFilter:0xb8e082c0>
     # /tmp/d20141028-18133-1ns6qix/spec.rb:138: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)>'

  20) NumberSet is enumerable
     Failure/Error: expect(values.size).to eq [Rational(5, 2), 8, 7, 9].size
       
       expected: 4
            got: 0
       
       (compared using ==)
     # /tmp/d20141028-18133-1ns6qix/spec.rb:155: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.02476 seconds
24 examples, 20 failures

Failed examples:

rspec /tmp/d20141028-18133-1ns6qix/spec.rb:2 # NumberSet can store integers
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:6 # NumberSet can store floating point numbers
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:10 # NumberSet can store complex numbers
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:14 # NumberSet can store rational numbers
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:18 # NumberSet can compare numbers of different types
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:42 # NumberSet can filter by complex type
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:48 # NumberSet can filter by integer type
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:54 # NumberSet can filter by real type
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:60 # NumberSet can filter by custom filter
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:66 # NumberSet can filter positive numbers
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:72 # NumberSet can filter non-positive numbers
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:78 # NumberSet can filter negative numbers
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:84 # NumberSet can filter non-negative numbers
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:90 # NumberSet can combine two filters with "and" rule
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:97 # NumberSet can combine two filters with "or" rule
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:104 # NumberSet can combine multiple filters with "and" rule
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:114 # NumberSet can combine multiple filters with "or" rule
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:124 # NumberSet can combine multiple filters with "and" and "or" rules
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:134 # NumberSet can combine multiple filters with "and", "or" and parenthesis
rspec /tmp/d20141028-18133-1ns6qix/spec.rb:144 # NumberSet is enumerable

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

Божидар обнови решението на 27.10.2014 16:54 (преди около 10 години)

+require 'set'
+class NumberSet
+ include Enumerable
+
+ attr_reader :size
+
+ def initialize
+ @size = 0
+ @elements = Set.new
+ end
+
+ def each
+ @elements.each { |element| element}
+ end
+
+ def << number
+ contains_flag = false
+ @elements.each do |x|
+ if x.to_c == number.to_c
+ contains_flag = true
+ end
+ end
+ if empty? || contains_flag == false
+ @elements.add number
+ @size += 1
+ end
+ end
+
+ def empty?
+ size == 0
+ end
+
+ def [](filter)
+ result = NumberSet.new
+ @elements.each do |item|
+ result << item if filter.call(item)
+ end
+ result
+ end
+end
+
+class Filter
+ def initialize(&block)
+ @filter = block
+ end
+
+ def call(*arguments)
+ p *arguments
+ @filter.(*arguments)
+ end
+end
+
+
+class TypeFilter
+ def initialize(type)
+ case type
+ when :integer then @block = lambda { |n| n.is_a? Integer }
+ when :complex then @block = lambda { |n| n.is_a? Complex }
+ when :real then @block = lambda { |n| n.is_a? Float || Rational }
+ end
+ end
+
+ def call(*arguments)
+ @block.(*arguments)
+ end
+end
+
+class SignFilter
+ def initialize(sign_filter)
+ case sign_filter
+ when :positive then @block = lambda { |n| n > 0 }
+ when :non_positive then @block = lambda { |n| n <= 0 }
+ when :negative then @block = lambda { |n| n < 0 }
+ when :non_negative then @block = lambda { |n| n >= 0 }
+ end
+ end
+
+ def call(*arguments)
+ @block.(*arguments)
+ end
+end