Решение на Втора задача от Виктория Христова

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

Към профила на Виктория Христова

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 10 успешни тест(а)
  • 1 неуспешни тест(а)

Код

class Criteria
attr_accessor :condition
def initialize (condition)
@condition = condition
end
def Criteria.name(song_name)
Criteria.new(lambda { |song| song.name == song_name })
end
def Criteria.artist(artist_name)
Criteria.new(lambda { |song| song.artist == artist_name })
end
def Criteria.album(album_name)
Criteria.new(lambda { |song| song.album == album_name })
end
def |(other)
Criteria.new(lambda { |song| condition or other.condition })
end
def &(other)
Criteria.new(lambda { |song| condition.(song) and other.condition.(song) })
end
def !
Criteria.new(lambda { |song| !condition.(song) })
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(array_songs)
@songs = array_songs
end
def Collection.parse(text)
list = text.split("\n\n").map do |song|
name, artist, album = song.split("\n")[0..2]
Song.new(name, artist, album)
end
Collection.new(list)
end
def find_all(type)
map { |song| song.send :"#{type}" }.uniq
end
def artists
find_all("artist")
end
def albums
find_all("album")
end
def names
find_all("name")
end
def each
@songs.each { |elem| yield elem }
end
def filter (criteria)
Collection.new(select(&criteria.condition))
end
def adjoin (other_collection)
Collection.new(songs + other_collection.songs)
end
end
class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end

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

........F..

Failures:

  1) Collection supports a disjunction of filters
     Failure/Error: ]
       expected collection contained:  ["Live at Blues Alley", "Ten Summoner's Tales", "The Soul Cages"]
       actual collection contained:    ["A Love Supreme", "Live at Blues Alley", "Live at Blues Alley", "Mysterioso", "One", "Portrait in Jazz", "Ten", "Ten Summoner's Tales", "The Soul Cages", "Yield"]
       the extra elements were:        ["A Love Supreme", "Live at Blues Alley", "Mysterioso", "One", "Portrait in Jazz", "Ten", "Yield"]
     # /tmp/d20130203-23049-1mpzc5y/spec.rb:74: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.01028 seconds
11 examples, 1 failure

Failed examples:

rspec /tmp/d20130203-23049-1mpzc5y/spec.rb:68 # Collection supports a disjunction of filters

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

Виктория обнови решението на 29.10.2012 23:55 (преди около 12 години)

+class Criteria
+
+ attr_accessor :condition
+
+ def initialize (condition)
+ @condition = condition
+ end
+
+ def Criteria.name(song_name)
+ Criteria.new(lambda { |song| song.name == song_name })
+ end
+
+ def Criteria.artist(artist_name)
+ Criteria.new(lambda { |song| song.artist == artist_name })
+ end
+
+ def Criteria.album(album_name)
+ Criteria.new(lambda { |song| song.album == album_name })
+ end
+
+ def |(other)
+ Criteria.new(lambda { |song| condition or other.condition })
+ end
+
+ def &(other)
+ Criteria.new(lambda { |song| condition.(song) and other.condition.(song) })
+ end
+
+ def !
+ Criteria.new(lambda { |song| !condition.(song) })
+ end
+
+end
+
+class Collection
+
+ include Enumerable
+
+ attr_accessor :songs
+
+ def initialize(array_songs)
+ @songs = array_songs
+ end
+
+ def Collection.parse(text)
+ list = text.split("\n\n").map do |song|
+ name, artist, album = song.split("\n")[0..2]
+ Song.new(name, artist, album)
+ end
+ Collection.new(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 each
+ @songs.each { |elem| yield elem }
+ end
+
+ def filter (criteria)
+ Collection.new(select { |song| criteria.condition.(song) })
+ end
+
+ def adjoin (other_collection)
+ Collection.new(songs + other_collection.songs)
+ end
+
+end
+
+class Song
+
+ include Enumerable
+
+ attr_accessor :name, :artist, :album
+
+ def initialize(name, artist, album)
+ @name = name
+ @artist = artist
+ @album = album
+ end
+
+ def each
+ yield name
+ yield artist
+ yield album
+ end
+
+end

Добре, много хубаво. Няколко неща:

  • Защо Song е Enumerable?
  • Трите метода в Collection - artists, names и albums си приличат. Можеш ли да дефинираш четвърти, който да премахва повторението?
  • Опитай вместо да подаваш ламбда на Criteria да подаваш блок.

Виктория обнови решението на 30.10.2012 07:45 (преди около 12 години)

class Criteria
attr_accessor :condition
def initialize (condition)
@condition = condition
end
def Criteria.name(song_name)
Criteria.new(lambda { |song| song.name == song_name })
end
def Criteria.artist(artist_name)
Criteria.new(lambda { |song| song.artist == artist_name })
end
def Criteria.album(album_name)
Criteria.new(lambda { |song| song.album == album_name })
end
def |(other)
Criteria.new(lambda { |song| condition or other.condition })
end
def &(other)
Criteria.new(lambda { |song| condition.(song) and other.condition.(song) })
end
def !
Criteria.new(lambda { |song| !condition.(song) })
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(array_songs)
@songs = array_songs
end
def Collection.parse(text)
list = text.split("\n\n").map do |song|
name, artist, album = song.split("\n")[0..2]
Song.new(name, artist, album)
end
Collection.new(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 each
@songs.each { |elem| yield elem }
end
def filter (criteria)
- Collection.new(select { |song| criteria.condition.(song) })
+ Collection.new(select(&criteria.condition))
end
def adjoin (other_collection)
Collection.new(songs + other_collection.songs)
end
end
class Song
- include Enumerable
-
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
- end
-
- def each
- yield name
- yield artist
- yield album
end
end

Виктория обнови решението на 31.10.2012 09:21 (преди около 12 години)

class Criteria
attr_accessor :condition
def initialize (condition)
@condition = condition
end
def Criteria.name(song_name)
Criteria.new(lambda { |song| song.name == song_name })
end
def Criteria.artist(artist_name)
Criteria.new(lambda { |song| song.artist == artist_name })
end
def Criteria.album(album_name)
Criteria.new(lambda { |song| song.album == album_name })
end
def |(other)
Criteria.new(lambda { |song| condition or other.condition })
end
def &(other)
Criteria.new(lambda { |song| condition.(song) and other.condition.(song) })
end
def !
Criteria.new(lambda { |song| !condition.(song) })
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(array_songs)
@songs = array_songs
end
def Collection.parse(text)
list = text.split("\n\n").map do |song|
name, artist, album = song.split("\n")[0..2]
Song.new(name, artist, album)
end
Collection.new(list)
end
+ def find_all(type)
+ map { |song| song.send :"#{type}" }.uniq
+ end
+
def artists
- map { |song| song.artist }.uniq
+ find_all("artist")
end
def albums
- map { |song| song.album }.uniq
+ find_all("album")
end
def names
- map { |song| song.name }.uniq
+ find_all("name")
end
def each
@songs.each { |elem| yield elem }
end
def filter (criteria)
Collection.new(select(&criteria.condition))
end
def adjoin (other_collection)
Collection.new(songs + other_collection.songs)
end
end
class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end