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

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

Към профила на Теодор Николов

Резултати

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

Код

class Song
attr_accessor :name, :artist, :genre, :subgenre, :tags
def initialize
@name = ''
@artist = ''
@genre = ''
@subgenre = ''
@tags = []
end
def find_by_tags(tags)
if tags.kind_of?(Array)
tags.all? do |tag|
@tags.include?(tag) or
(tag.end_with?("!") and !@tags.include?(tag.chop))
end
else
@tags.include?(tags)
end
end
def find(criteria)
criteria.all? do |key, value|
if key.to_s == "tags"
find_by_tags(value)
else
self.method(key).call == value
end
end
end
end
class Collection
attr_accessor :collection
def initialize(songs, tags = {} )
lines = songs.split("\n")
@collection = []
@params = []
lines.each_with_index do |line, index|
@params = line.split('.').map do |word|
word.strip
end
@params[2] = @params[2].split(",") if @params[2]
@params[3] = @params[3].split(",").each {|word| word.strip!} if @params[3]
@collection[index] = add_song_to_collection(@params, tags)
end
end
def add_song_to_collection(song_params, tags = {})
song = Song.new
song.name = song_params[0]
song.artist = song_params[1]
song.genre = song_params[2][0]
song.subgenre = song_params[2][1]
if song.subgenre != nil then song.subgenre.strip! end
song.tags = song_params[3] || []
tags[song.artist].each{|word| song.tags << word.strip!} if tags[song.artist]
song
end
def find(criteria = nil)
if criteria != nil
@collection.select { |song| song.find(criteria) }
else
@collection
end
end
end

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

...FF..FFFF.

Failures:

  1) Collection uses the genre and subgenre as tags
     Failure/Error: song(name: 'Miles Runs the Voodoo Down').tags.should include('jazz', 'fusion')
       expected ["weird"] to include "jazz" and "fusion"
       Diff:
       @@ -1,2 +1,2 @@
       -jazz
       +["weird"]
     # /tmp/d20111115-13548-12xdrj/spec.rb:49: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 songs by tag
     Failure/Error: songs(tags: 'baroque').map(&:name).should =~ ['Toccata e Fuga', 'Goldberg Variations']
       expected collection contained:  ["Goldberg Variations", "Toccata e Fuga"]
       actual collection contained:    []
       the missing elements were:      ["Goldberg Variations", "Toccata e Fuga"]
     # /tmp/d20111115-13548-12xdrj/spec.rb:53: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 filter songs by a lambda
     Failure/Error: collection.find(options)
     NameError:
       undefined method `filter' for class `Song'
     # /tmp/d20111115-13548-12xdrj/solution.rb:27:in `method'
     # /tmp/d20111115-13548-12xdrj/solution.rb:27:in `block in find'
     # /tmp/d20111115-13548-12xdrj/solution.rb:23:in `each'
     # /tmp/d20111115-13548-12xdrj/solution.rb:23:in `all?'
     # /tmp/d20111115-13548-12xdrj/solution.rb:23:in `find'
     # /tmp/d20111115-13548-12xdrj/solution.rb:63:in `block in find'
     # /tmp/d20111115-13548-12xdrj/solution.rb:63:in `select'
     # /tmp/d20111115-13548-12xdrj/solution.rb:63:in `find'
     # /tmp/d20111115-13548-12xdrj/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-12xdrj/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)>'

  4) Collection adds the artist tags to the songs
     Failure/Error: songs(tags: 'polyphone').map(&:name).should =~ ['Toccata e Fuga', 'Goldberg Variations']
       expected collection contained:  ["Goldberg Variations", "Toccata e Fuga"]
       actual collection contained:    []
       the missing elements were:      ["Goldberg Variations", "Toccata e Fuga"]
     # /tmp/d20111115-13548-12xdrj/spec.rb:69: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 allows multiple criteria
     Failure/Error: songs(name: "'Round Midnight", tags: 'bebop').map(&:artist).should eq ['Thelonious Monk']
       
       expected: ["Thelonious Monk"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Thelonious Monk"]
       +[]
     # /tmp/d20111115-13548-12xdrj/spec.rb:73: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 allows all criteria
     Failure/Error: ).map(&:artist).should eq ['Thelonious Monk']
       
       expected: ["Thelonious Monk"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Thelonious Monk"]
       +[]
     # /tmp/d20111115-13548-12xdrj/spec.rb:82: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.54655 seconds
12 examples, 6 failures

Failed examples:

rspec /tmp/d20111115-13548-12xdrj/spec.rb:48 # Collection uses the genre and subgenre as tags
rspec /tmp/d20111115-13548-12xdrj/spec.rb:52 # Collection can find songs by tag
rspec /tmp/d20111115-13548-12xdrj/spec.rb:64 # Collection can filter songs by a lambda
rspec /tmp/d20111115-13548-12xdrj/spec.rb:68 # Collection adds the artist tags to the songs
rspec /tmp/d20111115-13548-12xdrj/spec.rb:72 # Collection allows multiple criteria
rspec /tmp/d20111115-13548-12xdrj/spec.rb:76 # Collection allows all criteria

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

Теодор обнови решението на 31.10.2011 02:23 (преди над 12 години)

+class Song
+ attr_accessor :name, :artist, :genre, :subgenre, :tags
+ def initialize
+ @name = ''
+ @artist = ''
+ @genre = ''
+ @subgenre = ''
+ @tags = []
+ end
+
+ def find_by_tags(tags)
+ if tags.kind_of?(Array)
+ tags.all? do |tag|
+ @tags.include?(tag) or
+ (tag.end_with?("!") and !@tags.include?(tag.chop))
+ end
+ else
+ @tags.include?(tags)
+ end
+ end
+
+ def find(criteria)
+ criteria.all? do |key, value|
+ if key.to_s == "tags"
+ find_by_tags(value)
+ else
+ self.method(key).call == value
+ end
+ end
+ end
+end
+
+class Collection
+ attr_accessor :collection
+ def initialize(songs, tags = {} )
+ lines = songs.split("\n")
+ @collection = []
+ @params = []
+ lines.each_with_index do |line, index|
+ @params = line.split('.').map do |word|
+ word.strip
+ end
+ @params[2] = @params[2].split(",") if @params[2]
+ @params[3] = @params[3].split(",").each {|word| word.strip!} if @params[3]
+ @collection[index] = add_song_to_collection(@params, tags)
+ end
+ end
+
+ def add_song_to_collection(song_params, tags = {})
+ song = Song.new
+ song.name = song_params[0]
+ song.artist = song_params[1]
+ song.genre = song_params[2][0]
+ song.subgenre = song_params[2][1]
+ if song.subgenre != nil then song.subgenre.strip! end
+ song.tags = song_params[3] || []
+ tags[song.artist].each{|word| song.tags << word.strip!} if tags[song.artist]
+ song
+ end
+
+ def find(criteria = nil)
+ if criteria != nil
+ @collection.select { |song| song.find(criteria) }
+ else
+ @collection
+ end
+ end
+end