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

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

Към профила на Николай Беличев

Резултати

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

Код

class Song
def initialize(song_as_string, artist_tags)
song_array = song_as_string.split(/\s*\.\s*/)
@name = song_array[0]
@artist = song_array[1]
@genre = song_array[2].split(/\s*\,\s*/)[0]
@subgenre = song_array[2].split(/\s*\,\s*/)[1]
@tags = [@genre.downcase]
if artist_tags[@artist]
@tags |= artist_tags[@artist]
end
init_tags(song_array[3])
end
def init_tags(tags_string)
if tags_string
@tags |= tags_string.split(/\s*\,\s*/)
end
if @subgenre
@tags << @subgenre.downcase
end
end
def name
@name
end
def artist
@artist
end
def genre
@genre
end
def subgenre
@subgenre
end
def tags
@tags
end
end
class Collection
def initialize(songs_as_string, artist_tags)
@songs_as_string = songs_as_string
@artist_tags = artist_tags
@songs = []
songs_as_string.each_line {|s| @songs << Song.new(s.strip, artist_tags)}
end
def find(criteria)
a = @songs
if criteria[:tags]
a = find_tags(criteria[:tags], a)
end
if criteria[:artist]
a = find_artist(criteria[:artist], a)
end
if criteria[:name]
a = find_name(criteria[:name], a)
end
a
end
def find_tags(tags_string, array)
array = array.select {|v| v.tags.include?(tags_string)}
end
def find_artist(artist_string, array)
array.select {|v| v.artist == artist_string}
end
def find_name(name_string, array)
array.select {|v| v.name == name_string}
end
end

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

.....FFF....

Failures:

  1) Collection can find songs by multiple tags
     Failure/Error: songs(tags: %w[popular violin]).map(&:name).should eq ['Eine Kleine Nachtmusik']
       
       expected: ["Eine Kleine Nachtmusik"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Eine Kleine Nachtmusik"]
       +[]
     # /tmp/d20111115-13548-1qeeqdj/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: 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-1qeeqdj/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 can filter songs by a lambda
     Failure/Error: songs(filter: ->(song) { song.name == 'Autumn Leaves' }).map(&:name).should eq ['Autumn Leaves']
       
       expected: ["Autumn Leaves"]
            got: ["My Favourite Things", "Greensleves", "Alabama", "Acknowledgement", "Afro Blue", "'Round Midnight", "My Funny Valentine", "Tutu", "Miles Runs the Voodoo Down", "Boplicity", "Autumn Leaves", "Waltz for Debbie", "'Round Midnight", "Ruby, My Dear", "Fur Elise", "Moonlight Sonata", "Pathetique", "Toccata e Fuga", "Goldberg Variations", "Eine Kleine Nachtmusik"]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,21 @@
       -["Autumn Leaves"]
       +["My Favourite Things",
       + "Greensleves",
       + "Alabama",
       + "Acknowledgement",
       + "Afro Blue",
       + "'Round Midnight",
       + "My Funny Valentine",
       + "Tutu",
       + "Miles Runs the Voodoo Down",
       + "Boplicity",
       + "Autumn Leaves",
       + "Waltz for Debbie",
       + "'Round Midnight",
       + "Ruby, My Dear",
       + "Fur Elise",
       + "Moonlight Sonata",
       + "Pathetique",
       + "Toccata e Fuga",
       + "Goldberg Variations",
       + "Eine Kleine Nachtmusik"]
     # /tmp/d20111115-13548-1qeeqdj/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)>'

Finished in 0.55406 seconds
12 examples, 3 failures

Failed examples:

rspec /tmp/d20111115-13548-1qeeqdj/spec.rb:56 # Collection can find songs by multiple tags
rspec /tmp/d20111115-13548-1qeeqdj/spec.rb:60 # Collection can find songs that don't have a tag
rspec /tmp/d20111115-13548-1qeeqdj/spec.rb:64 # Collection can filter songs by a lambda

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

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

+class Song
+ def initialize(song_as_string, artist_tags)
+ song_array = song_as_string.split(/\s*\.\s*/)
+ @name = song_array[0]
+ @artist = song_array[1]
+ @genre = song_array[2].split(/\s*\,\s*/)[0]
+ @subgenre = song_array[2].split(/\s*\,\s*/)[1]
+ @tags = [@genre.downcase]
+ if artist_tags[@artist]
+ @tags |= artist_tags[@artist]
+ end
+ init_tags(song_array[3])
+ end
+ def init_tags(tags_string)
+ if tags_string
+ @tags |= tags_string.split(/\s*\,\s*/)
+ end
+ if @subgenre
+ @tags << @subgenre.downcase
+ end
+ end
+ def name
+ @name
+ end
+ def artist
+ @artist
+ end
+ def genre
+ @genre
+ end
+ def subgenre
+ @subgenre
+ end
+ def tags
+ @tags
+ end
+end
+
+
+class Collection
+ def initialize(songs_as_string, artist_tags)
+ @songs_as_string = songs_as_string
+ @artist_tags = artist_tags
+ @songs = []
+ songs_as_string.each_line {|s| @songs << Song.new(s.strip, artist_tags)}
+ end
+
+ def find(criteria)
+ a = @songs
+ if criteria[:tags]
+ a = find_tags(criteria[:tags], a)
+ end
+ if criteria[:artist]
+ a = find_artist(criteria[:artist], a)
+ end
+ if criteria[:name]
+ a = find_name(criteria[:name], a)
+ end
+ a
+ end
+
+ def find_tags(tags_string, array)
+ array = array.select {|v| v.tags.include?(tags_string)}
+ end
+ def find_artist(artist_string, array)
+ array.select {|v| v.artist == artist_string}
+ end
+ def find_name(name_string, array)
+ array.select {|v| v.name == name_string}
+ end
+end