Решение на Втора задача от Камелия Пандаклиева

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

Към профила на Камелия Пандаклиева

Резултати

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

Код

class Collection
include Enumerable
attr_reader :songs
def initialize(songs = [])
@songs = songs
end
def each
songs.each { |song| yield song }
end
def self.parse(songs_string)
songs_list = songs_string.split("\n\n").map do |song_string|
name, artist, album = song_string.split("\n")
Song.new name, artist, album
end
new songs_list
end
def artists
map { |song| song.artist }.uniq
end
def albums
map { |song| song.album }.uniq
end
def names
map { |song| song.name }.uniq
end
def adjoin(other)
Collection.new((songs + other.songs).uniq)
end
def filter(criteria)
Collection.new select { |song| criteria.checkup.call song }
end
end
class Song
attr_reader :artist, :album, :name
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
module Criteria
def self.artist(artist)
Filter.new { |song| song.artist == artist }
end
def self.album(album)
Filter.new { |song| song.album == album }
end
def self.name(name)
Filter.new { |song| song.name == name }
end
end
class Filter
attr_reader :checkup
def initialize(&checkup)
@checkup = checkup
end
def &(other)
Filter.new { |song| checkup.call song and other.checkup.call song }
end
def |(other)
Filter.new { |song| checkup.call song or other.checkup.call song }
end
def !
Filter.new { |song| not checkup.call song }
end
end

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

...........

Finished in 0.01005 seconds
11 examples, 0 failures

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

Камелия обнови решението на 30.10.2012 01:48 (преди около 12 години)

+class Collection
+ include Enumerable
+
+ attr_accessor :songs
+
+ def initialize(songs = [])
+ @songs = songs
+ end
+
+ def each
+ @songs.each { |song| yield song }
+ end
+
+ def self.parse(songs_string)
+ songs_list = songs_string.split("\n\n").map do |song_string|
+ Song.new(song_string.split("\n"))
+ end
+ Collection.new(songs_list)
+ end
+
+ def artists
+ map { |song| song.artist }.uniq
+ end
+
+ def albums
+ map { |song| song.album }.uniq
+ end
+
+ def names
+ map { |song| song.name }.uniq
+ end
+
+ def adjoin(other)
+ result = Collection.new
+ each { |song| result.songs << song }
+ other.each { |song| result.songs << song }
+ result
+ end
+
+ def filter(criteria)
+ Collection.new(select { |song| criteria.criteria.(song) })
+ end
+end
+
+class Song
+ attr_accessor :artist, :album, :name
+
+ def initialize(song_data)
+ @artist = song_data[1]
+ @album = song_data[2]
+ @name = song_data[0]
+ end
+end
+
+class Criteria
+ attr_accessor :criteria
+
+ def initialize(criteria)
+ @criteria = criteria
+ end
+
+ def self.artist(artist)
+ new lambda { |song| song.artist == artist }
+ end
+
+ def self.album(album)
+ new lambda { |song| song.album == album }
+ end
+
+ def self.name(name)
+ new lambda { |song| song.name == name }
+ end
+
+ def &(other)
+ Criteria.new lambda { |song| criteria.(song) and other.criteria.(song) }
+ end
+
+ def |(other)
+ Criteria.new lambda { |song| criteria.(song) or other.criteria.(song) }
+ end
+
+ def !
+ Criteria.new lambda { |song| not criteria.(song) }
+ end
+end

Камелия обнови решението на 30.10.2012 12:56 (преди около 12 години)

class Collection
include Enumerable
- attr_accessor :songs
+ attr_reader :songs
def initialize(songs = [])
@songs = songs
end
def each
@songs.each { |song| yield song }
end
def self.parse(songs_string)
songs_list = songs_string.split("\n\n").map do |song_string|
Song.new(song_string.split("\n"))
end
Collection.new(songs_list)
end
def artists
map { |song| song.artist }.uniq
end
def albums
map { |song| song.album }.uniq
end
def names
map { |song| song.name }.uniq
end
def adjoin(other)
- result = Collection.new
- each { |song| result.songs << song }
- other.each { |song| result.songs << song }
- result
+ Collection.new(songs + other.songs)
end
def filter(criteria)
- Collection.new(select { |song| criteria.criteria.(song) })
+ Collection.new(select { |song| criteria.check.(song) })
end
end
class Song
- attr_accessor :artist, :album, :name
+ attr_reader :artist, :album, :name
def initialize(song_data)
@artist = song_data[1]
@album = song_data[2]
@name = song_data[0]
end
end
class Criteria
- attr_accessor :criteria
-
- def initialize(criteria)
- @criteria = criteria
- end
-
def self.artist(artist)
- new lambda { |song| song.artist == artist }
+ Filter.new lambda { |song| song.artist == artist }
end
def self.album(album)
- new lambda { |song| song.album == album }
+ Filter.new lambda { |song| song.album == album }
end
def self.name(name)
- new lambda { |song| song.name == name }
+ Filter.new lambda { |song| song.name == name }
end
+end
+class Filter
+ attr_reader :check
+
+ def initialize(check)
+ @check = check
+ end
+
def &(other)
- Criteria.new lambda { |song| criteria.(song) and other.criteria.(song) }
+ Filter.new lambda { |song| check.(song) and other.check.(song) }
end
def |(other)
- Criteria.new lambda { |song| criteria.(song) or other.criteria.(song) }
+ Filter.new lambda { |song| check.(song) or other.check.(song) }
end
def !
- Criteria.new lambda { |song| not criteria.(song) }
+ Filter.new lambda { |song| not check.(song) }
end
end

Камелия обнови решението на 30.10.2012 13:49 (преди около 12 години)

class Collection
include Enumerable
attr_reader :songs
def initialize(songs = [])
@songs = songs
end
def each
@songs.each { |song| yield song }
end
def self.parse(songs_string)
songs_list = songs_string.split("\n\n").map do |song_string|
- Song.new(song_string.split("\n"))
+ Song.new song_string.split("\n")
end
- Collection.new(songs_list)
+ new songs_list
end
def artists
map { |song| song.artist }.uniq
end
def albums
map { |song| song.album }.uniq
end
def names
map { |song| song.name }.uniq
end
def adjoin(other)
- Collection.new(songs + other.songs)
+ Collection.new songs + other.songs
end
def filter(criteria)
- Collection.new(select { |song| criteria.check.(song) })
+ Collection.new select { |song| criteria.checkup.call song }
end
end
class Song
attr_reader :artist, :album, :name
def initialize(song_data)
@artist = song_data[1]
@album = song_data[2]
@name = song_data[0]
end
end
-class Criteria
+module Criteria
def self.artist(artist)
Filter.new lambda { |song| song.artist == artist }
end
def self.album(album)
Filter.new lambda { |song| song.album == album }
end
def self.name(name)
Filter.new lambda { |song| song.name == name }
end
end
class Filter
- attr_reader :check
+ attr_reader :checkup
- def initialize(check)
- @check = check
+ def initialize(checkup)
+ @checkup = checkup
end
def &(other)
- Filter.new lambda { |song| check.(song) and other.check.(song) }
+ Filter.new lambda { |song| checkup.call song and other.checkup.call song }
end
def |(other)
- Filter.new lambda { |song| check.(song) or other.check.(song) }
+ Filter.new lambda { |song| checkup.call song or other.checkup.call song }
end
def !
- Filter.new lambda { |song| not check.(song) }
+ Filter.new lambda { |song| not checkup.call song }
end
end

Камелия обнови решението на 30.10.2012 14:04 (преди около 12 години)

class Collection
include Enumerable
attr_reader :songs
def initialize(songs = [])
@songs = songs
end
def each
@songs.each { |song| yield song }
end
def self.parse(songs_string)
songs_list = songs_string.split("\n\n").map do |song_string|
Song.new song_string.split("\n")
end
new songs_list
end
def artists
map { |song| song.artist }.uniq
end
def albums
map { |song| song.album }.uniq
end
def names
map { |song| song.name }.uniq
end
def adjoin(other)
- Collection.new songs + other.songs
+ Collection.new (songs + other.songs).uniq
end
def filter(criteria)
Collection.new select { |song| criteria.checkup.call song }
end
end
class Song
attr_reader :artist, :album, :name
def initialize(song_data)
@artist = song_data[1]
@album = song_data[2]
@name = song_data[0]
end
end
module Criteria
def self.artist(artist)
Filter.new lambda { |song| song.artist == artist }
end
def self.album(album)
Filter.new lambda { |song| song.album == album }
end
def self.name(name)
Filter.new lambda { |song| song.name == name }
end
end
class Filter
attr_reader :checkup
def initialize(checkup)
@checkup = checkup
end
def &(other)
Filter.new lambda { |song| checkup.call song and other.checkup.call song }
end
def |(other)
Filter.new lambda { |song| checkup.call song or other.checkup.call song }
end
def !
Filter.new lambda { |song| not checkup.call song }
end
end

Камелия обнови решението на 31.10.2012 00:17 (преди около 12 години)

class Collection
include Enumerable
attr_reader :songs
def initialize(songs = [])
@songs = songs
end
def each
- @songs.each { |song| yield song }
+ songs.each { |song| yield song }
end
def self.parse(songs_string)
songs_list = songs_string.split("\n\n").map do |song_string|
- Song.new song_string.split("\n")
+ Song.new song_string
end
new songs_list
end
def artists
map { |song| song.artist }.uniq
end
def albums
map { |song| song.album }.uniq
end
def names
map { |song| song.name }.uniq
end
def adjoin(other)
Collection.new (songs + other.songs).uniq
end
def filter(criteria)
Collection.new select { |song| criteria.checkup.call song }
end
end
class Song
attr_reader :artist, :album, :name
- def initialize(song_data)
- @artist = song_data[1]
- @album = song_data[2]
- @name = song_data[0]
+ def initialize(song_string)
+ @name, @artist, @album = song_string.split("\n")
end
end
module Criteria
def self.artist(artist)
Filter.new lambda { |song| song.artist == artist }
end
def self.album(album)
Filter.new lambda { |song| song.album == album }
end
def self.name(name)
Filter.new lambda { |song| song.name == name }
end
end
class Filter
attr_reader :checkup
def initialize(checkup)
@checkup = checkup
end
def &(other)
Filter.new lambda { |song| checkup.call song and other.checkup.call song }
end
def |(other)
Filter.new lambda { |song| checkup.call song or other.checkup.call song }
end
def !
Filter.new lambda { |song| not checkup.call song }
end
end
  • На 34ти ред има warning. Напиши го като Collection.new((songs + other.songs).uniq), понеже Ruby не знае какво имаш предвид.
  • Лоша идея е да подаваш низ на конструктора на песен. Вместо това подай трите компонента и остави парсенето да става навън.
  • Можеш да подаваш блок вместо ламбда.

Камелия обнови решението на 31.10.2012 08:38 (преди около 12 години)

class Collection
include Enumerable
attr_reader :songs
def initialize(songs = [])
@songs = songs
end
def each
songs.each { |song| yield song }
end
def self.parse(songs_string)
songs_list = songs_string.split("\n\n").map do |song_string|
- Song.new song_string
+ name, artist, album = song_string.split("\n")
+ Song.new name, artist, album
end
new songs_list
end
def artists
map { |song| song.artist }.uniq
end
def albums
map { |song| song.album }.uniq
end
def names
map { |song| song.name }.uniq
end
def adjoin(other)
- Collection.new (songs + other.songs).uniq
+ Collection.new((songs + other.songs).uniq)
end
def filter(criteria)
Collection.new select { |song| criteria.checkup.call song }
end
end
class Song
attr_reader :artist, :album, :name
- def initialize(song_string)
- @name, @artist, @album = song_string.split("\n")
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
end
end
module Criteria
def self.artist(artist)
- Filter.new lambda { |song| song.artist == artist }
+ Filter.new { |song| song.artist == artist }
end
def self.album(album)
- Filter.new lambda { |song| song.album == album }
+ Filter.new { |song| song.album == album }
end
def self.name(name)
- Filter.new lambda { |song| song.name == name }
+ Filter.new { |song| song.name == name }
end
end
class Filter
attr_reader :checkup
- def initialize(checkup)
+ def initialize(&checkup)
@checkup = checkup
end
def &(other)
- Filter.new lambda { |song| checkup.call song and other.checkup.call song }
+ Filter.new { |song| checkup.call song and other.checkup.call song }
end
def |(other)
- Filter.new lambda { |song| checkup.call song or other.checkup.call song }
+ Filter.new { |song| checkup.call song or other.checkup.call song }
end
def !
- Filter.new lambda { |song| not checkup.call song }
+ Filter.new { |song| not checkup.call song }
end
end