Решение на Втора задача от Христо Банчев

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

Към профила на Христо Банчев

Резултати

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

Код

class Song
attr_accessor :name, :artist, :genre, :subgenre, :tags
def initialize(name, artist, genre, subgenre, tags)
@name = name
@artist = artist
@genre = genre
@subgenre = subgenre
@tags = tags
end
def matchCriteria(criteria)
name? criteria[:name] and
artist? criteria[:artist] and
tags? criteria[:tags] and
filter? criteria[:filter] and
true
end
private
def name?(name)
return true if name.nil?
@name == name
end
def artist?(artist)
return true if artist.nil?
@artist == artist
end
def tags?(tags)
return true if tags.nil?
all_tags = tags.kind_of?(String) ? [tags] : tags
included = []
excluded = []
all_tags.each { |tag| tag[-1]=='!' ? excluded << tag.chop : included << tag}
check_one = included - @tags == []
check_two = @tags - excluded == @tags
check_one and check_two
end
def filter?(filter)
return true if filter.nil?
filter.call self
end
end
class Collection
attr_reader :songs, :artist_tags
def initialize(songs_as_string, artist_tags)
@artist_tags = artist_tags ? artist_tags : {}
@songs = []
extract_songs(songs_as_string)
end
def find(criteria)
return @songs if criteria.nil? or criteria.empty?
@songs.select { |song| song.matchCriteria(criteria) }
end
private
def extract_songs(string)
string.lines("\n") { |song| @songs << song_from_line(song.sub("\n", '')) }
end
def song_from_line(song)
split_song = song.split(".")
name = split_song[0]
artist = split_song[1]
genres = split_song[2].split(", ")
tags = extract_tags(split_song[3], artist, genres)
Song.new(name, artist, genres[0], genres[1], tags)
end
def extract_tags(tags, artist, genres)
result = []
result += genres
result += tags.split(", ") if tags
result += @artist_tags[artist] if @artist_tags[artist]
result
end
end

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

.FFFFFFFFFFF

Failures:

  1) Collection can look up songs by artist
     Failure/Error: songs(artist: 'Bill Evans').map(&:name).should =~ ['Autumn Leaves', 'Waltz for Debbie']
       expected collection contained:  ["Autumn Leaves", "Waltz for Debbie"]
       actual collection contained:    []
       the missing elements were:      ["Autumn Leaves", "Waltz for Debbie"]
     # /tmp/d20111115-13548-qidx9j/spec.rb:41: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 look up songs by name
     Failure/Error: songs(name: "'Round Midnight").map(&:artist).should =~ ['John Coltrane', 'Thelonious Monk']
       expected collection contained:  ["John Coltrane", "Thelonious Monk"]
       actual collection contained:    []
       the missing elements were:      ["John Coltrane", "Thelonious Monk"]
     # /tmp/d20111115-13548-qidx9j/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)>'

  3) Collection uses the genre and subgenre as tags
     Failure/Error: song(name: 'Miles Runs the Voodoo Down').tags.should include('jazz', 'fusion')
     NoMethodError:
       undefined method `tags' for nil:NilClass
     # /tmp/d20111115-13548-qidx9j/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)>'

  4) 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-qidx9j/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)>'

  5) 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-qidx9j/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)>'

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

  7) 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: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Autumn Leaves"]
       +[]
     # /tmp/d20111115-13548-qidx9j/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)>'

  8) 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-qidx9j/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)>'

  9) 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-qidx9j/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)>'

  10) 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-qidx9j/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)>'

  11) Collection constructs an object for each song
     Failure/Error: song.name.should      eq 'Tutu'
     NoMethodError:
       undefined method `name' for nil:NilClass
     # /tmp/d20111115-13548-qidx9j/spec.rb:88: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.56339 seconds
12 examples, 11 failures

Failed examples:

rspec /tmp/d20111115-13548-qidx9j/spec.rb:40 # Collection can look up songs by artist
rspec /tmp/d20111115-13548-qidx9j/spec.rb:44 # Collection can look up songs by name
rspec /tmp/d20111115-13548-qidx9j/spec.rb:48 # Collection uses the genre and subgenre as tags
rspec /tmp/d20111115-13548-qidx9j/spec.rb:52 # Collection can find songs by tag
rspec /tmp/d20111115-13548-qidx9j/spec.rb:56 # Collection can find songs by multiple tags
rspec /tmp/d20111115-13548-qidx9j/spec.rb:60 # Collection can find songs that don't have a tag
rspec /tmp/d20111115-13548-qidx9j/spec.rb:64 # Collection can filter songs by a lambda
rspec /tmp/d20111115-13548-qidx9j/spec.rb:68 # Collection adds the artist tags to the songs
rspec /tmp/d20111115-13548-qidx9j/spec.rb:72 # Collection allows multiple criteria
rspec /tmp/d20111115-13548-qidx9j/spec.rb:76 # Collection allows all criteria
rspec /tmp/d20111115-13548-qidx9j/spec.rb:85 # Collection constructs an object for each song

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

Христо обнови решението на 30.10.2011 21:35 (преди над 12 години)

+class Song
+ attr_accessor :name, :artist, :genre, :subgenre, :tags
+
+ def initialize(name, artist, genre, subgenre, tags)
+ @name = name
+ @artist = artist
+ @genre = genre
+ @subgenre = subgenre
+ @tags = tags
+ end
+
+ def matchCriteria(criteria)
+ name? criteria[:name] and
+ artist? criteria[:artist] and
+ tags? criteria[:tags] and
+ filter? criteria[:filter] and
+ true
+ end
+
+ private
+
+ def name?(name)
+ return true if name.nil?
+ @name == name
+ end
+
+ def artist?(artist)
+ return true if artist.nil?
+ @artist == artist
+ end
+
+ def tags?(tags)
+ return true if tags.nil?
+
+ all_tags = tags.kind_of?(String) ? [tags] : tags
+
+ included = []
+ excluded = []
+
+ all_tags.each { |tag| tag[-1]=='!' ? excluded << tag.chop : included << tag}
+
+ check_one = included - @tags == []
+ check_two = @tags - excluded == @tags
+
+ check_one and check_two
+ end
+
+ def filter?(filter)
+ return true if filter.nil?
+ filter.call self
+ end
+end
+
+class Collection
+ attr_reader :songs, :artist_tags
+
+ def initialize(songs_as_string, artist_tags)
+ @artist_tags = artist_tags ? artist_tags : {}
+ @songs = []
+ extract_songs(songs_as_string)
+ end
+
+ def find(criteria)
+ return @songs if criteria.nil? or criteria.empty?
+
+ @songs.select { |song| song.matchCriteria(criteria) }
+ end
+
+ private
+
+ def extract_songs(string)
+ string.lines("\n") { |song| @songs << song_from_line(song.sub("\n", '')) }
+ end
+
+ def song_from_line(song)
+ split_song = song.split(".")
+
+ name = split_song[0]
+ artist = split_song[1]
+ genres = split_song[2].split(", ")
+ tags = extract_tags(split_song[3], artist, genres)
+
+ Song.new(name, artist, genres[0], genres[1], tags)
+ end
+
+ def extract_tags(tags, artist, genres)
+ result = []
+
+ result += genres
+ result += tags.split(", ") if tags
+ result += @artist_tags[artist] if @artist_tags[artist]
+
+ result
+ end
+end