Решение на Втора задача от Кристиан Цветков

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

Към профила на Кристиан Цветков

Резултати

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

Код

class NumberSet
include Enumerable
def initialize()
@numbers = []
end
def each
return @numbers.to_enum(:each) unless block_given?
@numbers.each { |n| yield n }
end
def <<(number)
unless @numbers.include? number
@numbers << number
end
end
def size
@numbers.size
end
def empty?
@numbers.empty?
end
def [](filter)
result = NumberSet.new
@numbers.select { |number| filter.accepts? number }.each do |n|
result << n
end
result
end
end
class Filter
def initialize(&block)
@block = block
end
def accepts?(number)
@block.(number)
end
def &(other)
Filter.new { |number| accepts? number and other.accepts? number }
end
def |(other)
Filter.new { |number| accepts? number or other.accepts? number }
end
end
class TypeFilter < Filter
def initialize(type)
case type
when :integer then super() { |number| number.integer? }
when :real then super() { |n| n.is_a? Float or n.is_a? Rational }
when :complex then super() { |number| number.is_a? Complex }
end
end
end
class SignFilter < Filter
def initialize(sign)
case sign
when :positive then super() { |number| number > 0 }
when :non_positive then super() { |number| number <= 0 }
when :negative then super() { |number| number < 0 }
when :non_negative then super() { |number| number >= 0 }
end
end
end

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

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

Finished in 0.02234 seconds
24 examples, 0 failures

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

Кристиан обнови решението на 26.10.2014 16:39 (преди около 10 години)

+class NumberSet
+ include Enumerable
+
+ def initialize()
+ @numbers = []
+ end
+
+ def each
+ @numbers.each { |n| yield n }
+ end
+
+ def <<(number)
+ unless contains? number
+ @numbers << number
+ end
+ end
+
+ def size
+ @numbers.size
+ end
+
+ def empty?
+ @numbers.empty?
+ end
+
+ def [](filter)
+ result = NumberSet.new
+ @numbers.select { |number| filter.accepts? number }.each do |n|
+ result << n
+ end
+ result
+ end
+
+ private
+
+ def contains?(number)
+ @numbers.each do |n|
+ return true if (n <=> number) == 0
+ end
+ false
+ end
+end
+
+class BaseFilter
+ def initialize(&block)
+ @block = block
+ end
+
+ def accepts?(number)
+ @block.(number)
+ end
+
+ def &(other_filter)
+ BaseFilter.new { |number| accepts? number and other_filter.accepts? number }
+ end
+
+ def |(other_filter)
+ BaseFilter.new { |number| accepts? number or other_filter.accepts? number }
+ end
+end
+
+class Filter < BaseFilter
+ def initialize(&block)
+ super(&block)
+ end
+end
+
+class TypeFilter < BaseFilter
+ def initialize(type)
+ case type
+ when :integer then super() { |number| number.integer? }
+ when :real then super() { |n| n.is_a? Float or n.is_a? Rational }
+ when :complex then super() { |number| not number.real? }
+ end
+ end
+end
+
+class SignFilter < BaseFilter
+ def initialize(sign)
+ case sign
+ when :positive then super() { |number| number > 0 }
+ when :non_positive then super() { |number| number <= 0 }
+ when :negative then super() { |number| number < 0 }
+ when :non_negative then super() { |number| number >= 0 }
+ end
+ end
+end

Кристиан обнови решението на 26.10.2014 17:22 (преди около 10 години)

class NumberSet
include Enumerable
def initialize()
@numbers = []
end
def each
@numbers.each { |n| yield n }
end
def <<(number)
unless contains? number
@numbers << number
end
end
def size
@numbers.size
end
def empty?
@numbers.empty?
end
def [](filter)
result = NumberSet.new
@numbers.select { |number| filter.accepts? number }.each do |n|
result << n
end
result
end
private
def contains?(number)
@numbers.each do |n|
- return true if (n <=> number) == 0
+ return true if n.to_c == number.to_c
end
false
end
end
class BaseFilter
def initialize(&block)
@block = block
end
def accepts?(number)
@block.(number)
end
def &(other_filter)
BaseFilter.new { |number| accepts? number and other_filter.accepts? number }
end
def |(other_filter)
BaseFilter.new { |number| accepts? number or other_filter.accepts? number }
end
end
class Filter < BaseFilter
def initialize(&block)
super(&block)
end
end
class TypeFilter < BaseFilter
def initialize(type)
case type
when :integer then super() { |number| number.integer? }
when :real then super() { |n| n.is_a? Float or n.is_a? Rational }
when :complex then super() { |number| not number.real? }
end
end
end
class SignFilter < BaseFilter
def initialize(sign)
case sign
when :positive then super() { |number| number > 0 }
when :non_positive then super() { |number| number <= 0 }
when :negative then super() { |number| number < 0 }
when :non_negative then super() { |number| number >= 0 }
end
end
end

Кристиан обнови решението на 26.10.2014 18:10 (преди около 10 години)

class NumberSet
include Enumerable
def initialize()
@numbers = []
end
def each
@numbers.each { |n| yield n }
end
def <<(number)
unless contains? number
@numbers << number
end
end
def size
@numbers.size
end
def empty?
@numbers.empty?
end
def [](filter)
result = NumberSet.new
@numbers.select { |number| filter.accepts? number }.each do |n|
result << n
end
result
end
private
def contains?(number)
@numbers.each do |n|
return true if n.to_c == number.to_c
end
false
end
end
class BaseFilter
def initialize(&block)
@block = block
end
def accepts?(number)
@block.(number)
end
def &(other_filter)
BaseFilter.new { |number| accepts? number and other_filter.accepts? number }
end
def |(other_filter)
BaseFilter.new { |number| accepts? number or other_filter.accepts? number }
end
end
class Filter < BaseFilter
def initialize(&block)
super(&block)
end
end
class TypeFilter < BaseFilter
def initialize(type)
case type
when :integer then super() { |number| number.integer? }
when :real then super() { |n| n.is_a? Float or n.is_a? Rational }
- when :complex then super() { |number| not number.real? }
+ when :complex then super() { |number| number.is_a? Complex }
end
end
end
class SignFilter < BaseFilter
def initialize(sign)
case sign
when :positive then super() { |number| number > 0 }
when :non_positive then super() { |number| number <= 0 }
when :negative then super() { |number| number < 0 }
when :non_negative then super() { |number| number >= 0 }
end
end
-end
+end

Кристиан обнови решението на 26.10.2014 20:58 (преди около 10 години)

class NumberSet
include Enumerable
def initialize()
@numbers = []
end
def each
@numbers.each { |n| yield n }
end
def <<(number)
unless contains? number
@numbers << number
end
end
def size
@numbers.size
end
def empty?
@numbers.empty?
end
def [](filter)
result = NumberSet.new
@numbers.select { |number| filter.accepts? number }.each do |n|
result << n
end
result
end
private
def contains?(number)
@numbers.each do |n|
return true if n.to_c == number.to_c
end
false
end
end
class BaseFilter
def initialize(&block)
@block = block
end
def accepts?(number)
@block.(number)
end
def &(other_filter)
BaseFilter.new { |number| accepts? number and other_filter.accepts? number }
end
def |(other_filter)
BaseFilter.new { |number| accepts? number or other_filter.accepts? number }
end
end
class Filter < BaseFilter
def initialize(&block)
- super(&block)
+ super
end
end
class TypeFilter < BaseFilter
def initialize(type)
case type
when :integer then super() { |number| number.integer? }
when :real then super() { |n| n.is_a? Float or n.is_a? Rational }
when :complex then super() { |number| number.is_a? Complex }
end
end
end
class SignFilter < BaseFilter
def initialize(sign)
case sign
when :positive then super() { |number| number > 0 }
when :non_positive then super() { |number| number <= 0 }
when :negative then super() { |number| number < 0 }
when :non_negative then super() { |number| number >= 0 }
end
end
end

Кристиан обнови решението на 26.10.2014 21:02 (преди около 10 години)

class NumberSet
include Enumerable
def initialize()
@numbers = []
end
def each
@numbers.each { |n| yield n }
end
def <<(number)
unless contains? number
@numbers << number
end
end
def size
@numbers.size
end
def empty?
@numbers.empty?
end
def [](filter)
result = NumberSet.new
@numbers.select { |number| filter.accepts? number }.each do |n|
result << n
end
result
end
private
def contains?(number)
@numbers.each do |n|
return true if n.to_c == number.to_c
end
false
end
end
-class BaseFilter
+class Filter
def initialize(&block)
@block = block
end
def accepts?(number)
@block.(number)
end
def &(other_filter)
- BaseFilter.new { |number| accepts? number and other_filter.accepts? number }
+ Filter.new { |number| accepts? number and other_filter.accepts? number }
end
def |(other_filter)
- BaseFilter.new { |number| accepts? number or other_filter.accepts? number }
+ Filter.new { |number| accepts? number or other_filter.accepts? number }
end
end
-class Filter < BaseFilter
- def initialize(&block)
- super
- end
-end
-
-class TypeFilter < BaseFilter
+class TypeFilter < Filter
def initialize(type)
case type
when :integer then super() { |number| number.integer? }
when :real then super() { |n| n.is_a? Float or n.is_a? Rational }
when :complex then super() { |number| number.is_a? Complex }
end
end
end
-class SignFilter < BaseFilter
+class SignFilter < Filter
def initialize(sign)
case sign
when :positive then super() { |number| number > 0 }
when :non_positive then super() { |number| number <= 0 }
when :negative then super() { |number| number < 0 }
when :non_negative then super() { |number| number >= 0 }
end
end
end

Харесва ми решението ти. Няколко неща, които мога да ти предложа:

  • Очакваме NumberSet#each да работи като Array#each, когато не му подадем блок.
  • Врътката с to_c в contains? метода според мен е излишна. Методът като цяло може да бъде премахнат / сменен с друг.
  • Погледни Enumerable#each_with_object. Може да ти е полезен.
  • Погледни какво казва style guide-а за наименоването при дефиниране на бинарни оператори.

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

class NumberSet
include Enumerable
def initialize()
@numbers = []
end
def each
+ return to_enum(:each) unless block_given?
@numbers.each { |n| yield n }
end
def <<(number)
- unless contains? number
+ unless @numbers.include? number
@numbers << number
end
end
def size
@numbers.size
end
def empty?
@numbers.empty?
end
def [](filter)
result = NumberSet.new
@numbers.select { |number| filter.accepts? number }.each do |n|
result << n
end
result
end
-
- private
-
- def contains?(number)
- @numbers.each do |n|
- return true if n.to_c == number.to_c
- end
- false
- end
end
class Filter
def initialize(&block)
@block = block
end
def accepts?(number)
@block.(number)
end
- def &(other_filter)
- Filter.new { |number| accepts? number and other_filter.accepts? number }
+ def &(other)
+ Filter.new { |number| accepts? number and other.accepts? number }
end
- def |(other_filter)
- Filter.new { |number| accepts? number or other_filter.accepts? number }
+ def |(other)
+ Filter.new { |number| accepts? number or other.accepts? number }
end
end
class TypeFilter < Filter
def initialize(type)
case type
when :integer then super() { |number| number.integer? }
when :real then super() { |n| n.is_a? Float or n.is_a? Rational }
when :complex then super() { |number| number.is_a? Complex }
end
end
end
class SignFilter < Filter
def initialize(sign)
case sign
when :positive then super() { |number| number > 0 }
when :non_positive then super() { |number| number <= 0 }
when :negative then super() { |number| number < 0 }
when :non_negative then super() { |number| number >= 0 }
end
end
end

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

class NumberSet
include Enumerable
def initialize()
@numbers = []
end
def each
- return to_enum(:each) unless block_given?
+ return @numbers.to_enum(:each) unless block_given?
@numbers.each { |n| yield n }
end
def <<(number)
unless @numbers.include? number
@numbers << number
end
end
def size
@numbers.size
end
def empty?
@numbers.empty?
end
def [](filter)
result = NumberSet.new
@numbers.select { |number| filter.accepts? number }.each do |n|
result << n
end
result
end
end
class Filter
def initialize(&block)
@block = block
end
def accepts?(number)
@block.(number)
end
def &(other)
Filter.new { |number| accepts? number and other.accepts? number }
end
def |(other)
Filter.new { |number| accepts? number or other.accepts? number }
end
end
class TypeFilter < Filter
def initialize(type)
case type
when :integer then super() { |number| number.integer? }
when :real then super() { |n| n.is_a? Float or n.is_a? Rational }
when :complex then super() { |number| number.is_a? Complex }
end
end
end
class SignFilter < Filter
def initialize(sign)
case sign
when :positive then super() { |number| number > 0 }
when :non_positive then super() { |number| number <= 0 }
when :negative then super() { |number| number < 0 }
when :non_negative then super() { |number| number >= 0 }
end
end
end