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

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

Към профила на Валентин Гелински

Резултати

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

Код

class Collection
include Enumerable
def Collection.parse(text)
unparsed_songs = []
text.each_line("\n\n").each do |unparsed_song|
unparsed_songs << unparsed_song.rstrip
end
Collection.parse_songs unparsed_songs
end
def Collection.parse_songs(unparsed_songs)
songs = []
unparsed_songs.each do |unparsed_song|
songs << Song.parse(unparsed_song)
end
Collection.new songs
end
attr_accessor :artists, :albums, :names
def initialize(songs)
@songs = songs
@artists = songs.map{ |song| song.artist }
@albums = songs.map{ |song| song.album }
@names = songs.map{ |song| song.name }
end
def each
@songs.each{ |song| yield song}
end
def filter(criteria)
Collection.new @songs.select{ |song| criteria.fits song }
end
def addjoin(other)
zip(other).flatten.compact
end
end
class Song
attr_accessor :name, :artist, :album
def Song.parse(unparsed_song)
attributes = []
unparsed_song.each_line("\n").each do |attribute|
attributes << attribute.rstrip
end
Song.new attributes[0], attributes[1], attributes[2]
end
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Criteria
attr_accessor :attribute
def Criteria.name(name)
CriteriaByName.new name
end
def Criteria.artist(artist)
CriteriaByArtist.new artist
end
def Criteria.album(album)
CriteriaByAlbum.new album
end
def &(other)
CriteriaAnd.new self, other
end
def |(other)
CriteriaOr.new self, other
end
def !
CriteriaNot.new self
end
def initialize(attribute)
@attribute = attribute
end
end
class CriteriaByName < Criteria
def fits(song)
song.name == @attribute
end
end
class CriteriaByArtist < Criteria
def fits(song)
song.artist == @attribute
end
end
class CriteriaByAlbum < Criteria
def fits(song)
song.album == @attribute
end
end
class CriteriaAnd < Criteria
def initialize(criteria1, criteria2)
@criteria1 = criteria1
@criteria2 = criteria2
end
def fits(song)
@criteria1.fits(song) && @criteria2.fits(song)
end
end
class CriteriaOr < Criteria
def initialize(criteria1, criteria2)
@criteria1 = criteria1
@criteria2 = criteria2
end
def fits(song)
@criteria1.fits(song) || @criteria2.fits(song)
end
end
class CriteriaNot < Criteria
def initialize(criteria)
@criteria = criteria
end
def fits(song)
!@criteria.fits(song)
end
end

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

FFF.......F

Failures:

  1) Collection can find all the artists in the collection
     Failure/Error: ]
       expected collection contained:  ["Bill Evans", "Eva Cassidy", "John Coltrane", "Norah Johnes", "Pearl Jam", "Sting", "Thelonious Monk"]
       actual collection contained:    ["Bill Evans", "Eva Cassidy", "Eva Cassidy", "John Coltrane", "Norah Johnes", "Pearl Jam", "Pearl Jam", "Sting", "Sting", "Thelonious Monk"]
       the extra elements were:        ["Eva Cassidy", "Pearl Jam", "Sting"]
     # /tmp/d20130203-23049-vie1cs/spec.rb:13: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 find all the names of all songs in the collection
     Failure/Error: ]
       expected collection contained:  ["Acknowledgment", "Autumn Leaves", "Brain of J.F.K", "Come Away With Me", "Fields of Gold", "Jeremy", "Mad About You", "Ruby, My Dear"]
       actual collection contained:    ["Acknowledgment", "Autumn Leaves", "Autumn Leaves", "Brain of J.F.K", "Come Away With Me", "Fields of Gold", "Fields of Gold", "Jeremy", "Mad About You", "Ruby, My Dear"]
       the extra elements were:        ["Autumn Leaves", "Fields of Gold"]
     # /tmp/d20130203-23049-vie1cs/spec.rb:26: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 find all the albums in the collection
     Failure/Error: ]
       expected collection contained:  ["A Love Supreme", "Live at Blues Alley", "Mysterioso", "One", "Portrait in Jazz", "Ten", "Ten Summoner's Tales", "The Soul Cages", "Yield"]
       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:        ["Live at Blues Alley"]
     # /tmp/d20130203-23049-vie1cs/spec.rb:40: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 be adjoined with another collection
     Failure/Error: adjoined = sting.adjoin(eva)
     NoMethodError:
       undefined method `adjoin' for #<Collection:0x9736c08>
     # /tmp/d20130203-23049-vie1cs/spec.rb:85: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.01153 seconds
11 examples, 4 failures

Failed examples:

rspec /tmp/d20130203-23049-vie1cs/spec.rb:4 # Collection can find all the artists in the collection
rspec /tmp/d20130203-23049-vie1cs/spec.rb:16 # Collection can find all the names of all songs in the collection
rspec /tmp/d20130203-23049-vie1cs/spec.rb:29 # Collection can find all the albums in the collection
rspec /tmp/d20130203-23049-vie1cs/spec.rb:82 # Collection can be adjoined with another collection

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

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

+class Collection
+ include Enumerable
+
+ def Collection.parse(text)
+ unparsed_songs = []
+ text.each_line("\n\n").each do |unparsed_song|
+ unparsed_songs << unparsed_song.rstrip
+ end
+ Collection.parse_songs unparsed_songs
+ end
+
+ def Collection.parse_songs(unparsed_songs)
+ songs = []
+ unparsed_songs.each do |unparsed_song|
+ songs << Song.parse(unparsed_song)
+ end
+ Collection.new songs
+ end
+
+ attr_accessor :artists, :albums, :names
+
+ def initialize(songs)
+ @songs = songs
+ @artists = songs.map{ |song| song.artist }
+ @albums = songs.map{ |song| song.album }
+ @names = songs.map{ |song| song.name }
+ end
+
+ def each
+ @songs.each{ |song| yield song}
+ end
+
+ def filter(criteria)
+ Collection.new @songs.select{ |song| criteria.fits song }
+ end
+
+ def addjoin(other)
+ zip(other).flatten.compact
+ end
+end
+
+class Song
+
+ attr_accessor :name, :artist, :album
+
+ def Song.parse(unparsed_song)
+ attributes = []
+ unparsed_song.each_line("\n").each do |attribute|
+ attributes << attribute.rstrip
+ end
+
+ Song.new attributes[0], attributes[1], attributes[2]
+ end
+
+ def initialize(name, artist, album)
+ @name = name
+ @artist = artist
+ @album = album
+ end
+end
+
+class Criteria
+ attr_accessor :attribute
+
+ def Criteria.name(name)
+ CriteriaByName.new name
+ end
+
+ def Criteria.artist(artist)
+ CriteriaByArtist.new artist
+ end
+
+ def Criteria.album(album)
+ CriteriaByAlbum.new album
+ end
+
+ def &(other)
+ CriteriaAnd.new self, other
+ end
+
+ def |(other)
+ CriteriaOr.new self, other
+ end
+
+ def !
+ CriteriaNot.new self
+ end
+
+ def initialize(attribute)
+ @attribute = attribute
+ end
+end
+
+class CriteriaByName < Criteria
+
+ def fits(song)
+ song.name == @attribute
+ end
+end
+
+class CriteriaByArtist < Criteria
+
+ def fits(song)
+ song.artist == @attribute
+ end
+end
+
+class CriteriaByAlbum < Criteria
+
+ def fits(song)
+ song.album == @attribute
+ end
+end
+
+class CriteriaAnd < Criteria
+
+ def initialize(criteria1, criteria2)
+ @criteria1 = criteria1
+ @criteria2 = criteria2
+ end
+
+ def fits(song)
+ @criteria1.fits(song) && @criteria2.fits(song)
+ end
+end
+
+class CriteriaOr < Criteria
+
+ def initialize(criteria1, criteria2)
+ @criteria1 = criteria1
+ @criteria2 = criteria2
+ end
+
+ def fits(song)
+ @criteria1.fits(song) || @criteria2.fits(song)
+ end
+end
+
+class CriteriaNot < Criteria
+
+ def initialize(criteria)
+ @criteria = criteria
+ end
+
+ def fits(song)
+ !@criteria.fits(song)
+ end
+end