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

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

Към профила на Ростислав Градинаров

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 3 успешни тест(а)
  • 8 неуспешни тест(а)

Код

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
attr_accessor :songs
include Enumerable
def initialize(list_of_songs)
@songs = list_of_songs
end
def self.parse(text)
arr = generate_song_array(text)
songs = generate_songs_from_array(arr)
Collection.new(songs)
end
def artists
array_of_artists = []
@songs.each { |song| array_of_artists << song.artist }
array_of_artists.uniq
end
def albums
array_of_albums = []
@songs.each { |song| array_of_albums << song.album }
array_of_albums.uniq
end
def names
array_of_song_names = []
@songs.each { |song| array_of_song_names << song.name }
array_of_song_names.uniq
end
def filter(filter_array)
collection = Collection.new([])
collection.songs = @songs.select { |song| filter_array.contains?(song) }
collection
end
def adjoin(collection)
new_collection = Collection.new([])
new_collection.songs = @songs | collection.songs
new_collection
end
private
def self.generate_song_array(text)
song_array = []
text.each_line do |line|
song_array << line.chomp
end
song_array.reject { |line| line.empty? }
end
def self.generate_songs_from_array(song_array)
array_of_songs = []
song_array.each_slice(3) do |sub_array|
array_of_songs << Song.new(sub_array[0], sub_array[1], sub_array[2])
end
array_of_songs
end
end
class FilterSong < Song
attr_accessor :not_name, :not_artist, :not_album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
@not_name, @not_artist, @not_album = nil, nil, nil
end
def combine_filter(filter_array)
filter_array.filter_songs.map do |filter_song|
filter_song.combine_with(self)
end
end
def !
song = FilterSong.new(@not_name, @not_artist, @not_album)
song.not_name, song.not_artist, song.not_album = @name, @artist, @album
song
end
def compare_with(other)
self.generate_fields(other)
other.name == @name && other.name != @not_name &&
other.artist == @artist && other.artist != @not_artist &&
other.album == @album && other.album != @not_album
end
def generate_fields(other)
@name = other.name if @name == nil
@artist = other.artist if @artist == nil
@album = other.album if @album == nil
end
def combine_with(other)
@name = other.name if @name == nil
@artist = other.artist if @artist == nil
@album = other.album if @album == nil
combine_not_fields_with(other)
end
def combine_not_fields_with(other)
@not_name = other.not_name if @not_name == nil
@not_artist = other.not_artist if @not_artist == nil
@not_album = other.not_album if @not_album == nil
self
end
def empty?
@name == nil && @artist == nil && @album == nil &&
@not_name == nil && @not_artist == nil && @not_album == nil
end
end
class Filter
attr_accessor :filter_songs
def initialize(name, artist, album)
@filter_songs = []
@filter_songs << FilterSong.new(name, artist, album)
end
def &(other)
f = Filter.new(nil, nil, nil)
@filter_songs.each { |song| f.filter_songs << song.combine_filter(other) }
f.filter_songs = f.filter_songs.reject { |song| song.empty? }.flatten
f
end
def |(other)
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs | other.filter_songs
filter
end
def !
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs.map { |song| !song }
filter
end
def contains?(other_song)
@filter_songs.any? { |song| song.compare_with(other_song) }
end
end
module Criteria
def self.name(song_name)
Filter.new(song_name, nil, nil)
end
def self.artist(song_artist)
Filter.new(nil, song_artist, nil)
end
def self.album(song_album)
Filter.new(nil, nil, song_album)
end
end

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

...FFFFFFFF

Failures:

  1) Collection can be filtered by song name
     Failure/Error: filtered.map(&:artist).should =~ ['Eva Cassidy', 'Sting']
     NoMethodError:
       undefined method `each' for #<Collection:0x9813914>
     # /tmp/d20130203-23049-1cooiwd/spec.rb:45:in `map'
     # /tmp/d20130203-23049-1cooiwd/spec.rb:45:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Collection can be filtered by song name
     Failure/Error: filtered.map(&:album).should =~ ['The Soul Cages', "Ten Summoner's Tales"]
     NoMethodError:
       undefined method `each' for #<Collection:0x982a448>
     # /tmp/d20130203-23049-1cooiwd/spec.rb:50:in `map'
     # /tmp/d20130203-23049-1cooiwd/spec.rb:50:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Collection can be filtered by album
     Failure/Error: filtered.map(&:name).should =~ ['Fields of Gold', 'Autumn Leaves']
     NoMethodError:
       undefined method `each' for #<Collection:0x9836518 @songs=[]>
     # /tmp/d20130203-23049-1cooiwd/spec.rb:55:in `map'
     # /tmp/d20130203-23049-1cooiwd/spec.rb:55:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Collection can return an empty result
     Failure/Error: filtered.to_a.should eq []
     NoMethodError:
       undefined method `each' for #<Collection:0x983e600 @songs=[]>
     # /tmp/d20130203-23049-1cooiwd/spec.rb:60:in `to_a'
     # /tmp/d20130203-23049-1cooiwd/spec.rb:60:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) Collection supports a conjuction of filters
     Failure/Error: filtered.map(&:album).should eq ["Ten Summoner's Tales"]
     NoMethodError:
       undefined method `each' for #<Collection:0x9846e18>
     # /tmp/d20130203-23049-1cooiwd/spec.rb:65:in `map'
     # /tmp/d20130203-23049-1cooiwd/spec.rb:65:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) Collection supports a disjunction of filters
     Failure/Error: filtered.map(&:album).should =~ [
     NoMethodError:
       undefined method `each' for #<Collection:0x9a80e2c>
     # /tmp/d20130203-23049-1cooiwd/spec.rb:70:in `map'
     # /tmp/d20130203-23049-1cooiwd/spec.rb:70:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) Collection supports negation of filters
     Failure/Error: filtered.map(&:name).should eq ['Mad About You']
     NoMethodError:
       undefined method `each' for #<Collection:0x9a7ec58 @songs=[]>
     # /tmp/d20130203-23049-1cooiwd/spec.rb:79:in `map'
     # /tmp/d20130203-23049-1cooiwd/spec.rb:79:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) Collection can be adjoined with another collection
     Failure/Error: adjoined.count.should eq 4
     NoMethodError:
       undefined method `each' for #<Collection:0xa06e2e4>
     # /tmp/d20130203-23049-1cooiwd/spec.rb:87:in `count'
     # /tmp/d20130203-23049-1cooiwd/spec.rb:87:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.00972 seconds
11 examples, 8 failures

Failed examples:

rspec /tmp/d20130203-23049-1cooiwd/spec.rb:43 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-1cooiwd/spec.rb:48 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-1cooiwd/spec.rb:53 # Collection can be filtered by album
rspec /tmp/d20130203-23049-1cooiwd/spec.rb:58 # Collection can return an empty result
rspec /tmp/d20130203-23049-1cooiwd/spec.rb:63 # Collection supports a conjuction of filters
rspec /tmp/d20130203-23049-1cooiwd/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-1cooiwd/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-1cooiwd/spec.rb:82 # Collection can be adjoined with another collection

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

Ростислав обнови решението на 31.10.2012 01:11 (преди около 12 години)

+class Song
+ attr_accessor :name, :artist, :album
+
+ def initialize(name, artist, album)
+ @name = name
+ @artist = artist
+ @album = album
+ end
+end
+
+class Collection
+ attr_accessor :songs
+
+ include Enumerable
+
+ def initialize(list_of_songs)
+ @songs = list_of_songs
+ end
+
+ def self.parse(text)
+ arr = generate_song_array(text)
+ songs = generate_songs_from_array(arr)
+ Collection.new(songs)
+ end
+
+ def artists
+ array_of_artists = []
+ @songs.each { |song| array_of_artists << song.artist }
+ array_of_artists.uniq
+ end
+
+ def albums
+ array_of_albums = []
+ @songs.each { |song| array_of_albums << song.album }
+ array_of_albums.uniq
+ end
+
+ def names
+ array_of_song_names = []
+ @songs.each { |song| array_of_song_names << song.name }
+ array_of_song_names.uniq
+ end
+
+ def filter(filter_array)
+ collection = Collection.new([])
+ collection.songs = @songs.select { |song| filter_array.contains?(song) }
+ collection
+ end
+
+ def adjoin(collection)
+ new_collection = Collection.new([])
+ new_collection = @songs | collection.songs
+ new_collection
+ end
+
+ private
+
+ def self.generate_song_array(text)
+ song_array = []
+ text.each_line do |line|
+ song_array << line.chomp
+ end
+ song_array.reject { |line| line.empty? }
+ end
+
+ def self.generate_songs_from_array(song_array)
+ array_of_songs = []
+ song_array.each_slice(3) do |sub_array|
+ array_of_songs << Song.new(sub_array[0], sub_array[1], sub_array[2])
+ end
+ array_of_songs
+ end
+end
+
+class FilterSong < Song
+ attr_accessor :not_name, :not_artist, :not_album
+
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
+ @not_name, @not_artist, @not_album = nil, nil, nil
+ end
+
+ def combine_filter(filter_array)
+ filter_array.filter_songs.map do |filter_song|
+ filter_song.combine_with(self)
+ end
+ end
+
+ def !
+ song = FilterSong.new(@not_name, @not_artist, @not_album)
+ song.not_name, song.not_artist, song.not_album = @name, @artist, @album
+ song
+ end
+
+ def compare_with(other)
+ song = generateCompareSong(FilterSong.new(@name, @artist, @album), other)
+ other.name == song.name && other.name != song.not_name &&
+ other.artist == song.artist && other.artist != song.not_artist &&
+ other.album == song.album && other.album != song.not_album
+ end
+
+ def generateCompareSong(song, other)
+ song.name = other.name if song.name == nil && song.not_name == nil
+ song.artist = other.artist if song.artist == nil && song.not_artist == nil
+ song.album = other.album if song.album == nil && song.not_album == nil
+ song
+ end
+
+ def combine_with(other)
+ @name = other.name if @name == nil
+ @artist = other.artist if @artist == nil
+ @album = other.album if @album == nil
+ combine_not_fields_with(other)
+ end
+
+ def combine_not_fields_with(other)
+ @not_name = other.not_name if @not_name == nil
+ @not_artist = other.not_artist if @not_artist == nil
+ @not_album = other.not_album if @not_album == nil
+ self
+ end
+
+ def empty?
+ @name == nil && @artist == nil && @album == nil &&
+ @not_name == nil && @not_artist == nil && @not_album == nil
+ end
+end
+
+class Filter
+ attr_accessor :filter_songs
+
+ def initialize(name, artist, album)
+ @filter_songs = []
+ @filter_songs << FilterSong.new(name, artist, album)
+ end
+
+ def &(other)
+ f = Filter.new(nil, nil, nil)
+ @filter_songs.each { |song| f.filter_songs << song.combine_filter(other) }
+ f.filter_songs = f.filter_songs.reject { |song| song.empty? }.flatten
+ f
+ end
+
+ def |(other)
+ filter = Filter.new(nil, nil, nil)
+ filter.filter_songs = @filter_songs | other.filter_songs
+ filter
+ end
+
+ def !
+ filter = Filter.new(nil, nil, nil)
+ filter.filter_songs = @filter_songs.map { |song| !song }
+ filter
+ end
+
+ def contains?(other_song)
+ @filter_songs.any? do |song|
+ song = generate_empty_strings(song)
+ song.compare_with(other_song)
+ end
+ end
+
+ private
+
+ def generate_empty_strings(song)
+ song.not_name == "1" if song.not_name == nil
+ song.not_artist == "1" if song.not_artist == nil
+ song.not_album == "1" if song.not_album == nil
+ song
+ end
+end
+
+module Criteria
+ def self.name(song_name)
+ Filter.new(song_name, nil, nil)
+ end
+
+ def self.artist(song_artist)
+ Filter.new(nil, song_artist, nil)
+ end
+
+ def self.album(song_album)
+ Filter.new(nil, nil, song_album)
+ end
+end

Ростислав обнови решението на 31.10.2012 01:14 (преди около 12 години)

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
attr_accessor :songs
include Enumerable
def initialize(list_of_songs)
@songs = list_of_songs
end
def self.parse(text)
arr = generate_song_array(text)
songs = generate_songs_from_array(arr)
Collection.new(songs)
end
def artists
array_of_artists = []
@songs.each { |song| array_of_artists << song.artist }
array_of_artists.uniq
end
def albums
array_of_albums = []
@songs.each { |song| array_of_albums << song.album }
array_of_albums.uniq
end
def names
array_of_song_names = []
@songs.each { |song| array_of_song_names << song.name }
array_of_song_names.uniq
end
def filter(filter_array)
collection = Collection.new([])
collection.songs = @songs.select { |song| filter_array.contains?(song) }
collection
end
def adjoin(collection)
new_collection = Collection.new([])
new_collection = @songs | collection.songs
new_collection
end
private
def self.generate_song_array(text)
song_array = []
text.each_line do |line|
song_array << line.chomp
end
song_array.reject { |line| line.empty? }
end
def self.generate_songs_from_array(song_array)
array_of_songs = []
song_array.each_slice(3) do |sub_array|
array_of_songs << Song.new(sub_array[0], sub_array[1], sub_array[2])
end
array_of_songs
end
end
class FilterSong < Song
attr_accessor :not_name, :not_artist, :not_album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
@not_name, @not_artist, @not_album = nil, nil, nil
end
def combine_filter(filter_array)
filter_array.filter_songs.map do |filter_song|
filter_song.combine_with(self)
end
end
def !
song = FilterSong.new(@not_name, @not_artist, @not_album)
song.not_name, song.not_artist, song.not_album = @name, @artist, @album
song
end
def compare_with(other)
song = generateCompareSong(FilterSong.new(@name, @artist, @album), other)
other.name == song.name && other.name != song.not_name &&
other.artist == song.artist && other.artist != song.not_artist &&
other.album == song.album && other.album != song.not_album
end
def generateCompareSong(song, other)
song.name = other.name if song.name == nil && song.not_name == nil
song.artist = other.artist if song.artist == nil && song.not_artist == nil
song.album = other.album if song.album == nil && song.not_album == nil
song
end
def combine_with(other)
@name = other.name if @name == nil
@artist = other.artist if @artist == nil
@album = other.album if @album == nil
combine_not_fields_with(other)
end
def combine_not_fields_with(other)
@not_name = other.not_name if @not_name == nil
@not_artist = other.not_artist if @not_artist == nil
@not_album = other.not_album if @not_album == nil
self
end
def empty?
@name == nil && @artist == nil && @album == nil &&
@not_name == nil && @not_artist == nil && @not_album == nil
end
end
class Filter
attr_accessor :filter_songs
def initialize(name, artist, album)
@filter_songs = []
@filter_songs << FilterSong.new(name, artist, album)
end
def &(other)
f = Filter.new(nil, nil, nil)
@filter_songs.each { |song| f.filter_songs << song.combine_filter(other) }
f.filter_songs = f.filter_songs.reject { |song| song.empty? }.flatten
f
end
def |(other)
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs | other.filter_songs
filter
end
def !
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs.map { |song| !song }
filter
end
def contains?(other_song)
- @filter_songs.any? do |song|
- song = generate_empty_strings(song)
- song.compare_with(other_song)
- end
- end
-
- private
-
- def generate_empty_strings(song)
- song.not_name == "1" if song.not_name == nil
- song.not_artist == "1" if song.not_artist == nil
- song.not_album == "1" if song.not_album == nil
- song
+ @filter_songs.any? { |song| song.compare_with(other_song) }
end
end
module Criteria
def self.name(song_name)
Filter.new(song_name, nil, nil)
end
def self.artist(song_artist)
Filter.new(nil, song_artist, nil)
end
def self.album(song_album)
Filter.new(nil, nil, song_album)
end
end

Ростислав обнови решението на 31.10.2012 10:17 (преди около 12 години)

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
attr_accessor :songs
include Enumerable
def initialize(list_of_songs)
@songs = list_of_songs
end
def self.parse(text)
arr = generate_song_array(text)
songs = generate_songs_from_array(arr)
Collection.new(songs)
end
def artists
array_of_artists = []
@songs.each { |song| array_of_artists << song.artist }
array_of_artists.uniq
end
def albums
array_of_albums = []
@songs.each { |song| array_of_albums << song.album }
array_of_albums.uniq
end
def names
array_of_song_names = []
@songs.each { |song| array_of_song_names << song.name }
array_of_song_names.uniq
end
def filter(filter_array)
collection = Collection.new([])
collection.songs = @songs.select { |song| filter_array.contains?(song) }
collection
end
def adjoin(collection)
new_collection = Collection.new([])
- new_collection = @songs | collection.songs
+ new_collection.songs = @songs | collection.songs
new_collection
end
private
def self.generate_song_array(text)
song_array = []
text.each_line do |line|
song_array << line.chomp
end
song_array.reject { |line| line.empty? }
end
def self.generate_songs_from_array(song_array)
array_of_songs = []
song_array.each_slice(3) do |sub_array|
array_of_songs << Song.new(sub_array[0], sub_array[1], sub_array[2])
end
array_of_songs
end
end
class FilterSong < Song
attr_accessor :not_name, :not_artist, :not_album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
@not_name, @not_artist, @not_album = nil, nil, nil
end
def combine_filter(filter_array)
filter_array.filter_songs.map do |filter_song|
filter_song.combine_with(self)
end
end
def !
song = FilterSong.new(@not_name, @not_artist, @not_album)
song.not_name, song.not_artist, song.not_album = @name, @artist, @album
song
end
def compare_with(other)
song = generateCompareSong(FilterSong.new(@name, @artist, @album), other)
other.name == song.name && other.name != song.not_name &&
other.artist == song.artist && other.artist != song.not_artist &&
other.album == song.album && other.album != song.not_album
end
def generateCompareSong(song, other)
song.name = other.name if song.name == nil && song.not_name == nil
song.artist = other.artist if song.artist == nil && song.not_artist == nil
song.album = other.album if song.album == nil && song.not_album == nil
song
end
def combine_with(other)
@name = other.name if @name == nil
@artist = other.artist if @artist == nil
@album = other.album if @album == nil
combine_not_fields_with(other)
end
def combine_not_fields_with(other)
@not_name = other.not_name if @not_name == nil
@not_artist = other.not_artist if @not_artist == nil
@not_album = other.not_album if @not_album == nil
self
end
def empty?
@name == nil && @artist == nil && @album == nil &&
@not_name == nil && @not_artist == nil && @not_album == nil
end
end
class Filter
attr_accessor :filter_songs
def initialize(name, artist, album)
@filter_songs = []
@filter_songs << FilterSong.new(name, artist, album)
end
def &(other)
f = Filter.new(nil, nil, nil)
@filter_songs.each { |song| f.filter_songs << song.combine_filter(other) }
f.filter_songs = f.filter_songs.reject { |song| song.empty? }.flatten
f
end
def |(other)
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs | other.filter_songs
filter
end
def !
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs.map { |song| !song }
filter
end
def contains?(other_song)
@filter_songs.any? { |song| song.compare_with(other_song) }
end
end
module Criteria
def self.name(song_name)
Filter.new(song_name, nil, nil)
end
def self.artist(song_artist)
Filter.new(nil, song_artist, nil)
end
def self.album(song_album)
Filter.new(nil, nil, song_album)
end
end

Ростислав обнови решението на 31.10.2012 13:25 (преди около 12 години)

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
attr_accessor :songs
include Enumerable
def initialize(list_of_songs)
@songs = list_of_songs
end
def self.parse(text)
arr = generate_song_array(text)
songs = generate_songs_from_array(arr)
Collection.new(songs)
end
def artists
array_of_artists = []
@songs.each { |song| array_of_artists << song.artist }
array_of_artists.uniq
end
def albums
array_of_albums = []
@songs.each { |song| array_of_albums << song.album }
array_of_albums.uniq
end
def names
array_of_song_names = []
@songs.each { |song| array_of_song_names << song.name }
array_of_song_names.uniq
end
def filter(filter_array)
collection = Collection.new([])
collection.songs = @songs.select { |song| filter_array.contains?(song) }
collection
end
def adjoin(collection)
new_collection = Collection.new([])
new_collection.songs = @songs | collection.songs
new_collection
end
private
def self.generate_song_array(text)
song_array = []
text.each_line do |line|
song_array << line.chomp
end
song_array.reject { |line| line.empty? }
end
def self.generate_songs_from_array(song_array)
array_of_songs = []
song_array.each_slice(3) do |sub_array|
array_of_songs << Song.new(sub_array[0], sub_array[1], sub_array[2])
end
array_of_songs
end
end
class FilterSong < Song
attr_accessor :not_name, :not_artist, :not_album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
@not_name, @not_artist, @not_album = nil, nil, nil
end
def combine_filter(filter_array)
filter_array.filter_songs.map do |filter_song|
filter_song.combine_with(self)
end
end
def !
song = FilterSong.new(@not_name, @not_artist, @not_album)
song.not_name, song.not_artist, song.not_album = @name, @artist, @album
song
end
def compare_with(other)
- song = generateCompareSong(FilterSong.new(@name, @artist, @album), other)
+ song.generate_fields(other)
other.name == song.name && other.name != song.not_name &&
other.artist == song.artist && other.artist != song.not_artist &&
other.album == song.album && other.album != song.not_album
end
- def generateCompareSong(song, other)
- song.name = other.name if song.name == nil && song.not_name == nil
- song.artist = other.artist if song.artist == nil && song.not_artist == nil
- song.album = other.album if song.album == nil && song.not_album == nil
- song
+ def generate_fields(other)
+ @name = other.name if @name == nil
+ @artist = other.artist if @artist == nil
+ @album = other.album if @album == nil
end
def combine_with(other)
@name = other.name if @name == nil
@artist = other.artist if @artist == nil
@album = other.album if @album == nil
combine_not_fields_with(other)
end
def combine_not_fields_with(other)
@not_name = other.not_name if @not_name == nil
@not_artist = other.not_artist if @not_artist == nil
@not_album = other.not_album if @not_album == nil
self
end
def empty?
@name == nil && @artist == nil && @album == nil &&
@not_name == nil && @not_artist == nil && @not_album == nil
end
end
class Filter
attr_accessor :filter_songs
def initialize(name, artist, album)
@filter_songs = []
@filter_songs << FilterSong.new(name, artist, album)
end
def &(other)
f = Filter.new(nil, nil, nil)
@filter_songs.each { |song| f.filter_songs << song.combine_filter(other) }
f.filter_songs = f.filter_songs.reject { |song| song.empty? }.flatten
f
end
def |(other)
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs | other.filter_songs
filter
end
def !
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs.map { |song| !song }
filter
end
def contains?(other_song)
@filter_songs.any? { |song| song.compare_with(other_song) }
end
end
module Criteria
def self.name(song_name)
Filter.new(song_name, nil, nil)
end
def self.artist(song_artist)
Filter.new(nil, song_artist, nil)
end
def self.album(song_album)
Filter.new(nil, nil, song_album)
end
end

Ростислав обнови решението на 31.10.2012 13:30 (преди около 12 години)

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
attr_accessor :songs
include Enumerable
def initialize(list_of_songs)
@songs = list_of_songs
end
def self.parse(text)
arr = generate_song_array(text)
songs = generate_songs_from_array(arr)
Collection.new(songs)
end
def artists
array_of_artists = []
@songs.each { |song| array_of_artists << song.artist }
array_of_artists.uniq
end
def albums
array_of_albums = []
@songs.each { |song| array_of_albums << song.album }
array_of_albums.uniq
end
def names
array_of_song_names = []
@songs.each { |song| array_of_song_names << song.name }
array_of_song_names.uniq
end
def filter(filter_array)
collection = Collection.new([])
collection.songs = @songs.select { |song| filter_array.contains?(song) }
collection
end
def adjoin(collection)
new_collection = Collection.new([])
new_collection.songs = @songs | collection.songs
new_collection
end
private
def self.generate_song_array(text)
song_array = []
text.each_line do |line|
song_array << line.chomp
end
song_array.reject { |line| line.empty? }
end
def self.generate_songs_from_array(song_array)
array_of_songs = []
song_array.each_slice(3) do |sub_array|
array_of_songs << Song.new(sub_array[0], sub_array[1], sub_array[2])
end
array_of_songs
end
end
class FilterSong < Song
attr_accessor :not_name, :not_artist, :not_album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
@not_name, @not_artist, @not_album = nil, nil, nil
end
def combine_filter(filter_array)
filter_array.filter_songs.map do |filter_song|
filter_song.combine_with(self)
end
end
def !
song = FilterSong.new(@not_name, @not_artist, @not_album)
song.not_name, song.not_artist, song.not_album = @name, @artist, @album
song
end
def compare_with(other)
- song.generate_fields(other)
- other.name == song.name && other.name != song.not_name &&
- other.artist == song.artist && other.artist != song.not_artist &&
- other.album == song.album && other.album != song.not_album
+ self.generate_fields(other)
+ other.name == @name && other.name != @not_name &&
+ other.artist == @artist && other.artist != @not_artist &&
+ other.album == @album && other.album != @not_album
end
def generate_fields(other)
@name = other.name if @name == nil
@artist = other.artist if @artist == nil
@album = other.album if @album == nil
end
def combine_with(other)
@name = other.name if @name == nil
@artist = other.artist if @artist == nil
@album = other.album if @album == nil
combine_not_fields_with(other)
end
def combine_not_fields_with(other)
@not_name = other.not_name if @not_name == nil
@not_artist = other.not_artist if @not_artist == nil
@not_album = other.not_album if @not_album == nil
self
end
def empty?
@name == nil && @artist == nil && @album == nil &&
@not_name == nil && @not_artist == nil && @not_album == nil
end
end
class Filter
attr_accessor :filter_songs
def initialize(name, artist, album)
@filter_songs = []
@filter_songs << FilterSong.new(name, artist, album)
end
def &(other)
f = Filter.new(nil, nil, nil)
@filter_songs.each { |song| f.filter_songs << song.combine_filter(other) }
f.filter_songs = f.filter_songs.reject { |song| song.empty? }.flatten
f
end
def |(other)
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs | other.filter_songs
filter
end
def !
filter = Filter.new(nil, nil, nil)
filter.filter_songs = @filter_songs.map { |song| !song }
filter
end
def contains?(other_song)
@filter_songs.any? { |song| song.compare_with(other_song) }
end
end
module Criteria
def self.name(song_name)
Filter.new(song_name, nil, nil)
end
def self.artist(song_artist)
Filter.new(nil, song_artist, nil)
end
def self.album(song_album)
Filter.new(nil, nil, song_album)
end
end