Решение на Втора задача от Ангел Николов

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

Към профила на Ангел Николов

Резултати

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

Код

class Song
def initialize(name, artist, album)
@name = name.strip
@artist = artist.strip
@album = album.strip
end
attr_reader :name, :artist, :album
end
class Collection
def initialize(songs = [])
@songs = songs
end
attr_accessor :songs
def Collection.parse(text)
songs = []
text.lines.each_slice(3) { |s| songs << Song.new(s[0], s[1], s[2]) }
Collection.new songs
end
include Enumerable
def each(&block)
songs.each &block
end
def artists
@songs.map { |s| s.artist }.uniq
end
def albums
@songs.map { |s| s.album }.uniq
end
def names
@songs.map { |s| s.name }.uniq
end
def filter(criteria)
Collection.new songs.select { |song| criteria.fits song }
end
def adjoin(other)
Collection.new songs | other.songs
end
end
class Criteria
def initialize(checker_lambda)
@checker_lambda = checker_lambda
end
def Criteria.name(criterion) # Демонстрирам ползване на речник
Criteria.new lambda { |song| song.name == criterion }
end
def Criteria.artist(criterion)
Criteria.new lambda { |song| song.artist == criterion }
end
def Criteria.album(criterion)
Criteria.new lambda { |song| song.album == criterion }
end
def fits(song)
@checker_lambda.call song
end
def &(other)
Criteria.new lambda { |song| other.fits(song) & fits(song) }
end
def |(other)
Criteria.new lambda { |song| other.fits(song) | fits(song) }
end
def !
Criteria.new lambda { |song| !fits song }
end
end

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

FFFFFF..FFF

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:    ["", "A Love Supreme", "Autumn Leaves", "Come Away With Me", "Eva Cassidy", "Live at Blues Alley", "Mad About You", "Pearl Jam", "Sting", "Thelonious Monk", "Yield"]
       the missing elements were:      ["Bill Evans", "John Coltrane", "Norah Johnes"]
       the extra elements were:        ["", "A Love Supreme", "Autumn Leaves", "Come Away With Me", "Live at Blues Alley", "Mad About You", "Yield"]
     # /tmp/d20130203-23049-ttxn5/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:    ["", "Autumn Leaves", "Eva Cassidy", "Fields of Gold", "Jeremy", "John Coltrane", "One", "Pearl Jam", "Portrait in Jazz", "Ruby, My Dear", "The Soul Cages"]
       the missing elements were:      ["Acknowledgment", "Brain of J.F.K", "Come Away With Me", "Mad About You"]
       the extra elements were:        ["", "Eva Cassidy", "John Coltrane", "One", "Pearl Jam", "Portrait in Jazz", "The Soul Cages"]
     # /tmp/d20130203-23049-ttxn5/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:    ["", "Acknowledgment", "Bill Evans", "Brain of J.F.K", "Fields of Gold", "Live at Blues Alley", "Mysterioso", "Norah Johnes", "Sting", "Ten", "Ten Summoner's Tales"]
       the missing elements were:      ["A Love Supreme", "One", "Portrait in Jazz", "The Soul Cages", "Yield"]
       the extra elements were:        ["", "Acknowledgment", "Bill Evans", "Brain of J.F.K", "Fields of Gold", "Norah Johnes", "Sting"]
     # /tmp/d20130203-23049-ttxn5/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 filtered by song name
     Failure/Error: filtered.map(&:artist).should =~ ['Eva Cassidy', 'Sting']
       expected collection contained:  ["Eva Cassidy", "Sting"]
       actual collection contained:    ["Sting"]
       the missing elements were:      ["Eva Cassidy"]
     # /tmp/d20130203-23049-ttxn5/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)>'

  5) Collection can be filtered by song name
     Failure/Error: filtered.map(&:album).should =~ ['The Soul Cages', "Ten Summoner's Tales"]
       expected collection contained:  ["Ten Summoner's Tales", "The Soul Cages"]
       actual collection contained:    ["Ten Summoner's Tales"]
       the missing elements were:      ["The Soul Cages"]
     # /tmp/d20130203-23049-ttxn5/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)>'

  6) Collection can be filtered by album
     Failure/Error: filtered.map(&:name).should =~ ['Fields of Gold', 'Autumn Leaves']
       expected collection contained:  ["Autumn Leaves", "Fields of Gold"]
       actual collection contained:    ["Autumn Leaves"]
       the missing elements were:      ["Fields of Gold"]
     # /tmp/d20130203-23049-ttxn5/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)>'

  7) 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:    ["Ten Summoner's Tales"]
       the missing elements were:      ["Live at Blues Alley", "The Soul Cages"]
     # /tmp/d20130203-23049-ttxn5/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)>'

  8) Collection supports negation of filters
     Failure/Error: filtered.map(&:name).should eq ['Mad About You']
       
       expected: ["Mad About You"]
            got: []
       
       (compared using ==)
     # /tmp/d20130203-23049-ttxn5/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)>'

  9) Collection can be adjoined with another collection
     Failure/Error: adjoined.count.should eq 4
       
       expected: 4
            got: 2
       
       (compared using ==)
     # /tmp/d20130203-23049-ttxn5/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.01198 seconds
11 examples, 9 failures

Failed examples:

rspec /tmp/d20130203-23049-ttxn5/spec.rb:4 # Collection can find all the artists in the collection
rspec /tmp/d20130203-23049-ttxn5/spec.rb:16 # Collection can find all the names of all songs in the collection
rspec /tmp/d20130203-23049-ttxn5/spec.rb:29 # Collection can find all the albums in the collection
rspec /tmp/d20130203-23049-ttxn5/spec.rb:43 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-ttxn5/spec.rb:48 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-ttxn5/spec.rb:53 # Collection can be filtered by album
rspec /tmp/d20130203-23049-ttxn5/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-ttxn5/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-ttxn5/spec.rb:82 # Collection can be adjoined with another collection

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

Ангел обнови решението на 31.10.2012 16:26 (преди около 12 години)

+class Song
+ def initialize(name, artist, album)
+ @name = name
+ @artist = artist
+ @album = album
+ end
+
+ attr_reader :name, :artist, :album
+end
+
+class Collection
+ def initialize(songs = [])
+ @songs = songs
+ end
+
+ attr_accessor :songs
+
+ def Collection.parse(text)
+ songs = []
+ text.lines.each_slice(3) { |s| songs << Song.new(s[0], s[1], s[2]) }
+ Collection.new songs
+ end
+
+ include Enumerable
+
+ def each(&block)
+ songs.each &block
+ end
+
+ def artists
+ @songs.map { |s| s.artist }.uniq
+ end
+
+ def albums
+ @songs.map { |s| s.album }.uniq
+ end
+
+ def names
+ @songs.map { |s| s.name }.uniq
+ end
+
+ def filter(criteria)
+ new songs.select { |song| criteria.fits song }
+ end
+
+ def adjoin(other)
+ new songs | other.songs
+ end
+end
+
+class Criteria
+ def initialize(checker_lambda)
+ @checker_lambda = checker_lambda
+ end
+
+ def Criteria.name(criterion) # Демонстрирам ползване на речник
+ new lambda { |song| song.name == criterion }
+ end
+
+ def Criteria.artist(criterion)
+ new lambda { |song| song.artist == criterion }
+ end
+
+ def Criteria.album(criterion)
+ new lambda { |song| song.album == criterion }
+ end
+
+ def fits(song)
+ @checker_lambda.call song
+ end
+
+ def &(other)
+ new lambda { |song| other.fits(song) & fits(song) }
+ end
+
+ def |(other)
+ new lambda { |song| other.fits(song) | fits(song) }
+ end
+
+ def !
+ new lambda { |song| !fits song }
+ end
+end

Ангел обнови решението на 31.10.2012 16:54 (преди около 12 години)

class Song
def initialize(name, artist, album)
- @name = name
- @artist = artist
- @album = album
+ @name = name.strip
+ @artist = artist.strip
+ @album = album.strip
end
attr_reader :name, :artist, :album
end
class Collection
def initialize(songs = [])
@songs = songs
end
attr_accessor :songs
def Collection.parse(text)
songs = []
text.lines.each_slice(3) { |s| songs << Song.new(s[0], s[1], s[2]) }
Collection.new songs
end
include Enumerable
def each(&block)
songs.each &block
end
def artists
@songs.map { |s| s.artist }.uniq
end
def albums
@songs.map { |s| s.album }.uniq
end
def names
@songs.map { |s| s.name }.uniq
end
def filter(criteria)
- new songs.select { |song| criteria.fits song }
+ Collection.new songs.select { |song| criteria.fits song }
end
def adjoin(other)
- new songs | other.songs
+ Collection.new songs | other.songs
end
end
class Criteria
def initialize(checker_lambda)
@checker_lambda = checker_lambda
end
def Criteria.name(criterion) # Демонстрирам ползване на речник
- new lambda { |song| song.name == criterion }
+ Criteria.new lambda { |song| song.name == criterion }
end
def Criteria.artist(criterion)
- new lambda { |song| song.artist == criterion }
+ Criteria.new lambda { |song| song.artist == criterion }
end
def Criteria.album(criterion)
- new lambda { |song| song.album == criterion }
+ Criteria.new lambda { |song| song.album == criterion }
end
def fits(song)
@checker_lambda.call song
end
def &(other)
- new lambda { |song| other.fits(song) & fits(song) }
+ Criteria.new lambda { |song| other.fits(song) & fits(song) }
end
def |(other)
- new lambda { |song| other.fits(song) | fits(song) }
+ Criteria.new lambda { |song| other.fits(song) | fits(song) }
end
def !
- new lambda { |song| !fits song }
+ Criteria.new lambda { |song| !fits song }
end
end