Решение на Втора задача от Методи Димитров

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

Към профила на Методи Димитров

Резултати

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

Код

class NumberSet
include Enumerable
def initialize
@unique_numbers = []
end
def each
@unique_numbers.each { |number| yield number }
end
def <<(number)
@unique_numbers << number if @unique_numbers.all? do |member|
member != number
end
end
def size
@unique_numbers.size
end
def empty?
@unique_numbers.empty?
end
def [](filter)
select &filter.criteria
end
end
class Filter
attr_reader :criteria
def initialize(&block)
@criteria = block
end
def &(new_filter)
Filter.new do |element|
criteria.(element) and new_filter.criteria.(element)
end
end
def |(new_filter)
Filter.new do |element|
criteria.(element) or new_filter.criteria.(element)
end
end
end
class TypeFilter < Filter
def initialize(type)
@@member_types = { integer: [Fixnum, Bignum],
real: [Float, Rational],
complex: [Complex], }
@criteria = ->(member) { @@member_types[type].include? member.class }
end
end
class SignFilter < Filter
def initialize(sign)
@@member_signs = { positive: '>',
non_positive: '<=',
negative: '<',
non_negative: '>=', }
@criteria = ->(number) { number.send @@member_signs[sign], 0 }
end
end

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

.......................F

Failures:

  1) 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-162tax6/solution.rb:9:in `block in each'
     # /tmp/d20141028-18133-162tax6/solution.rb:9:in `each'
     # /tmp/d20141028-18133-162tax6/solution.rb:9:in `each'
     # /tmp/d20141028-18133-162tax6/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.02289 seconds
24 examples, 1 failure

Failed examples:

rspec /tmp/d20141028-18133-162tax6/spec.rb:159 # NumberSet returns enumerable of set's contents if no block is given to each

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

Методи обнови решението на 24.10.2014 11:59 (преди около 10 години)

+class NumberSet
+ include Enumerable
+
+ def initialize
+ @unique_numbers = []
+ end
+
+ def each
+ @unique_numbers.each {|number| yield number}
+ end
+
+ def <<(new_number)
+ @unique_numbers << new_number if @unique_numbers.all? do |number|
+ number != new_number
+ end
+ end
+
+ def size
+ @unique_numbers.size
+ end
+
+ def empty?
+ @unique_numbers.empty?
+ end
+
+ def [](filter)
+ select { |element| element if filter.criteria.(element)}
+ end
+
+end
+
+class Filter
+
+ attr_reader :criteria
+
+ def initialize(&block)
+ @criteria = block
+ end
+
+ def &(new_filter)
+ Filter.new do |element|
+ criteria.(element) and new_filter.criteria.(element)
+ end
+ end
+
+ def |(new_filter)
+ Filter.new do |element|
+ criteria.(element) or new_filter.criteria.(element)
+ end
+ end
+end
+
+class TypeFilter < Filter
+ def initialize(member_type)
+ case member_type
+ when :integer then @criteria = ->(member) { member.is_a? Integer }
+ when :real then @criteria = ->(member) { member.is_a? Float }
+ when :complex then @criteria = ->(member) { member.is_a? Rational }
+ end
+ end
+end
+
+class SignFilter < Filter
+ def initialize(sign)
+ case sign
+ when :positive then @criteria = ->(number) { number > 0 }
+ when :non_positive then @criteria = ->(number) { number <= 0 }
+ when :negative then @criteria = ->(number) { number < 0 }
+ when :non_negative then @criteria = ->(number) { number >= 0 }
+ end
+ end
+end

Методи обнови решението на 24.10.2014 12:01 (преди около 10 години)

class NumberSet
include Enumerable
def initialize
@unique_numbers = []
end
def each
- @unique_numbers.each {|number| yield number}
+ @unique_numbers.each { |number| yield number }
end
def <<(new_number)
@unique_numbers << new_number if @unique_numbers.all? do |number|
number != new_number
end
end
def size
@unique_numbers.size
end
def empty?
@unique_numbers.empty?
end
def [](filter)
- select { |element| element if filter.criteria.(element)}
+ select { |element| element if filter.criteria.(element) }
end
end
class Filter
attr_reader :criteria
def initialize(&block)
@criteria = block
end
def &(new_filter)
Filter.new do |element|
criteria.(element) and new_filter.criteria.(element)
end
end
def |(new_filter)
Filter.new do |element|
criteria.(element) or new_filter.criteria.(element)
end
end
end
class TypeFilter < Filter
def initialize(member_type)
case member_type
when :integer then @criteria = ->(member) { member.is_a? Integer }
when :real then @criteria = ->(member) { member.is_a? Float }
when :complex then @criteria = ->(member) { member.is_a? Rational }
end
end
end
class SignFilter < Filter
def initialize(sign)
case sign
when :positive then @criteria = ->(number) { number > 0 }
when :non_positive then @criteria = ->(number) { number <= 0 }
when :negative then @criteria = ->(number) { number < 0 }
when :non_negative then @criteria = ->(number) { number >= 0 }
end
end
end

Методи обнови решението на 24.10.2014 21:29 (преди около 10 години)

class NumberSet
include Enumerable
def initialize
@unique_numbers = []
end
def each
@unique_numbers.each { |number| yield number }
end
def <<(new_number)
@unique_numbers << new_number if @unique_numbers.all? do |number|
number != new_number
end
end
def size
@unique_numbers.size
end
def empty?
@unique_numbers.empty?
end
def [](filter)
- select { |element| element if filter.criteria.(element) }
+ select &filter.criteria
end
end
class Filter
attr_reader :criteria
def initialize(&block)
@criteria = block
end
def &(new_filter)
Filter.new do |element|
criteria.(element) and new_filter.criteria.(element)
end
end
def |(new_filter)
Filter.new do |element|
criteria.(element) or new_filter.criteria.(element)
end
end
end
class TypeFilter < Filter
def initialize(member_type)
case member_type
when :integer then @criteria = ->(member) { member.is_a? Integer }
when :real then @criteria = ->(member) { member.is_a? Float }
when :complex then @criteria = ->(member) { member.is_a? Rational }
end
end
end
class SignFilter < Filter
def initialize(sign)
case sign
when :positive then @criteria = ->(number) { number > 0 }
when :non_positive then @criteria = ->(number) { number <= 0 }
when :negative then @criteria = ->(number) { number < 0 }
when :non_negative then @criteria = ->(number) { number >= 0 }
end
end
end

Методи обнови решението на 27.10.2014 10:36 (преди около 10 години)

class NumberSet
include Enumerable
def initialize
@unique_numbers = []
end
def each
@unique_numbers.each { |number| yield number }
end
- def <<(new_number)
- @unique_numbers << new_number if @unique_numbers.all? do |number|
- number != new_number
+ def <<(number)
+ @unique_numbers << number if @unique_numbers.all? do |member|
+ member != number
end
end
def size
@unique_numbers.size
end
def empty?
@unique_numbers.empty?
end
def [](filter)
select &filter.criteria
end
-
end
class Filter
-
attr_reader :criteria
def initialize(&block)
@criteria = block
end
def &(new_filter)
Filter.new do |element|
criteria.(element) and new_filter.criteria.(element)
end
end
def |(new_filter)
Filter.new do |element|
criteria.(element) or new_filter.criteria.(element)
end
end
end
class TypeFilter < Filter
- def initialize(member_type)
- case member_type
- when :integer then @criteria = ->(member) { member.is_a? Integer }
- when :real then @criteria = ->(member) { member.is_a? Float }
- when :complex then @criteria = ->(member) { member.is_a? Rational }
- end
+ def initialize(type)
+ @@member_types = { integer: [Integer],
+ real: [Float, Rational],
+ complex: [Complex], }
+
+ @criteria = ->(member) { @@member_types[type].include? member.class }
end
end
class SignFilter < Filter
def initialize(sign)
- case sign
- when :positive then @criteria = ->(number) { number > 0 }
- when :non_positive then @criteria = ->(number) { number <= 0 }
- when :negative then @criteria = ->(number) { number < 0 }
- when :non_negative then @criteria = ->(number) { number >= 0 }
- end
+ @@member_signs = { positive: '>',
+ non_positive: '<=',
+ negative: '<',
+ non_negative: '>=', }
+
+ @criteria = ->(number) { number.send @@member_signs[sign], 0 }
end
end

Методи обнови решението на 27.10.2014 10:42 (преди около 10 години)

class NumberSet
include Enumerable
def initialize
@unique_numbers = []
end
def each
@unique_numbers.each { |number| yield number }
end
def <<(number)
@unique_numbers << number if @unique_numbers.all? do |member|
member != number
end
end
def size
@unique_numbers.size
end
def empty?
@unique_numbers.empty?
end
def [](filter)
select &filter.criteria
end
end
class Filter
attr_reader :criteria
def initialize(&block)
@criteria = block
end
def &(new_filter)
Filter.new do |element|
criteria.(element) and new_filter.criteria.(element)
end
end
def |(new_filter)
Filter.new do |element|
criteria.(element) or new_filter.criteria.(element)
end
end
end
class TypeFilter < Filter
def initialize(type)
- @@member_types = { integer: [Integer],
+ @@member_types = { integer: [Fixnum, Bignum],
real: [Float, Rational],
complex: [Complex], }
@criteria = ->(member) { @@member_types[type].include? member.class }
end
end
class SignFilter < Filter
def initialize(sign)
@@member_signs = { positive: '>',
non_positive: '<=',
negative: '<',
non_negative: '>=', }
@criteria = ->(number) { number.send @@member_signs[sign], 0 }
end
end