Решение на Втора задача от Михаил Илиев

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

Към профила на Михаил Илиев

Резултати

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

Код

class Collection
def initialize(catalog, artists_tags)
@music = []
catalog.lines { |line|
song = []
line.lines('.') { |attribute| song<<attribute.strip.chomp('.').chomp }
@music<<Song.new(song)
}
artists_tags.each{ |artist, tags|
(self.find artist: artist).each { |song|
song.add_more_tags tags
}
}
end
def find(criteria)
result = @music
criteria.each { |key, value|
if key == :tags
result = @music.select { |song|
# not working
(song.send(key) & value.split).sort == value.split.sort }
elsif key == :filter
result = @music.select { |song| value.call song }
else
result = @music.select { |song| song.send(key) == value }
end
}
result
end
end
class Song
attr_accessor :name, :artist, :genre, :subgenre, :tags
def initialize(song)
@name = song[0]
@artist = song[1]
@genre = song[2].split(',')[0]
@subgenre = song[2].sub(/, /,',').split(',')[1]
@tags = []
if song[3]
@tags = song[3].downcase.sub(/, /,',').split(',')
end
@tags<<@genre.downcase
if @subgenre
@tags<<@subgenre.downcase
end
end
def add_more_tags(more_tags)
@tags = (@tags + more_tags).uniq
end
end

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

.....FF..FF.

Failures:

  1) Collection can find songs by multiple tags
     Failure/Error: collection.find(options)
     NoMethodError:
       undefined method `split' for ["popular", "violin"]:Array
     # /tmp/d20111115-13548-g84nr9/solution.rb:23:in `block (2 levels) in find'
     # /tmp/d20111115-13548-g84nr9/solution.rb:21:in `select'
     # /tmp/d20111115-13548-g84nr9/solution.rb:21:in `block in find'
     # /tmp/d20111115-13548-g84nr9/solution.rb:18:in `each'
     # /tmp/d20111115-13548-g84nr9/solution.rb:18:in `find'
     # /tmp/d20111115-13548-g84nr9/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-g84nr9/spec.rb:57: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 that don't have a tag
     Failure/Error: collection.find(options)
     NoMethodError:
       undefined method `split' for ["weird", "cool!"]:Array
     # /tmp/d20111115-13548-g84nr9/solution.rb:23:in `block (2 levels) in find'
     # /tmp/d20111115-13548-g84nr9/solution.rb:21:in `select'
     # /tmp/d20111115-13548-g84nr9/solution.rb:21:in `block in find'
     # /tmp/d20111115-13548-g84nr9/solution.rb:18:in `each'
     # /tmp/d20111115-13548-g84nr9/solution.rb:18:in `find'
     # /tmp/d20111115-13548-g84nr9/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-g84nr9/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)>'

  3) Collection allows multiple criteria
     Failure/Error: songs(name: "'Round Midnight", tags: 'bebop').map(&:artist).should eq ['Thelonious Monk']
       
       expected: ["Thelonious Monk"]
            got: ["John Coltrane", "John Coltrane", "Miles Davis", "Thelonious Monk"]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Thelonious Monk"]
       +["John Coltrane", "John Coltrane", "Miles Davis", "Thelonious Monk"]
     # /tmp/d20111115-13548-g84nr9/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)>'

  4) Collection allows all criteria
     Failure/Error: ).map(&:artist).should eq ['Thelonious Monk']
       
       expected: ["Thelonious Monk"]
            got: ["John Coltrane", "John Coltrane", "John Coltrane", "John Coltrane", "John Coltrane", "John Coltrane", "Miles Davis", "Miles Davis", "Miles Davis", "Miles Davis", "Bill Evans", "Bill Evans", "Thelonious Monk", "Thelonious Monk"]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,15 @@
       -["Thelonious Monk"]
       +["John Coltrane",
       + "John Coltrane",
       + "John Coltrane",
       + "John Coltrane",
       + "John Coltrane",
       + "John Coltrane",
       + "Miles Davis",
       + "Miles Davis",
       + "Miles Davis",
       + "Miles Davis",
       + "Bill Evans",
       + "Bill Evans",
       + "Thelonious Monk",
       + "Thelonious Monk"]
     # /tmp/d20111115-13548-g84nr9/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.54993 seconds
12 examples, 4 failures

Failed examples:

rspec /tmp/d20111115-13548-g84nr9/spec.rb:56 # Collection can find songs by multiple tags
rspec /tmp/d20111115-13548-g84nr9/spec.rb:60 # Collection can find songs that don't have a tag
rspec /tmp/d20111115-13548-g84nr9/spec.rb:72 # Collection allows multiple criteria
rspec /tmp/d20111115-13548-g84nr9/spec.rb:76 # Collection allows all criteria

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

Михаил обнови решението на 31.10.2011 15:56 (преди около 13 години)

+class Collection
+ def initialize(catalog, artists_tags)
+ @music = []
+ catalog.lines { |line|
+ song = []
+ line.lines('.') { |attribute| song<<attribute.strip.chomp('.').chomp }
+ @music<<Song.new(song)
+ }
+ artists_tags.each{ |artist, tags|
+ (self.find artist: artist).each { |song|
+ song.add_more_tags tags
+ }
+ }
+ end
+
+ def find(criteria)
+ result = @music
+ criteria.each { |key, value|
+
+ if key == :tags
+ result = @music.select { |song|
+ # not working
+ (song.send(key) & value.split).sort == value.split.sort }
+
+ elsif key == :filter
+ result = @music.select { |song| value.call song }
+
+ else
+ result = @music.select { |song| song.send(key) == value }
+ end
+ }
+ result
+ end
+end
+
+
+class Song
+ attr_accessor :name, :artist, :genre, :subgenre, :tags
+
+ def initialize(song)
+ @name = song[0]
+ @artist = song[1]
+ @genre = song[2].split(',')[0]
+ @subgenre = song[2].sub(/, /,',').split(',')[1]
+
+ @tags = []
+ if song[3]
+ @tags = song[3].downcase.sub(/, /,',').split(',')
+ end
+
+ @tags<<@genre.downcase
+ if @subgenre
+ @tags<<@subgenre.downcase
+ end
+ end
+
+ def add_more_tags(more_tags)
+ @tags = (@tags + more_tags).uniq
+ end
+end