Решение на Втора задача от Снежана Спасова

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

Към профила на Снежана Спасова

Резултати

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

Код

class NumberSet
include Enumerable
def initialize(container = [])
@container = container
end
def <<(number)
@container << number unless @container.include? number
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](condition)
NumberSet.new @container.select { |number| condition.satisfied_by? number }
end
def each(&block)
@container.each(&block)
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def satisfied_by?(number)
@condition.call number
end
def &(other)
Filter.new { |number| satisfied_by? number and other.satisfied_by? number }
end
def |(other)
Filter.new { |number| satisfied_by? number or other.satisfied_by? number }
end
end
class TypeFilter < Filter
def initialize(condition)
@condition = condition
end
def satisfied_by?(number)
case @condition
when :integer then number.class == Fixnum or number.class == Bignum
when :real then number.class == Float or number.class == Rational
else number.class == Complex
end
end
end
class SignFilter < Filter
def initialize(condition)
@condition = condition
end
def satisfied_by?(number)
case @condition
when :positive then number > 0
when :non_positive then number <= 0
when :negative then number < 0
else number >= 0
end
end
end

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

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

Finished in 0.02191 seconds
24 examples, 0 failures

История (11 версии и 4 коментара)

Снежана обнови решението на 22.10.2014 16:31 (преди над 9 години)

+class NumberSet
+ include Enumerable
+
+ attr_reader :container
+
+ def initialize(container = [])
+ @container = container.uniq
+ end
+
+ def <<(number)
+ @container << number
+ end
+
+ def size
+ @container.map { |number| Float(number) rescue number }.uniq.size
+ end
+
+ def empty?
+ @container.empty?
+ end
+
+ def [](criteria)
+ NumberSet.new @container.select { |number| criteria.meets? number }
+ end
+
+ def each
+ @container.each { |number| yield number }
+ end
+end
+
+class Filter
+ def initialize(&condition)
+ @condition = condition
+ end
+
+ def meets?(number)
+ @condition.call number
+ end
+
+ def &(other)
+ Filter.new { |number| meets? number and other.meets? number }
+ end
+
+ def |(other)
+ Filter.new { |number| meets? number or other.meets? number }
+ end
+end
+
+class TypeFilter < Filter
+ attr_reader :condition
+
+ def initialize(condition)
+ @condition = condition
+ end
+
+ def meets?(number)
+ send @condition.to_s + "?", number
+ end
+
+ def integer?(number)
+ number.class == Fixnum or number.class == Bignum
+ end
+
+ def real?(number)
+ number.class == Float or number.class == Rational
+ end
+
+ def complex?(number)
+ number.class == Complex
+ end
+end
+
+class SignFilter < Filter
+ attr_reader :condition
+
+ def initialize(condition)
+ @condition = condition
+ end
+
+ def meets?(number)
+ send @condition.to_s + "?", number
+ end
+
+ def positive?(number)
+ number > 0
+ end
+
+ def non_positive?(number)
+ number <= 0
+ end
+
+ def negative?(number)
+ number < 0
+ end
+
+ def non_negative?(number)
+ number >= 0
+ end
+end

Снежана обнови решението на 22.10.2014 16:35 (преди над 9 години)

class NumberSet
include Enumerable
attr_reader :container
def initialize(container = [])
@container = container.uniq
end
def <<(number)
@container << number
end
def size
@container.map { |number| Float(number) rescue number }.uniq.size
end
def empty?
@container.empty?
end
def [](criteria)
NumberSet.new @container.select { |number| criteria.meets? number }
end
def each
@container.each { |number| yield number }
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def meets?(number)
@condition.call number
end
def &(other)
Filter.new { |number| meets? number and other.meets? number }
end
def |(other)
Filter.new { |number| meets? number or other.meets? number }
end
end
class TypeFilter < Filter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def meets?(number)
send @condition.to_s + "?", number
end
def integer?(number)
number.class == Fixnum or number.class == Bignum
end
def real?(number)
number.class == Float or number.class == Rational
end
def complex?(number)
number.class == Complex
end
end
-class SignFilter < Filter
+class SignFilter < TypeFilter
attr_reader :condition
def initialize(condition)
@condition = condition
- end
-
- def meets?(number)
- send @condition.to_s + "?", number
end
def positive?(number)
number > 0
end
def non_positive?(number)
number <= 0
end
def negative?(number)
number < 0
end
def non_negative?(number)
number >= 0
end
end

Снежана обнови решението на 22.10.2014 18:26 (преди над 9 години)

class NumberSet
include Enumerable
attr_reader :container
def initialize(container = [])
@container = container.uniq
end
def <<(number)
- @container << number
+ @container.all? { |element| Float(element) != number rescue
+ element != number } ? @container << number : @container
end
def size
- @container.map { |number| Float(number) rescue number }.uniq.size
+ @container.size
end
def empty?
@container.empty?
end
def [](criteria)
NumberSet.new @container.select { |number| criteria.meets? number }
end
def each
@container.each { |number| yield number }
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def meets?(number)
@condition.call number
end
def &(other)
Filter.new { |number| meets? number and other.meets? number }
end
def |(other)
Filter.new { |number| meets? number or other.meets? number }
end
end
class TypeFilter < Filter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def meets?(number)
send @condition.to_s + "?", number
end
def integer?(number)
number.class == Fixnum or number.class == Bignum
end
def real?(number)
number.class == Float or number.class == Rational
end
def complex?(number)
number.class == Complex
end
end
class SignFilter < TypeFilter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def positive?(number)
number > 0
end
def non_positive?(number)
number <= 0
end
def negative?(number)
number < 0
end
def non_negative?(number)
number >= 0
end
end

Снежана обнови решението на 22.10.2014 18:27 (преди над 9 години)

class NumberSet
include Enumerable
attr_reader :container
def initialize(container = [])
@container = container.uniq
end
def <<(number)
- @container.all? { |element| Float(element) != number rescue
+ @container.all? { |element| Float(element) != Float(number) rescue
element != number } ? @container << number : @container
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](criteria)
NumberSet.new @container.select { |number| criteria.meets? number }
end
def each
@container.each { |number| yield number }
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def meets?(number)
@condition.call number
end
def &(other)
Filter.new { |number| meets? number and other.meets? number }
end
def |(other)
Filter.new { |number| meets? number or other.meets? number }
end
end
class TypeFilter < Filter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def meets?(number)
send @condition.to_s + "?", number
end
def integer?(number)
number.class == Fixnum or number.class == Bignum
end
def real?(number)
number.class == Float or number.class == Rational
end
def complex?(number)
number.class == Complex
end
end
class SignFilter < TypeFilter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def positive?(number)
number > 0
end
def non_positive?(number)
number <= 0
end
def negative?(number)
number < 0
end
def non_negative?(number)
number >= 0
end
end

Снежана обнови решението на 22.10.2014 18:28 (преди над 9 години)

class NumberSet
include Enumerable
attr_reader :container
def initialize(container = [])
- @container = container.uniq
+ @container = container
end
def <<(number)
@container.all? { |element| Float(element) != Float(number) rescue
element != number } ? @container << number : @container
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](criteria)
NumberSet.new @container.select { |number| criteria.meets? number }
end
def each
@container.each { |number| yield number }
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def meets?(number)
@condition.call number
end
def &(other)
Filter.new { |number| meets? number and other.meets? number }
end
def |(other)
Filter.new { |number| meets? number or other.meets? number }
end
end
class TypeFilter < Filter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def meets?(number)
send @condition.to_s + "?", number
end
def integer?(number)
number.class == Fixnum or number.class == Bignum
end
def real?(number)
number.class == Float or number.class == Rational
end
def complex?(number)
number.class == Complex
end
end
class SignFilter < TypeFilter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def positive?(number)
number > 0
end
def non_positive?(number)
number <= 0
end
def negative?(number)
number < 0
end
def non_negative?(number)
number >= 0
end
end

Здрасти,

Имам няколко забележки по решението ти:

  • Защо ти е reader-а за container? Според мен не ти трябва?
  • Защо се налага проверката за Float(element) != Float(number)? Пробвай Rational(22, 2) == 11 в irb.
  • Разгледай как се държи Array#each, ако не му подадем блок. Очакваме NumberSet#each да работи по същия начин.
  • send @condition.to_s + "?", number е твърде хитро... Изреди различните възможности в meets? метода. По-просто е.
  • Не намирам логика в това SignFilter да наследява TypeFilter. Ако е само за да reuse-неш "хитростта" със send - недей. :)

Добро е решението ти, помисли върху горните неща. :)

Снежана обнови решението на 22.10.2014 23:48 (преди над 9 години)

class NumberSet
include Enumerable
- attr_reader :container
-
def initialize(container = [])
@container = container
end
def <<(number)
- @container.all? { |element| Float(element) != Float(number) rescue
- element != number } ? @container << number : @container
+ @container << number if @container.all? { |element| element != number }
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](criteria)
NumberSet.new @container.select { |number| criteria.meets? number }
end
def each
- @container.each { |number| yield number }
+ block_given? ? @container.each{ |x| yield x } : @container.each
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def meets?(number)
@condition.call number
- end
+ end
def &(other)
Filter.new { |number| meets? number and other.meets? number }
end
def |(other)
Filter.new { |number| meets? number or other.meets? number }
end
end
class TypeFilter < Filter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def meets?(number)
- send @condition.to_s + "?", number
+ case @condition
+ when :integer
+ integer? number
+ when :real
+ real? number
+ else
+ complex? number
+ end
end
def integer?(number)
number.class == Fixnum or number.class == Bignum
end
def real?(number)
number.class == Float or number.class == Rational
end
def complex?(number)
number.class == Complex
end
end
-class SignFilter < TypeFilter
+class SignFilter < Filter
attr_reader :condition
def initialize(condition)
@condition = condition
+ end
+
+ def meets?(number)
+ case @condition
+ when :positive
+ positive? number
+ when :non_positive
+ non_positive? number
+ when :negative
+ negative? number
+ else
+ non_negative? number
+ end
end
def positive?(number)
number > 0
end
def non_positive?(number)
number <= 0
end
def negative?(number)
number < 0
end
def non_negative?(number)
number >= 0
end
end

Снежана обнови решението на 22.10.2014 23:49 (преди над 9 години)

class NumberSet
include Enumerable
def initialize(container = [])
@container = container
end
def <<(number)
@container << number if @container.all? { |element| element != number }
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](criteria)
NumberSet.new @container.select { |number| criteria.meets? number }
end
def each
block_given? ? @container.each{ |x| yield x } : @container.each
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def meets?(number)
@condition.call number
- end
+ end
def &(other)
Filter.new { |number| meets? number and other.meets? number }
end
def |(other)
Filter.new { |number| meets? number or other.meets? number }
end
end
class TypeFilter < Filter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def meets?(number)
case @condition
when :integer
integer? number
when :real
real? number
else
complex? number
end
end
def integer?(number)
number.class == Fixnum or number.class == Bignum
end
def real?(number)
number.class == Float or number.class == Rational
end
def complex?(number)
number.class == Complex
end
end
class SignFilter < Filter
attr_reader :condition
def initialize(condition)
@condition = condition
end
def meets?(number)
case @condition
when :positive
positive? number
when :non_positive
non_positive? number
when :negative
negative? number
else
non_negative? number
end
end
def positive?(number)
number > 0
end
def non_positive?(number)
number <= 0
end
def negative?(number)
number < 0
end
def non_negative?(number)
number >= 0
end
end
  • Помисли дали не можеш да запишеш if @container.all? { |element| element != number } по друг начин.
  • block_given? ? @container.each{ |x| yield x } : @container.each може да стане по по-прост начин. Пробвай да вземеш блока като параметър.
  • По мое мнение методите positive?, non_positive?, ..., real?, integer? са излишни. Пробвай да ги разпишеш направо в case-а.
  • attr_reader :condition?

Снежана обнови решението на 23.10.2014 00:36 (преди над 9 години)

class NumberSet
include Enumerable
def initialize(container = [])
@container = container
end
def <<(number)
- @container << number if @container.all? { |element| element != number }
+ @container << number if not @container.include? number
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](criteria)
NumberSet.new @container.select { |number| criteria.meets? number }
end
- def each
- block_given? ? @container.each{ |x| yield x } : @container.each
+ def each(&block)
+ @container.each(&block)
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def meets?(number)
@condition.call number
end
def &(other)
Filter.new { |number| meets? number and other.meets? number }
end
def |(other)
Filter.new { |number| meets? number or other.meets? number }
end
end
class TypeFilter < Filter
- attr_reader :condition
-
def initialize(condition)
@condition = condition
end
def meets?(number)
case @condition
when :integer
- integer? number
+ number.class == Fixnum or number.class == Bignum
when :real
- real? number
+ number.class == Float or number.class == Rational
else
- complex? number
+ number.class == Complex
end
end
-
- def integer?(number)
- number.class == Fixnum or number.class == Bignum
- end
-
- def real?(number)
- number.class == Float or number.class == Rational
- end
-
- def complex?(number)
- number.class == Complex
- end
end
class SignFilter < Filter
- attr_reader :condition
-
def initialize(condition)
@condition = condition
end
def meets?(number)
case @condition
when :positive
- positive? number
+ number > 0
when :non_positive
- non_positive? number
+ number <= 0
when :negative
- negative? number
+ number < 0
else
- non_negative? number
+ number >= 0
end
- end
-
- def positive?(number)
- number > 0
- end
-
- def non_positive?(number)
- number <= 0
- end
-
- def negative?(number)
- number < 0
- end
-
- def non_negative?(number)
- number >= 0
end
end

Снежана обнови решението на 23.10.2014 00:41 (преди над 9 години)

class NumberSet
include Enumerable
def initialize(container = [])
@container = container
end
def <<(number)
@container << number if not @container.include? number
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](criteria)
NumberSet.new @container.select { |number| criteria.meets? number }
end
def each(&block)
@container.each(&block)
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def meets?(number)
@condition.call number
end
def &(other)
Filter.new { |number| meets? number and other.meets? number }
end
def |(other)
Filter.new { |number| meets? number or other.meets? number }
end
end
class TypeFilter < Filter
def initialize(condition)
@condition = condition
end
def meets?(number)
case @condition
- when :integer
- number.class == Fixnum or number.class == Bignum
- when :real
- number.class == Float or number.class == Rational
- else
- number.class == Complex
+ when :integer then number.class == Fixnum or number.class == Bignum
+ when :real then number.class == Float or number.class == Rational
+ else number.class == Complex
end
end
end
class SignFilter < Filter
def initialize(condition)
@condition = condition
end
def meets?(number)
case @condition
- when :positive
- number > 0
- when :non_positive
- number <= 0
- when :negative
- number < 0
- else
- number >= 0
+ when :positive then number > 0
+ when :non_positive then number <= 0
+ when :negative then number < 0
+ else number >= 0
end
end
end

Супер.

@container << number if not @container.include? number можеш да го запишеш и като @container << number if not not not not not not not @container.include? number. Защо се противиш да ползваш unless? :D

Харесва ми, че си помислила за името на Filter#meets? дали няма по-добри варианти? Какво мислиш за satisfies?, satisfied_by?, accepts?? Намираш ли разлика между тях? Други предложения?

Снежана обнови решението на 23.10.2014 15:05 (преди над 9 години)

class NumberSet
include Enumerable
def initialize(container = [])
@container = container
end
def <<(number)
- @container << number if not @container.include? number
+ @container << number unless @container.include? number
end
def size
@container.size
end
def empty?
@container.empty?
end
- def [](criteria)
- NumberSet.new @container.select { |number| criteria.meets? number }
+ def [](condition)
+ NumberSet.new @container.select { |number| condition.satisfied_by? number }
end
def each(&block)
@container.each(&block)
end
end
class Filter
def initialize(&condition)
@condition = condition
end
- def meets?(number)
+ def satisfied_by?(number)
@condition.call number
end
def &(other)
- Filter.new { |number| meets? number and other.meets? number }
+ Filter.new { |number| satisfied_by? number and other.satisfied_by? number }
end
def |(other)
- Filter.new { |number| meets? number or other.meets? number }
+ Filter.new { |number| satisfied_by? number or other.satisfied_by? number }
end
end
class TypeFilter < Filter
def initialize(condition)
@condition = condition
end
- def meets?(number)
+ def satisfied_by?(number)
case @condition
when :integer then number.class == Fixnum or number.class == Bignum
when :real then number.class == Float or number.class == Rational
else number.class == Complex
end
end
end
class SignFilter < Filter
def initialize(condition)
@condition = condition
end
- def meets?(number)
+ def satisfied_by?(number)
case @condition
when :positive then number > 0
when :non_positive then number <= 0
when :negative then number < 0
else number >= 0
end
end
end

Снежана обнови решението на 23.10.2014 15:05 (преди над 9 години)

class NumberSet
include Enumerable
def initialize(container = [])
@container = container
end
def <<(number)
@container << number unless @container.include? number
end
def size
@container.size
end
def empty?
@container.empty?
end
def [](condition)
NumberSet.new @container.select { |number| condition.satisfied_by? number }
end
def each(&block)
@container.each(&block)
end
end
class Filter
def initialize(&condition)
@condition = condition
end
def satisfied_by?(number)
@condition.call number
end
def &(other)
- Filter.new { |number| satisfied_by? number and other.satisfied_by? number }
+ Filter.new { |number| satisfied_by? number and other.satisfied_by? number }
end
def |(other)
Filter.new { |number| satisfied_by? number or other.satisfied_by? number }
end
end
class TypeFilter < Filter
def initialize(condition)
@condition = condition
end
def satisfied_by?(number)
case @condition
when :integer then number.class == Fixnum or number.class == Bignum
when :real then number.class == Float or number.class == Rational
else number.class == Complex
end
end
end
class SignFilter < Filter
def initialize(condition)
@condition = condition
end
def satisfied_by?(number)
case @condition
when :positive then number > 0
when :non_positive then number <= 0
when :negative then number < 0
else number >= 0
end
end
end