Решение на Втора задача от Иван Арабаджиев

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

Към профила на Иван Арабаджиев

Резултати

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

Код

class Song
attr_reader :name, :artist, :album
def initialize(info)
@name, @artist, @album = *info.first(3).map(&:chomp)
end
def hash
[@name, @artist, @album].hash
end
def eql?(other)
hash == other.hash
end
end
class Collection
include Enumerable
@songs = []
attr_reader :songs
def initialize(songs)
@songs = songs
end
def self.parse(input)
Collection.new input.lines.each_slice(4).map { |rows| Song.new rows }
end
def names
get_details :name
end
def artists
get_details :artist
end
def albums
get_details :album
end
def filter(criteria)
Collection.new @songs.select { |song| criteria.accept? song }
end
def each(&block)
@songs.each(&block)
end
def adjoin(other)
Collection.new (@songs + other.songs).uniq
end
private
def get_details(method)
@songs.map { |song| song.send method }.uniq
end
end
class Criteria
def initialize(compiled)
@compiled = compiled
end
def accept?(song)
@compiled[song]
end
def self.name(wanted_name)
Criteria.new ->(song) { song.name == wanted_name }
end
def self.artist(wanted_artist)
Criteria.new ->(song) { song.artist == wanted_artist }
end
def self.album(wanted_album)
Criteria.new ->(song) { song.album == wanted_album }
end
def !
Criteria.new ->(song) { not accept? song }
end
def |(criteria)
Criteria.new ->(song) { accept? song or criteria.accept? song }
end
def &(criteria)
Criteria.new ->(song) { accept? song and criteria.accept? song }
end
end

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

...........

Finished in 0.01043 seconds
11 examples, 0 failures

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

Иван обнови решението на 31.10.2012 11:52 (преди над 11 години)

+class Song
+ attr_reader :name, :artist, :album
+
+ def initialize(info)
+ @name, @artist, @album = *info.first(3).map(&:chomp)
+ end
+
+ def hash
+ [@name, @artist, @album].hash
+ end
+
+ def eql?(other)
+ hash == other.hash
+ end
+end
+
+class Collection
+ include Enumerable
+
+ @songs = []
+
+ attr_reader :songs
+
+ def initialize(songs)
+ @songs = songs
+ end
+
+ def self.parse(input)
+ Collection.new input.lines.each_slice(4).map { |rows| Song.new rows }
+ end
+
+ def names
+ get_details :name
+ end
+
+ def artists
+ get_details :artist
+ end
+
+ def albums
+ get_details :album
+ end
+
+ def filter(criteria)
+ Collection.new @songs.select { |song| criteria.compiled[song] }
+ end
+
+ def each(&block)
+ @songs.each(&block)
+ end
+
+ def adjoin(other)
+ Collection.new (@songs + other.songs).uniq
+ end
+
+ private
+ def get_details(method)
+ @songs.map { |song| song.send method }.uniq
+ end
+end
+
+class Criteria
+ attr_reader :compiled
+
+ def initialize(compiled)
+ @compiled = compiled
+ end
+
+ def self.name(wanted_name)
+ Criteria.new ->(song) { song.name == wanted_name }
+ end
+
+ def self.artist(wanted_artist)
+ Criteria.new ->(song) { song.artist == wanted_artist }
+ end
+
+ def self.album(wanted_album)
+ Criteria.new ->(song) { song.album == wanted_album }
+ end
+
+ def !
+ Criteria.new ->(song) { not @compiled[song] }
+ end
+
+ def |(criteria)
+ Criteria.new ->(song) { @compiled[song] or criteria.compiled[song] }
+ end
+
+ def &(criteria)
+ Criteria.new ->(song) { @compiled[song] and criteria.compiled[song] }
+ end
+end

Иван обнови решението на 31.10.2012 12:03 (преди над 11 години)

class Song
attr_reader :name, :artist, :album
def initialize(info)
@name, @artist, @album = *info.first(3).map(&:chomp)
end
def hash
[@name, @artist, @album].hash
end
def eql?(other)
hash == other.hash
end
end
class Collection
include Enumerable
@songs = []
attr_reader :songs
def initialize(songs)
@songs = songs
end
def self.parse(input)
Collection.new input.lines.each_slice(4).map { |rows| Song.new rows }
end
def names
get_details :name
end
def artists
get_details :artist
end
def albums
get_details :album
end
def filter(criteria)
- Collection.new @songs.select { |song| criteria.compiled[song] }
+ Collection.new @songs.select { |song| criteria.accept? song }
end
def each(&block)
@songs.each(&block)
end
def adjoin(other)
Collection.new (@songs + other.songs).uniq
end
private
def get_details(method)
@songs.map { |song| song.send method }.uniq
end
end
class Criteria
- attr_reader :compiled
-
def initialize(compiled)
@compiled = compiled
end
+ def accept?(song)
+ @compiled[song]
+ end
+
def self.name(wanted_name)
Criteria.new ->(song) { song.name == wanted_name }
end
def self.artist(wanted_artist)
Criteria.new ->(song) { song.artist == wanted_artist }
end
def self.album(wanted_album)
Criteria.new ->(song) { song.album == wanted_album }
end
def !
- Criteria.new ->(song) { not @compiled[song] }
+ Criteria.new ->(song) { not accept? song }
end
def |(criteria)
- Criteria.new ->(song) { @compiled[song] or criteria.compiled[song] }
+ Criteria.new ->(song) { accept? song or criteria.accept? song }
end
def &(criteria)
- Criteria.new ->(song) { @compiled[song] and criteria.compiled[song] }
+ Criteria.new ->(song) { accept? song and criteria.accept? song }
end
-end
+end