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

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

Към профила на Адриан Кателиев

Резултати

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

Код

class Collection
include Enumerable
attr_accessor :collection
def initialize(songs)
@collection = songs
end
def Collection.parse(content)
songs = []
content=content.split("\n\n").map { |song| song.split("\n") }
content.map { |name,artist,album| songs << Song.new(name,artist,album) }
Collection.new songs
end
def each
@collection.map { |c| yield c }
end
def artists()
@collection.map { |song| song.artist }.uniq
end
def albums()
@collection.map { |song| song.album }.uniq
end
def names()
@collection.map { |song| song.name }.uniq
end
def filter(criteria)
result=[]
@collection.select do |song|
end
result
end
end
class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name,@artist,@album=name,artist,album
end
def artist()
@artist
end
def album()
@album
end
def name()
@name
end
end
class Criteria
attr_accessor :key,:value
def initialize(key,value)
@key=[key]
@value=[value]
end
def Criteria.name(name)
Criteria.new('name',name)
end
def Criteria.artist(artist)
Criteria.new('artist',artist)
end
def Criteria.album(album)
Criteria.new('album',album)
end
def &(other)
@key<<other.key
@value<<other.value
self
end
def |(other)
end
def !(other)
end
end

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

...FFF.FFFF

Failures:

  1) 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:    []
       the missing elements were:      ["Eva Cassidy", "Sting"]
     # /tmp/d20130203-23049-qa5ga1/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)>'

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

  3) 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:    []
       the missing elements were:      ["Autumn Leaves", "Fields of Gold"]
     # /tmp/d20130203-23049-qa5ga1/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)>'

  4) Collection supports a conjuction of filters
     Failure/Error: filtered.map(&:album).should eq ["Ten Summoner's Tales"]
       
       expected: ["Ten Summoner's Tales"]
            got: []
       
       (compared using ==)
     # /tmp/d20130203-23049-qa5ga1/spec.rb:65: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 supports a disjunction of filters
     Failure/Error: ]
       expected collection contained:  ["Live at Blues Alley", "Ten Summoner's Tales", "The Soul Cages"]
       actual collection contained:    []
       the missing elements were:      ["Live at Blues Alley", "Ten Summoner's Tales", "The Soul Cages"]
     # /tmp/d20130203-23049-qa5ga1/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)>'

  6) Collection supports negation of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & !Criteria.name('Fields of Gold')
     ArgumentError:
       wrong number of arguments (0 for 1)
     # /tmp/d20130203-23049-qa5ga1/solution.rb:87:in `!'
     # /tmp/d20130203-23049-qa5ga1/spec.rb:78: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 can be adjoined with another collection
     Failure/Error: adjoined = sting.adjoin(eva)
     NoMethodError:
       undefined method `adjoin' for []:Array
     # /tmp/d20130203-23049-qa5ga1/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.02397 seconds
11 examples, 7 failures

Failed examples:

rspec /tmp/d20130203-23049-qa5ga1/spec.rb:43 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-qa5ga1/spec.rb:48 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-qa5ga1/spec.rb:53 # Collection can be filtered by album
rspec /tmp/d20130203-23049-qa5ga1/spec.rb:63 # Collection supports a conjuction of filters
rspec /tmp/d20130203-23049-qa5ga1/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-qa5ga1/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-qa5ga1/spec.rb:82 # Collection can be adjoined with another collection

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

Адриан обнови решението на 31.10.2012 16:42 (преди над 11 години)

+class Collection
+ include Enumerable
+ attr_accessor :collection
+
+ def initialize(songs)
+ @collection = songs
+ end
+
+ def Collection.parse(content)
+ songs = []
+ content=content.split("\n\n").map { |song| song.split("\n") }
+ content.map { |name,artist,album| songs << Song.new(name,artist,album) }
+ Collection.new songs
+ end
+
+ def each
+ @collection.map { |c| yield c }
+ end
+
+ def artists()
+ @collection.map { |song| song.artist }.uniq
+ end
+
+ def albums()
+ @collection.map { |song| song.album }.uniq
+ end
+
+ def names()
+ @collection.map { |song| song.name }.uniq
+ end
+
+ def filter(criteria)
+ result=[]
+ @collection.select do |song|
+ end
+ result
+ end
+end
+
+class Song
+ attr_accessor :name, :artist, :album
+ def initialize(name, artist, album)
+ @name,@artist,@album=name,artist,album
+ end
+
+ def artist()
+ @artist
+ end
+
+ def album()
+ @album
+ end
+
+ def name()
+ @name
+ end
+end
+
+class Criteria
+ attr_accessor :key,:value
+ def initialize(key,value)
+ @key=[key]
+ @value=[value]
+ end
+
+ def Criteria.name(name)
+ Criteria.new('name',name)
+ end
+
+ def Criteria.artist(artist)
+ Criteria.new('artist',artist)
+ end
+
+ def Criteria.album(album)
+ Criteria.new('album',album)
+ end
+
+ def &(other)
+ @key<<other.key
+ @value<<other.value
+ self
+ end
+
+ def |(other)
+ end
+
+ def !(other)
+ end
+end