Решение на Втора задача от Светослав Кръстев

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

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

Резултати

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

Код

class NumberSet
include Enumerable
def initialize
@array = []
end
def each
if (block_given?)
@array.each do |current|
yield current
end
return @array
else
to_enum(:each)
end
end
def <<(input_number)
unless @array.include?(input_number)
@array << input_number
end
end
def size
@array.size
end
def empty?
@array.empty?
end
def [](condition)
@array.each_with_object([]) { |i , a| a << i if condition.filter.call(i) }
end
end
class Filter
attr_reader :filter
def initialize(&block)
@filter = block
end
def to_proc
->(object, *args) { object.public_send self, *args}
end
def &(other)
Filter.new { |x| @filter.call(x) and other.filter.call(x) }
end
def |(other)
Filter.new { |x| @filter.call(x) or other.filter.call(x) }
end
end
class TypeFilter < Filter
def initialize(filter_key)
@filter =
case filter_key
when :integer then lambda { |number| number.integer? }
when :real then lambda { |number| number.real? and (not number.integer?) }
else lambda { |number| number.eql?(number.to_c) }
end
end
end
class SignFilter < Filter
def initialize(filter_key)
@filter =
case filter_key
when :positive then lambda { |number| number > 0 }
when :non_positive then lambda { |number| number <= 0 }
when :negative then lambda { |number| number < 0 }
else lambda { |number| number >= 0}
end
end
end

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

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

Finished in 0.02226 seconds
24 examples, 0 failures

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

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

+class NumberSet
+ include Enumerable
+
+ def initialize()
+ @array = Array.new
+ end
+
+ def each
+ @array.each do |current|
+ yield current
+ end
+ end
+
+ def <<(input_number)
+ if not @array.include?(input_number)
+ @array << input_number
+ end
+ end
+
+ def size
+ @array.size
+ end
+
+ def empty?
+ @array.empty?
+ end
+
+ def [](filter)
+ result = filter.filter_array(@array)
+
+ filtered_set = NumberSet.new
+ result.each do |current|
+ filtered_set << current
+ end
+
+ return filtered_set
+ end
+
+ def print_set
+ puts @array
+ end
+
+end
+
+module CustomFilterOperators
+
+ def &(other)
+ Filter.new { |x| get_filter.call(x) && other.get_filter.call(x) }
+ end
+
+ def |(other)
+ Filter.new { |x| get_filter.call(x) || other.get_filter.call(x) }
+ end
+
+end
+
+
+class Filter
+
+ include CustomFilterOperators
+
+ def initialize(&block)
+ @filter = block
+ end
+
+ def filter_array(numbers)
+ @new_arr = Array.new
+ numbers.each do |current|
+ if @filter.call(current)
+ @new_arr << current
+ end
+ end
+
+ return @new_arr
+ end
+
+ def get_filter
+ @filter
+ end
+
+end
+
+class TypeFilter
+
+ include CustomFilterOperators
+
+
+ def initialize(index)
+ is_integer = lambda { |number| number.integer? }
+ is_real = lambda { |number| number.real? }
+ is_complex = lambda { |number| number.eql?(number.to_c) }
+
+ @hash = { :integer => is_integer ,
+ :real => is_real ,
+ :complex => is_complex }
+ @hash_index = index
+ end
+
+ def filter_array(numbers)
+ @new_arr = Array.new
+ numbers.each do |current|
+ if @hash[@hash_index].call(current)
+ @new_arr << current
+ end
+ end
+
+ return @new_arr
+ end
+
+ def get_filter
+ @hash[@hash_index]
+ end
+
+end
+
+class SignFilter
+
+ include CustomFilterOperators
+
+ def initialize(index)
+ is_positive = lambda { |number| number > 0 }
+ is_non_positive = lambda { |number| number <= 0 }
+ is_negative = lambda { |number| number < 0 }
+ is_non_negative = lambda { |number| number >= 0}
+
+ @hash = { :positive => is_positive , :non_positive => is_non_positive ,
+ :negative => is_negative , :non_negative => is_non_negative }
+ @hash_index = index
+ end
+
+ def filter_array(numbers)
+ @new_arr = Array.new
+ numbers.each do |current|
+ if @hash[@hash_index].call(current)
+ @new_arr << current
+ end
+ end
+
+ return @new_arr
+ end
+
+ def get_filter
+ @hash[@hash_index]
+ end
+
+end

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

class NumberSet
include Enumerable
def initialize()
@array = Array.new
end
def each
@array.each do |current|
yield current
end
end
def <<(input_number)
if not @array.include?(input_number)
@array << input_number
end
end
def size
@array.size
end
def empty?
@array.empty?
end
def [](filter)
result = filter.filter_array(@array)
filtered_set = NumberSet.new
result.each do |current|
filtered_set << current
end
return filtered_set
end
- def print_set
- puts @array
- end
-
end
+
module CustomFilterOperators
def &(other)
Filter.new { |x| get_filter.call(x) && other.get_filter.call(x) }
end
def |(other)
Filter.new { |x| get_filter.call(x) || other.get_filter.call(x) }
end
-
end
class Filter
-
include CustomFilterOperators
def initialize(&block)
@filter = block
end
def filter_array(numbers)
@new_arr = Array.new
numbers.each do |current|
if @filter.call(current)
@new_arr << current
end
end
return @new_arr
end
def get_filter
@filter
end
-
end
-class TypeFilter
+class TypeFilter
include CustomFilterOperators
-
def initialize(index)
is_integer = lambda { |number| number.integer? }
is_real = lambda { |number| number.real? }
is_complex = lambda { |number| number.eql?(number.to_c) }
@hash = { :integer => is_integer ,
:real => is_real ,
:complex => is_complex }
@hash_index = index
end
def filter_array(numbers)
@new_arr = Array.new
numbers.each do |current|
if @hash[@hash_index].call(current)
@new_arr << current
end
end
return @new_arr
end
def get_filter
@hash[@hash_index]
end
-
end
-class SignFilter
+class SignFilter
include CustomFilterOperators
def initialize(index)
is_positive = lambda { |number| number > 0 }
is_non_positive = lambda { |number| number <= 0 }
is_negative = lambda { |number| number < 0 }
is_non_negative = lambda { |number| number >= 0}
@hash = { :positive => is_positive , :non_positive => is_non_positive ,
:negative => is_negative , :non_negative => is_non_negative }
@hash_index = index
end
def filter_array(numbers)
@new_arr = Array.new
numbers.each do |current|
if @hash[@hash_index].call(current)
@new_arr << current
end
end
return @new_arr
end
def get_filter
@hash[@hash_index]
end
-
end

Светослав обнови решението на 25.10.2014 01:05 (преди около 10 години)

class NumberSet
include Enumerable
def initialize()
@array = Array.new
end
def each
@array.each do |current|
yield current
end
end
def <<(input_number)
if not @array.include?(input_number)
@array << input_number
end
end
def size
@array.size
end
def empty?
@array.empty?
end
def [](filter)
result = filter.filter_array(@array)
filtered_set = NumberSet.new
result.each do |current|
filtered_set << current
end
return filtered_set
end
end
module CustomFilterOperators
def &(other)
- Filter.new { |x| get_filter.call(x) && other.get_filter.call(x) }
+ Filter.new { |x| get_filter.call(x) and other.get_filter.call(x) }
end
def |(other)
- Filter.new { |x| get_filter.call(x) || other.get_filter.call(x) }
+ Filter.new { |x| get_filter.call(x) or other.get_filter.call(x) }
end
end
class Filter
include CustomFilterOperators
def initialize(&block)
@filter = block
end
def filter_array(numbers)
@new_arr = Array.new
numbers.each do |current|
if @filter.call(current)
@new_arr << current
end
end
return @new_arr
end
def get_filter
@filter
end
end
class TypeFilter
include CustomFilterOperators
def initialize(index)
is_integer = lambda { |number| number.integer? }
is_real = lambda { |number| number.real? }
is_complex = lambda { |number| number.eql?(number.to_c) }
@hash = { :integer => is_integer ,
:real => is_real ,
:complex => is_complex }
@hash_index = index
end
def filter_array(numbers)
@new_arr = Array.new
numbers.each do |current|
if @hash[@hash_index].call(current)
@new_arr << current
end
end
return @new_arr
end
def get_filter
@hash[@hash_index]
end
end
class SignFilter
include CustomFilterOperators
def initialize(index)
is_positive = lambda { |number| number > 0 }
is_non_positive = lambda { |number| number <= 0 }
is_negative = lambda { |number| number < 0 }
is_non_negative = lambda { |number| number >= 0}
@hash = { :positive => is_positive , :non_positive => is_non_positive ,
:negative => is_negative , :non_negative => is_non_negative }
@hash_index = index
end
def filter_array(numbers)
@new_arr = Array.new
numbers.each do |current|
if @hash[@hash_index].call(current)
@new_arr << current
end
end
return @new_arr
end
def get_filter
@hash[@hash_index]
end
end

Здрасти,

Ето няколко неща, върху които можеш да помислиш:

  • Не ползвай Array.new. Има доста по-удобен литерал в езика.
  • Не слагай скоби при дефиниране на метод без параметри.
  • Виж как се държи Array#each, когато не му подадеш блок. Очакваме NumberSet#each да работи по същия начин.
  • if not ... е същото като unless .... Предпочитаме второто, защото е по-ясно.
  • NumberSet#[] е неправилно идентиран. Също така можеш да хвърлиш око на Enumerable#each_with_object.
  • Защо си решил да ползваш mixin? Според мен можеш да имплементираш методите на CustomFilterOperators във Filter и да оставиш TypeFilter и SignFilter да ги наследят.
  • Мисля си, че get_filter метода ти е излишен. Можеш да пазиш анонимната функция във @filter поле и да го достъпваш директно.
  • Също така си мисля, че ако вместо Hash-ове ползваш case кодът ти ще стане по-разбираем.

Светослав обнови решението на 25.10.2014 14:53 (преди около 10 години)

class NumberSet
include Enumerable
- def initialize()
- @array = Array.new
+ def initialize
+ @array = []
end
def each
- @array.each do |current|
- yield current
+ if (block_given?)
+ @array.each do |current|
+ yield current
+ end
+
+ return @array
+ else
+ to_enum(:each)
end
end
def <<(input_number)
- if not @array.include?(input_number)
+ unless @array.include?(input_number)
@array << input_number
end
end
def size
@array.size
end
def empty?
@array.empty?
end
- def [](filter)
- result = filter.filter_array(@array)
+ def [](filter_object)
+ result_array = filter_object.filter_array(@array)
filtered_set = NumberSet.new
- result.each do |current|
+ result_array.each do |current|
filtered_set << current
end
return filtered_set
end
end
-module CustomFilterOperators
-
- def &(other)
- Filter.new { |x| get_filter.call(x) and other.get_filter.call(x) }
- end
-
- def |(other)
- Filter.new { |x| get_filter.call(x) or other.get_filter.call(x) }
- end
-end
-
-
class Filter
- include CustomFilterOperators
+ attr_reader :filter
def initialize(&block)
@filter = block
end
def filter_array(numbers)
- @new_arr = Array.new
+ @new_arr = []
numbers.each do |current|
if @filter.call(current)
@new_arr << current
end
end
return @new_arr
end
- def get_filter
- @filter
+ def &(other)
+ Filter.new { |x| @filter.call(x) && other.filter.call(x) }
end
-end
+ def |(other)
+ Filter.new { |x| @filter.call(x) || other.filter.call(x) }
+ end
-class TypeFilter
- include CustomFilterOperators
+end
- def initialize(index)
- is_integer = lambda { |number| number.integer? }
- is_real = lambda { |number| number.real? }
- is_complex = lambda { |number| number.eql?(number.to_c) }
- @hash = { :integer => is_integer ,
- :real => is_real ,
- :complex => is_complex }
- @hash_index = index
- end
+class TypeFilter < Filter
- def filter_array(numbers)
- @new_arr = Array.new
- numbers.each do |current|
- if @hash[@hash_index].call(current)
- @new_arr << current
- end
- end
-
- return @new_arr
+ def initialize(filter_key)
+ @filter = case filter_key
+ when :integer then lambda { |number| number.integer? }
+ when :real then lambda { |number| number.real? }
+ else lambda { |number| number.eql?(number.to_c) }
+ end
end
- def get_filter
- @hash[@hash_index]
- end
end
-class SignFilter
- include CustomFilterOperators
+class SignFilter < Filter
- def initialize(index)
- is_positive = lambda { |number| number > 0 }
- is_non_positive = lambda { |number| number <= 0 }
- is_negative = lambda { |number| number < 0 }
- is_non_negative = lambda { |number| number >= 0}
-
- @hash = { :positive => is_positive , :non_positive => is_non_positive ,
- :negative => is_negative , :non_negative => is_non_negative }
- @hash_index = index
+ def initialize(filter_key)
+ @filter = case filter_key
+ when :positive then lambda { |number| number > 0 }
+ when :non_positive then lambda { |number| number <= 0 }
+ when :negative then lambda { |number| number < 0 }
+ else lambda { |number| number >= 0}
+ end
end
- def filter_array(numbers)
- @new_arr = Array.new
- numbers.each do |current|
- if @hash[@hash_index].call(current)
- @new_arr << current
- end
- end
-
- return @new_arr
- end
-
- def get_filter
- @hash[@hash_index]
- end
end

Светослав обнови решението на 25.10.2014 15:04 (преди около 10 години)

class NumberSet
include Enumerable
def initialize
@array = []
end
def each
if (block_given?)
@array.each do |current|
yield current
end
return @array
else
to_enum(:each)
end
end
def <<(input_number)
unless @array.include?(input_number)
@array << input_number
end
end
def size
@array.size
end
def empty?
@array.empty?
end
def [](filter_object)
result_array = filter_object.filter_array(@array)
filtered_set = NumberSet.new
result_array.each do |current|
filtered_set << current
end
return filtered_set
end
end
class Filter
attr_reader :filter
def initialize(&block)
@filter = block
end
def filter_array(numbers)
@new_arr = []
numbers.each do |current|
if @filter.call(current)
@new_arr << current
end
end
return @new_arr
end
def &(other)
- Filter.new { |x| @filter.call(x) && other.filter.call(x) }
+ Filter.new { |x| @filter.call(x) and other.filter.call(x) }
end
def |(other)
- Filter.new { |x| @filter.call(x) || other.filter.call(x) }
+ Filter.new { |x| @filter.call(x) or other.filter.call(x) }
end
end
class TypeFilter < Filter
def initialize(filter_key)
@filter = case filter_key
when :integer then lambda { |number| number.integer? }
when :real then lambda { |number| number.real? }
else lambda { |number| number.eql?(number.to_c) }
end
end
end
class SignFilter < Filter
def initialize(filter_key)
@filter = case filter_key
when :positive then lambda { |number| number > 0 }
when :non_positive then lambda { |number| number <= 0 }
when :negative then lambda { |number| number < 0 }
else lambda { |number| number >= 0}
end
end
end

@Светослав, за NumberSet#[] коментарът на Станислав съдържа два елемента:

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

class NumberSet
include Enumerable
def initialize
@array = []
end
def each
if (block_given?)
@array.each do |current|
yield current
end
return @array
else
to_enum(:each)
end
end
def <<(input_number)
unless @array.include?(input_number)
@array << input_number
end
end
def size
@array.size
end
def empty?
@array.empty?
end
- def [](filter_object)
- result_array = filter_object.filter_array(@array)
-
- filtered_set = NumberSet.new
- result_array.each do |current|
- filtered_set << current
- end
-
- return filtered_set
+ def [](condition)
+ @array.each_with_object([]) { |i , a| a << i if condition.filter.call(i) }
end
end
class Filter
attr_reader :filter
def initialize(&block)
@filter = block
- end
-
- def filter_array(numbers)
- @new_arr = []
- numbers.each do |current|
- if @filter.call(current)
- @new_arr << current
- end
- end
-
- return @new_arr
end
def &(other)
Filter.new { |x| @filter.call(x) and other.filter.call(x) }
end
def |(other)
Filter.new { |x| @filter.call(x) or other.filter.call(x) }
end
end
class TypeFilter < Filter
def initialize(filter_key)
@filter = case filter_key
when :integer then lambda { |number| number.integer? }
when :real then lambda { |number| number.real? }
else lambda { |number| number.eql?(number.to_c) }
end
end
end
class SignFilter < Filter
def initialize(filter_key)
@filter = case filter_key
when :positive then lambda { |number| number > 0 }
when :non_positive then lambda { |number| number <= 0 }
when :negative then lambda { |number| number < 0 }
else lambda { |number| number >= 0}
end
end
end

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

class NumberSet
include Enumerable
def initialize
@array = []
end
def each
if (block_given?)
@array.each do |current|
yield current
end
return @array
else
to_enum(:each)
end
end
def <<(input_number)
unless @array.include?(input_number)
@array << input_number
end
end
def size
@array.size
end
def empty?
@array.empty?
end
def [](condition)
@array.each_with_object([]) { |i , a| a << i if condition.filter.call(i) }
end
end
class Filter
attr_reader :filter
def initialize(&block)
@filter = block
end
+ def to_proc
+ ->(object, *args) { object.public_send self, *args}
+ end
+
def &(other)
Filter.new { |x| @filter.call(x) and other.filter.call(x) }
end
def |(other)
Filter.new { |x| @filter.call(x) or other.filter.call(x) }
end
end
class TypeFilter < Filter
def initialize(filter_key)
- @filter = case filter_key
- when :integer then lambda { |number| number.integer? }
- when :real then lambda { |number| number.real? }
- else lambda { |number| number.eql?(number.to_c) }
- end
+ @filter =
+ case filter_key
+ when :integer then lambda { |number| number.integer? }
+ when :real then lambda { |number| number.real? and (not number.integer?) }
+ else lambda { |number| number.eql?(number.to_c) }
+ end
end
end
class SignFilter < Filter
def initialize(filter_key)
- @filter = case filter_key
- when :positive then lambda { |number| number > 0 }
- when :non_positive then lambda { |number| number <= 0 }
- when :negative then lambda { |number| number < 0 }
- else lambda { |number| number >= 0}
- end
+ @filter =
+ case filter_key
+ when :positive then lambda { |number| number > 0 }
+ when :non_positive then lambda { |number| number <= 0 }
+ when :negative then lambda { |number| number < 0 }
+ else lambda { |number| number >= 0}
+ end
end
end