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

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

Към профила на Николай Стоицев

Резултати

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

Код

class Collection
def initialize(songs_as_string, artist_tags)
@storage = []
songs_as_string.split("\n").each{ |song|
@storage << Song.new(song)
}
end
def find(criteria)
result = @storage
criteria.each_pair do |key, value|
if key == :tag
result = find_by_tag(result, value)
else
result = result.select{ |song| song.send(key) == value}
end
end
result
end
def find_by_tag(input, tags_arr)
Array(tags_arr).each{ |tag|
input = input.select{ |song| song.tags.include? tag}
}
input
end
end
class Song
attr_accessor :name, :artist, :genre, :subgenre, :tags
def initialize(song_as_string)
attr_array = []
song_as_string.split(".").each { |entry|
attr_array.push entry.lstrip.rstrip
}
@name = attr_array[0]
@artist = attr_array[1]
genre = attr_array[2].split(", ")
@genre = genre[0]
@subgenre = genre[1]
if attr_array[3] != nil
@tags = Array(attr_array[3].split(", "))
end
end
end

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

...FF.FFFFF.

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-clere0/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-clere0/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 find songs that don't have a tag
     Failure/Error: songs(tags: %w[weird cool!]).map(&:name).should eq ['Miles Runs the Voodoo Down']
       
       expected: ["Miles Runs the Voodoo Down"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Miles Runs the Voodoo Down"]
       +[]
     # /tmp/d20111115-13548-clere0/spec.rb:61: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 filter songs by a lambda
     Failure/Error: collection.find(options)
     NoMethodError:
       undefined method `filter' for #<Song:0x9adfbd4>
     # /tmp/d20111115-13548-clere0/solution.rb:16:in `block (2 levels) in find'
     # /tmp/d20111115-13548-clere0/solution.rb:16:in `select'
     # /tmp/d20111115-13548-clere0/solution.rb:16:in `block in find'
     # /tmp/d20111115-13548-clere0/solution.rb:12:in `each_pair'
     # /tmp/d20111115-13548-clere0/solution.rb:12:in `find'
     # /tmp/d20111115-13548-clere0/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-clere0/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 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-clere0/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)>'

  6) 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-clere0/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)>'

  7) 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-clere0/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.54665 seconds
12 examples, 7 failures

Failed examples:

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

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

Николай обнови решението на 31.10.2011 00:50 (преди около 13 години)

+class Collection
+
+ def initialize(songs_as_string, artist_tags)
+ @storage = []
+ songs_as_string.split("\n").each{ |song|
+ @storage << Song.new(song)
+ }
+ end
+
+ def find(criteria)
+ result = @storage
+ criteria.each_pair do |key, value|
+ if key == :tag
+ result = find_by_tag(result, value)
+ else
+ result = result.select{ |song| song.send(key) == value}
+ end
+ end
+ result
+ end
+
+ def find_by_tag(input, tags_arr)
+ Array(tags_arr).each{ |tag|
+ input = input.select{ |song| song.tags.include? tag}
+ }
+ input
+ end
+
+end
+
+class Song
+
+ attr_accessor :name, :artist, :genre, :subgenre, :tags
+
+ def initialize(song_as_string)
+ attr_array = []
+ song_as_string.split(".").each { |entry|
+ attr_array.push entry.lstrip.rstrip
+ }
+ @name = attr_array[0]
+ @artist = attr_array[1]
+ genre = attr_array[2].split(", ")
+ @genre = genre[0]
+ @subgenre = genre[1]
+ if attr_array[3] != nil
+ @tags = Array(attr_array[3].split(", "))
+ end
+ end
+
+end