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

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

Към профила на Кирчо Кирев

Резултати

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

Код

class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class Criteria
def self.name(name)
check_name = ->(song) { song.name == name }
Criteria.new(check_name)
end
def self.artist(artist)
check_artist = ->(song) { song.artist == artist }
Criteria.new(check_artist)
end
def self.album(album)
check_album = ->(song) { song.album == album }
Criteria.new(check_album)
end
attr_accessor :function
def initialize(function)
@function = function
end
def |(other)
combined = ->(song) { function.call(song) or other.function.call(song) }
Criteria.new(combined)
end
def &(other)
combined = ->(song) { function.call(song) and other.function.call(song) }
Criteria.new(combined)
end
def !
negate = ->(song) { !function.call(song) }
Criteria.new(negate)
end
end
class Collection
include Enumerable
def self.parse(text)
lines = text.split("\n")
songs = lines.each_slice(4).map do |song|
Song.new(song[0], song[1], song[2])
end
Collection.new(songs)
end
attr_reader :songs
def initialize(songs)
@songs = songs
end
def names
songs.map { |song| song.name }.uniq
end
def artists
songs.map { |song| song.artist }.uniq
end
def albums
songs.map { |song| song.album }.uniq
end
def filter(criteria)
filtered = songs.select { |song| criteria.function.call(song) }
Collection.new(filtered)
end
def adjoin(collection)
Collection.new(songs | collection.songs)
end
def each
0.upto(songs.length-1) { |current| yield songs[current] }
end
end

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

...........

Finished in 0.01 seconds
11 examples, 0 failures

История (6 версии и 0 коментара)

Кирчо обнови решението на 27.10.2012 16:00 (преди около 12 години)

+class Song
+ attr_reader :name, :artist, :album
+
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+
+ def match_criterias?(criterias_list)
+ criterias_list.any? do |criterias|
+ conjunction_true? criterias
+ end
+ end
+
+ private
+ def conjunction_true?(list)
+ list.all? { |criteria| match_single_criteria? criteria }
+ end
+
+ def match_single_criteria?(criteria)
+ if criteria.attribute.include? "_not"
+ !(match_single_criteria? !criteria)
+ else
+ send "#{criteria.attribute}_matches?", criteria.value
+ end
+ end
+
+ def name_matches?(name)
+ @name == name
+ end
+
+ def artist_matches?(artist)
+ @artist == artist
+ end
+
+ def album_matches?(album)
+ @album == album
+ end
+end
+
+class CriteriaList
+ attr_reader :list
+
+ def initialize(list)
+ @list = list
+ end
+
+ def |(other)
+ CriteriaList.new(@list | other.list)
+ end
+
+ def &(other)
+ arr, limit = [], @list.size * other.list.size - 1
+ 0.upto(limit) do |i|
+ arr << (@list[i % @list.size] | other.list[(limit - i) % other.list.size])
+ end
+ CriteriaList.new(arr)
+ end
+
+ def !
+ list = @list.map { |criterias| negate_criterias criterias }
+ negated_list = list.map { |current| CriteriaList.new([current]) }
+ negated_list.inject(&:&)
+ end
+
+ private
+ def negate_criterias(criterias)
+ criterias.map { |criteria| !criteria }
+ end
+end
+
+class Criteria
+ attr_reader :attribute, :value
+
+ def self.name(name)
+ CriteriaList.new [[Criteria.new("name", name)]]
+ end
+
+ def self.artist(artist)
+ CriteriaList.new [[Criteria.new("artist", artist)]]
+ end
+
+ def self.album(album)
+ CriteriaList.new [[Criteria.new("album", album)]]
+ end
+
+ def initialize(attribute, value)
+ @attribute = attribute
+ @value = value
+ end
+
+ def !
+ if @attribute.include? "_not"
+ Criteria.new(@attribute.gsub("_not", ""), @value)
+ else
+ Criteria.new(@attribute + "_not", @value)
+ end
+ end
+end
+
+class Collection
+ include Enumerable
+ attr_reader :songs
+
+ def self.parse(text)
+ lines = text.split("\n")
+ songs = lines.each_slice(4).map do |song|
+ Song.new(song[0], song[1], song[2])
+ end
+ Collection.new(songs)
+ end
+
+ def initialize(songs)
+ @songs = songs
+ end
+
+ def names
+ @songs.map { |song| song.name }.uniq
+ end
+
+ def artists
+ @songs.map { |song| song.artist }.uniq
+ end
+
+ def albums
+ @songs.map { |song| song.album }.uniq
+ end
+
+ def filter(criteria)
+ filtered = @songs.select { |song| song.match_criterias? criteria.list }
+ Collection.new(filtered)
+ end
+
+ def adjoin(collection)
+ Collection.new(@songs | collection.songs)
+ end
+
+ def each
+ 0.upto(@songs.length-1) { |current| yield @songs[current] }
+ end
+end

Кирчо обнови решението на 28.10.2012 11:06 (преди около 12 години)

class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def match_criterias?(criterias_list)
criterias_list.any? do |criterias|
conjunction_true? criterias
end
end
private
def conjunction_true?(list)
list.all? { |criteria| match_single_criteria? criteria }
end
def match_single_criteria?(criteria)
if criteria.attribute.include? "_not"
!(match_single_criteria? !criteria)
else
send "#{criteria.attribute}_matches?", criteria.value
end
end
def name_matches?(name)
@name == name
end
def artist_matches?(artist)
@artist == artist
end
def album_matches?(album)
@album == album
end
end
class CriteriaList
attr_reader :list
def initialize(list)
@list = list
end
def |(other)
CriteriaList.new(@list | other.list)
end
def &(other)
arr, limit = [], @list.size * other.list.size - 1
0.upto(limit) do |i|
arr << (@list[i % @list.size] | other.list[(limit - i) % other.list.size])
end
CriteriaList.new(arr)
end
def !
list = @list.map { |criterias| negate_criterias criterias }
negated_list = list.map { |current| CriteriaList.new([current]) }
negated_list.inject(&:&)
end
private
def negate_criterias(criterias)
- criterias.map { |criteria| !criteria }
+ criterias.map(&:!)
end
end
class Criteria
attr_reader :attribute, :value
def self.name(name)
CriteriaList.new [[Criteria.new("name", name)]]
end
def self.artist(artist)
CriteriaList.new [[Criteria.new("artist", artist)]]
end
def self.album(album)
CriteriaList.new [[Criteria.new("album", album)]]
end
def initialize(attribute, value)
@attribute = attribute
@value = value
end
def !
if @attribute.include? "_not"
Criteria.new(@attribute.gsub("_not", ""), @value)
else
Criteria.new(@attribute + "_not", @value)
end
end
end
class Collection
include Enumerable
attr_reader :songs
def self.parse(text)
- lines = text.split("\n")
- songs = lines.each_slice(4).map do |song|
- Song.new(song[0], song[1], song[2])
- end
- Collection.new(songs)
+ lines = text.split("\n")
+ songs = lines.each_slice(4).map do |song|
+ Song.new(song[0], song[1], song[2])
+ end
+ Collection.new(songs)
end
def initialize(songs)
@songs = songs
end
def names
@songs.map { |song| song.name }.uniq
end
def artists
@songs.map { |song| song.artist }.uniq
end
def albums
@songs.map { |song| song.album }.uniq
end
def filter(criteria)
filtered = @songs.select { |song| song.match_criterias? criteria.list }
Collection.new(filtered)
end
def adjoin(collection)
Collection.new(@songs | collection.songs)
end
def each
0.upto(@songs.length-1) { |current| yield @songs[current] }
end
end

Кирчо обнови решението на 29.10.2012 22:06 (преди около 12 години)

class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def match_criterias?(criterias_list)
criterias_list.any? do |criterias|
conjunction_true? criterias
end
end
private
def conjunction_true?(list)
list.all? { |criteria| match_single_criteria? criteria }
end
def match_single_criteria?(criteria)
if criteria.attribute.include? "_not"
!(match_single_criteria? !criteria)
else
send "#{criteria.attribute}_matches?", criteria.value
end
end
def name_matches?(name)
@name == name
end
def artist_matches?(artist)
@artist == artist
end
def album_matches?(album)
@album == album
end
end
class CriteriaList
attr_reader :list
def initialize(list)
@list = list
end
def |(other)
- CriteriaList.new(@list | other.list)
+ CriteriaList.new(list | other.list)
end
def &(other)
- arr, limit = [], @list.size * other.list.size - 1
+ arr, limit = [], list.size * other.list.size - 1
0.upto(limit) do |i|
- arr << (@list[i % @list.size] | other.list[(limit - i) % other.list.size])
+ arr << (list[i % list.size] | other.list[(limit - i) % other.list.size])
end
CriteriaList.new(arr)
end
def !
list = @list.map { |criterias| negate_criterias criterias }
negated_list = list.map { |current| CriteriaList.new([current]) }
negated_list.inject(&:&)
end
private
def negate_criterias(criterias)
criterias.map(&:!)
end
end
class Criteria
attr_reader :attribute, :value
def self.name(name)
CriteriaList.new [[Criteria.new("name", name)]]
end
def self.artist(artist)
CriteriaList.new [[Criteria.new("artist", artist)]]
end
def self.album(album)
CriteriaList.new [[Criteria.new("album", album)]]
end
def initialize(attribute, value)
@attribute = attribute
@value = value
end
def !
if @attribute.include? "_not"
- Criteria.new(@attribute.gsub("_not", ""), @value)
+ Criteria.new(attribute.gsub("_not", ""), value)
else
- Criteria.new(@attribute + "_not", @value)
+ Criteria.new(attribute + "_not", value)
end
end
end
class Collection
include Enumerable
attr_reader :songs
def self.parse(text)
lines = text.split("\n")
songs = lines.each_slice(4).map do |song|
Song.new(song[0], song[1], song[2])
end
Collection.new(songs)
end
def initialize(songs)
@songs = songs
end
def names
- @songs.map { |song| song.name }.uniq
+ songs.map { |song| song.name }.uniq
end
def artists
- @songs.map { |song| song.artist }.uniq
+ songs.map { |song| song.artist }.uniq
end
def albums
- @songs.map { |song| song.album }.uniq
+ songs.map { |song| song.album }.uniq
end
def filter(criteria)
- filtered = @songs.select { |song| song.match_criterias? criteria.list }
+ filtered = songs.select { |song| song.match_criterias? criteria.list }
Collection.new(filtered)
end
def adjoin(collection)
- Collection.new(@songs | collection.songs)
+ Collection.new(songs | collection.songs)
end
def each
- 0.upto(@songs.length-1) { |current| yield @songs[current] }
+ 0.upto(songs.length - 1) { |current| yield songs[current] }
end
end

Кирчо обнови решението на 31.10.2012 09:20 (преди около 12 години)

class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
+end
- def match_criterias?(criterias_list)
- criterias_list.any? do |criterias|
- conjunction_true? criterias
- end
+class Criteria
+ def self.name(name)
+ f = lambda { |song| song.name == name }
+ Criteria.new(f)
end
- private
- def conjunction_true?(list)
- list.all? { |criteria| match_single_criteria? criteria }
+ def self.artist(artist)
+ f = lambda { |song| song.artist == artist }
+ Criteria.new(f)
end
- def match_single_criteria?(criteria)
- if criteria.attribute.include? "_not"
- !(match_single_criteria? !criteria)
- else
- send "#{criteria.attribute}_matches?", criteria.value
- end
+ def self.album(album)
+ f = lambda { |song| song.album == album }
+ Criteria.new(f)
end
- def name_matches?(name)
- @name == name
- end
+ attr_accessor :function
- def artist_matches?(artist)
- @artist == artist
+ def initialize(function)
+ @function = function
end
- def album_matches?(album)
- @album == album
- end
-end
-
-class CriteriaList
- attr_reader :list
-
- def initialize(list)
- @list = list
- end
-
def |(other)
- CriteriaList.new(list | other.list)
+ f = lambda { |song| function.call(song) or other.function.call(song) }
+ Criteria.new(f)
end
def &(other)
- arr, limit = [], list.size * other.list.size - 1
- 0.upto(limit) do |i|
- arr << (list[i % list.size] | other.list[(limit - i) % other.list.size])
- end
- CriteriaList.new(arr)
+ f = lambda { |song| function.call(song) and other.function.call(song) }
+ Criteria.new(f)
end
def !
- list = @list.map { |criterias| negate_criterias criterias }
- negated_list = list.map { |current| CriteriaList.new([current]) }
- negated_list.inject(&:&)
+ f = lambda { |song| !function.call(song) }
+ Criteria.new(f)
end
-
- private
- def negate_criterias(criterias)
- criterias.map(&:!)
- end
end
-class Criteria
- attr_reader :attribute, :value
-
- def self.name(name)
- CriteriaList.new [[Criteria.new("name", name)]]
- end
-
- def self.artist(artist)
- CriteriaList.new [[Criteria.new("artist", artist)]]
- end
-
- def self.album(album)
- CriteriaList.new [[Criteria.new("album", album)]]
- end
-
- def initialize(attribute, value)
- @attribute = attribute
- @value = value
- end
-
- def !
- if @attribute.include? "_not"
- Criteria.new(attribute.gsub("_not", ""), value)
- else
- Criteria.new(attribute + "_not", value)
- end
- end
-end
-
class Collection
include Enumerable
attr_reader :songs
def self.parse(text)
lines = text.split("\n")
songs = lines.each_slice(4).map do |song|
Song.new(song[0], song[1], song[2])
end
Collection.new(songs)
end
def initialize(songs)
@songs = songs
end
def names
songs.map { |song| song.name }.uniq
end
def artists
songs.map { |song| song.artist }.uniq
end
def albums
songs.map { |song| song.album }.uniq
end
def filter(criteria)
- filtered = songs.select { |song| song.match_criterias? criteria.list }
+ filtered = songs.select { |song| criteria.function.call(song) }
Collection.new(filtered)
end
def adjoin(collection)
Collection.new(songs | collection.songs)
end
def each
- 0.upto(songs.length - 1) { |current| yield songs[current] }
+ 0.upto(songs.length-1) { |current| yield songs[current] }
end
end

Кирчо обнови решението на 31.10.2012 16:36 (преди около 12 години)

class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class Criteria
def self.name(name)
- f = lambda { |song| song.name == name }
- Criteria.new(f)
+ check_name = ->(song) { song.name == name }
+ Criteria.new(check_name)
end
def self.artist(artist)
- f = lambda { |song| song.artist == artist }
- Criteria.new(f)
+ check_artist = ->(song) { song.artist == artist }
+ Criteria.new(check_artist)
end
def self.album(album)
- f = lambda { |song| song.album == album }
- Criteria.new(f)
+ check_album = ->(song) { song.album == album }
+ Criteria.new(check_album)
end
attr_accessor :function
def initialize(function)
@function = function
end
def |(other)
- f = lambda { |song| function.call(song) or other.function.call(song) }
- Criteria.new(f)
+ combined = ->(song) { function.call(song) or other.function.call(song) }
+ Criteria.new(combined)
end
def &(other)
- f = lambda { |song| function.call(song) and other.function.call(song) }
- Criteria.new(f)
+ combined = ->(song) { function.call(song) and other.function.call(song) }
+ Criteria.new(combined)
end
def !
- f = lambda { |song| !function.call(song) }
- Criteria.new(f)
+ negate = ->(song) { !function.call(song) }
+ Criteria.new(negate)
end
end
class Collection
include Enumerable
- attr_reader :songs
+
def self.parse(text)
lines = text.split("\n")
songs = lines.each_slice(4).map do |song|
Song.new(song[0], song[1], song[2])
end
Collection.new(songs)
end
+
+ attr_reader :songs
def initialize(songs)
@songs = songs
end
def names
songs.map { |song| song.name }.uniq
end
def artists
songs.map { |song| song.artist }.uniq
end
def albums
songs.map { |song| song.album }.uniq
end
def filter(criteria)
filtered = songs.select { |song| criteria.function.call(song) }
Collection.new(filtered)
end
def adjoin(collection)
Collection.new(songs | collection.songs)
end
def each
0.upto(songs.length-1) { |current| yield songs[current] }
end
end

Кирчо обнови решението на 31.10.2012 16:39 (преди около 12 години)

class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class Criteria
def self.name(name)
check_name = ->(song) { song.name == name }
Criteria.new(check_name)
end
def self.artist(artist)
check_artist = ->(song) { song.artist == artist }
Criteria.new(check_artist)
end
def self.album(album)
check_album = ->(song) { song.album == album }
Criteria.new(check_album)
end
attr_accessor :function
def initialize(function)
@function = function
end
def |(other)
combined = ->(song) { function.call(song) or other.function.call(song) }
Criteria.new(combined)
end
def &(other)
combined = ->(song) { function.call(song) and other.function.call(song) }
Criteria.new(combined)
end
def !
negate = ->(song) { !function.call(song) }
Criteria.new(negate)
end
end
class Collection
include Enumerable
-
def self.parse(text)
lines = text.split("\n")
songs = lines.each_slice(4).map do |song|
Song.new(song[0], song[1], song[2])
end
Collection.new(songs)
end
attr_reader :songs
def initialize(songs)
@songs = songs
end
def names
songs.map { |song| song.name }.uniq
end
def artists
songs.map { |song| song.artist }.uniq
end
def albums
songs.map { |song| song.album }.uniq
end
def filter(criteria)
filtered = songs.select { |song| criteria.function.call(song) }
Collection.new(filtered)
end
def adjoin(collection)
Collection.new(songs | collection.songs)
end
def each
0.upto(songs.length-1) { |current| yield songs[current] }
end
end