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

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

Към профила на Дамян Димитров

Резултати

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

Код

class NumberSet
include Enumerable
def initialize
@numbers = []
end
def each(&block)
@numbers.each(&block)
end
def <<(other)
@numbers << other unless include?(other)
end
def size
@numbers.size
end
def empty?
if count == 0
true
else
false
end
end
def [](filter)
each_with_object(NumberSet.new) do |number, filter_result|
filter_result << number if filter.met_by? number
end
end
end
module FilterOperations
def met_by?(number)
@filter.call(number)
end
def &(other)
Filter.new { |number| met_by?(number) and other.met_by?(number) }
end
def |(other)
Filter.new { |number| met_by?(number) or other.met_by?(number) }
end
end
class Filter
include FilterOperations
def initialize(&filter)
@filter = filter
end
end
class SignFilter
include FilterOperations
def initialize(filter)
if filter == :positive
@filter = Proc.new { |number| number > 0}
elsif filter == :non_positive
@filter = Proc.new { |number| number <= 0}
elsif filter == :negative
@filter = Proc.new { |number| number < 0}
elsif filter == :non_negative
@filter = Proc.new { |number| number >= 0}
end
end
end
class TypeFilter
include FilterOperations
def initialize(filter)
if filter == :integer
@filter = Proc.new { |number| number.is_a? Integer }
elsif filter == :real
@filter = Proc.new do |number|
(number.is_a? Float) or (number.is_a? Rational)
end
elsif filter == :complex
@filter = Proc.new { |number| number.is_a? Complex }
end
end
end

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

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

Finished in 0.03157 seconds
24 examples, 0 failures

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

Дамян обнови решението на 26.10.2014 13:19 (преди около 10 години)

+class NumberSet
+ include Enumerable
+
+ def initialize
+ @numbers = []
+ end
+
+ def each(&block)
+ @numbers.each(&block)
+ end
+
+ def <<(other)
+ @numbers << other if all? { |number| number != other }
+ end
+
+ def size
+ count
+ end
+
+ def empty?
+ count == 0 ? true : false
+ end
+
+ def [](filter)
+ filter_result = NumberSet.new
+ @numbers.each { |number| filter_result << number if filter.met_by? number }
+ filter_result
+ end
+end
+
+class Filter
+ def initialize(&filter)
+ @filter = filter
+ end
+
+ def met_by?(number)
+ @filter.call(number)
+ end
+
+ def &(other)
+ Filter.new { |number| met_by?(number) and other.met_by?(number) }
+ end
+
+ def |(other)
+ Filter.new { |number| met_by?(number) or other.met_by?(number) }
+ end
+end
+
+class SignFilter
+ def initialize(filter)
+ if filter == :positive
+ @filter = Proc.new { |number| number > 0}
+ elsif filter == :non_positive
+ @filter = Proc.new { |number| number <= 0}
+ elsif filter == :negative
+ @filter = Proc.new { |number| number < 0}
+ elsif filter == :non_negative
+ @filter = Proc.new { |number| number >= 0}
+ end
+ end
+
+ def met_by?(number)
+ @filter.call(number)
+ end
+
+ def &(other)
+ Filter.new { |number| met_by?(number) and other.met_by?(number) }
+ end
+
+ def |(other)
+ Filter.new { |number| met_by?(number) or other.met_by?(number) }
+ end
+end
+
+class TypeFilter
+ def initialize(filter)
+ if filter == :integer
+ @filter = Proc.new { |number| number.is_a? Integer }
+ elsif filter == :real
+ @filter = Proc.new do |number|
+ (number.is_a? Float) or (number.is_a? Rational)
+ end
+ elsif filter == :complex
+ @filter = Proc.new { |number| number.is_a? Complex }
+ end
+ end
+
+ def met_by?(number)
+ @filter.call(number)
+ end
+
+ def &(other)
+ Filter.new { |number| met_by?(number) and other.met_by?(number) }
+ end
+
+ def |(other)
+ Filter.new { |number| met_by?(number) or other.met_by?(number) }
+ end
+end

Здрасти,

Имам няколко коментара по кода ти:

  • Имаш три метода, които си приличат адски много. Крещи за някакъв тип reuse. :))
  • Погледни Enumerable#each_with_object и помисли дали може да ти е полезно.
  • count == 0 ? true : false. По добър начин да напишеш това?
  • ... if all? { |number| number != other }. За това има по-кратък вариант. Помисли какво проверяваш и прегледай документацията на Array.
  • Прегледай методите Array#size и Array#count. В какво се различават? Кой би предпочел?

Дамян обнови решението на 27.10.2014 09:24 (преди около 10 години)

class NumberSet
include Enumerable
def initialize
@numbers = []
end
def each(&block)
@numbers.each(&block)
end
def <<(other)
- @numbers << other if all? { |number| number != other }
+ @numbers << other unless include?(other)
end
def size
- count
+ @numbers.size
end
def empty?
- count == 0 ? true : false
+ if count == 0
+ true
+ else
+ false
+ end
end
def [](filter)
- filter_result = NumberSet.new
- @numbers.each { |number| filter_result << number if filter.met_by? number }
- filter_result
+ each_with_object(NumberSet.new) do |number, filter_result|
+ filter_result << number if filter.met_by? number
+ end
end
end
-class Filter
- def initialize(&filter)
- @filter = filter
- end
-
+module FilterOperations
def met_by?(number)
@filter.call(number)
end
def &(other)
Filter.new { |number| met_by?(number) and other.met_by?(number) }
end
def |(other)
Filter.new { |number| met_by?(number) or other.met_by?(number) }
end
end
+class Filter
+ include FilterOperations
+
+ def initialize(&filter)
+ @filter = filter
+ end
+end
+
class SignFilter
+ include FilterOperations
+
def initialize(filter)
if filter == :positive
@filter = Proc.new { |number| number > 0}
elsif filter == :non_positive
@filter = Proc.new { |number| number <= 0}
elsif filter == :negative
@filter = Proc.new { |number| number < 0}
elsif filter == :non_negative
@filter = Proc.new { |number| number >= 0}
end
end
-
- def met_by?(number)
- @filter.call(number)
- end
-
- def &(other)
- Filter.new { |number| met_by?(number) and other.met_by?(number) }
- end
-
- def |(other)
- Filter.new { |number| met_by?(number) or other.met_by?(number) }
- end
end
class TypeFilter
+ include FilterOperations
+
def initialize(filter)
if filter == :integer
@filter = Proc.new { |number| number.is_a? Integer }
elsif filter == :real
@filter = Proc.new do |number|
(number.is_a? Float) or (number.is_a? Rational)
end
elsif filter == :complex
@filter = Proc.new { |number| number.is_a? Complex }
end
- end
-
- def met_by?(number)
- @filter.call(number)
- end
-
- def &(other)
- Filter.new { |number| met_by?(number) and other.met_by?(number) }
- end
-
- def |(other)
- Filter.new { |number| met_by?(number) or other.met_by?(number) }
end
end

Здравей :)

Благодаря за коментарите. Бяха наистина полезни.

За човек, който е учил ООП преди 10 години и оттогава не е писал никакъв обектно ориентиран код, си мислех, че трудно ще мога да направя решението по правилата. А всъщност трябваше само още веднъж да прегледам лекцията.

Надявам се, че сега съм се справил по-добре.

Дамян обнови решението на 27.10.2014 09:33 (преди около 10 години)

class NumberSet
include Enumerable
def initialize
@numbers = []
end
def each(&block)
@numbers.each(&block)
end
def <<(other)
@numbers << other unless include?(other)
end
def size
@numbers.size
end
def empty?
if count == 0
true
else
false
end
end
def [](filter)
each_with_object(NumberSet.new) do |number, filter_result|
filter_result << number if filter.met_by? number
end
end
end
module FilterOperations
+
def met_by?(number)
@filter.call(number)
end
def &(other)
Filter.new { |number| met_by?(number) and other.met_by?(number) }
end
def |(other)
Filter.new { |number| met_by?(number) or other.met_by?(number) }
end
end
class Filter
include FilterOperations
def initialize(&filter)
@filter = filter
end
end
class SignFilter
include FilterOperations
def initialize(filter)
if filter == :positive
@filter = Proc.new { |number| number > 0}
elsif filter == :non_positive
@filter = Proc.new { |number| number <= 0}
elsif filter == :negative
@filter = Proc.new { |number| number < 0}
elsif filter == :non_negative
@filter = Proc.new { |number| number >= 0}
end
end
end
class TypeFilter
include FilterOperations
def initialize(filter)
if filter == :integer
@filter = Proc.new { |number| number.is_a? Integer }
elsif filter == :real
@filter = Proc.new do |number|
(number.is_a? Float) or (number.is_a? Rational)
end
elsif filter == :complex
@filter = Proc.new { |number| number.is_a? Complex }
end
end
end