Решение на Втора задача от Деян Камбуров

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

Към профила на Деян Камбуров

Резултати

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

Код

class Song
def initialize(song_attributes,artist_tags)
@name = song_attributes[0].strip
@artist = song_attributes[1].strip
n = song_attributes[2].split(", ")
@genre = n[0].strip
if n[1].nil? then @subgenre = nil
else @subgenre = n[1].chomp
end
@tags = []
if song_attributes[3] != nil then @tags << song_attributes[3].split(", ")
end
@tags << @genre.downcase << @subgenre
@tags.delete_if {|x| x == nil}.flatten!
@tags.each {|n| n.strip!}.map{|n| n.downcase!}
artist_tags.each do|key,value| if key==@artist then @tags<<value.flatten!
end
end
end
def name
@name
end
def artist
@artist
end
def genre
@genre
end
def subgenre
@subgenre
end
def tags
@tags
end
end
class ResultSong
def initialize(song_attributes)
@name = song_attributes.name
@artist = song_attributes.artist
@genre = song_attributes.genre
@subgenre = song_attributes.subgenre
@tags = song_attributes.tags
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)
@array_songs = []
i = 0
songs_as_string.lines.map do |n|
@array_songs[i] = Song.new(n.split("."),artist_tags)
i = i + 1
end
end
def find(criteria)
criteria.each do |key,value|
if key == :tags then find_tags(Array(value))
end
if key == :name then find_name(value)
end
if key == :artist then find_artist(value)
end
end
end
def find_name(string)
result = []
@array_songs.select{|n| n.name == string}.each{ |n|
result << ResultSong.new(n)
}
result
end
def find_artist(string)
result = []
@array_songs.select {|n| n.artist == string}.each{ |n|
result<<ResultSong.new(n)
}
result
end
def find_tags(string_array)
result = []
string_array.each do |tag|
if tag.end_with?("!")
@array_songs.select{|n| not n.tags.include?(tag.chomp)}.each{ |n|
result<<ResultSong.new(n)
}
else
@array_songs.select {|n| n.tags.include?(tag)}.each { |n|
result<<ResultSong.new(n)
}
end
end
result
end
end

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

FFFFFFFFFFFF

Failures:

  1) Collection returns all entries if called without parameters
     Failure/Error: collection.find({}).should have(input.lines.count).items
       expected 20 items, got 0
     # /tmp/d20111115-13548-8y2d06/spec.rb:37: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 artist
     Failure/Error: songs(artist: 'Bill Evans').map(&:name).should =~ ['Autumn Leaves', 'Waltz for Debbie']
     NoMethodError:
       undefined method `name' for [:artist, "Bill Evans"]:Array
     # /tmp/d20111115-13548-8y2d06/spec.rb:41:in `each'
     # /tmp/d20111115-13548-8y2d06/spec.rb:41:in `map'
     # /tmp/d20111115-13548-8y2d06/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)>'

  3) Collection can look up songs by name
     Failure/Error: songs(name: "'Round Midnight").map(&:artist).should =~ ['John Coltrane', 'Thelonious Monk']
     NoMethodError:
       undefined method `artist' for [:name, "'Round Midnight"]:Array
     # /tmp/d20111115-13548-8y2d06/spec.rb:45:in `each'
     # /tmp/d20111115-13548-8y2d06/spec.rb:45:in `map'
     # /tmp/d20111115-13548-8y2d06/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)>'

  4) 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 [:name, "Miles Runs the Voodoo Down"]:Array
     # /tmp/d20111115-13548-8y2d06/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)>'

  5) Collection can find songs by tag
     Failure/Error: songs(tags: 'baroque').map(&:name).should =~ ['Toccata e Fuga', 'Goldberg Variations']
     NoMethodError:
       undefined method `name' for [:tags, "baroque"]:Array
     # /tmp/d20111115-13548-8y2d06/spec.rb:53:in `each'
     # /tmp/d20111115-13548-8y2d06/spec.rb:53:in `map'
     # /tmp/d20111115-13548-8y2d06/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)>'

  6) Collection can find songs by multiple tags
     Failure/Error: songs(tags: %w[popular violin]).map(&:name).should eq ['Eine Kleine Nachtmusik']
     NoMethodError:
       undefined method `name' for [:tags, ["popular", "violin"]]:Array
     # /tmp/d20111115-13548-8y2d06/spec.rb:57:in `each'
     # /tmp/d20111115-13548-8y2d06/spec.rb:57:in `map'
     # /tmp/d20111115-13548-8y2d06/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)>'

  7) 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']
     NoMethodError:
       undefined method `name' for [:tags, ["weird", "cool!"]]:Array
     # /tmp/d20111115-13548-8y2d06/spec.rb:61:in `each'
     # /tmp/d20111115-13548-8y2d06/spec.rb:61:in `map'
     # /tmp/d20111115-13548-8y2d06/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)>'

  8) Collection can filter songs by a lambda
     Failure/Error: songs(filter: ->(song) { song.name == 'Autumn Leaves' }).map(&:name).should eq ['Autumn Leaves']
     NoMethodError:
       undefined method `name' for #<Array:0xa1e97b8>
     # /tmp/d20111115-13548-8y2d06/spec.rb:65:in `each'
     # /tmp/d20111115-13548-8y2d06/spec.rb:65:in `map'
     # /tmp/d20111115-13548-8y2d06/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)>'

  9) Collection adds the artist tags to the songs
     Failure/Error: songs(tags: 'polyphone').map(&:name).should =~ ['Toccata e Fuga', 'Goldberg Variations']
     NoMethodError:
       undefined method `name' for [:tags, "polyphone"]:Array
     # /tmp/d20111115-13548-8y2d06/spec.rb:69:in `each'
     # /tmp/d20111115-13548-8y2d06/spec.rb:69:in `map'
     # /tmp/d20111115-13548-8y2d06/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)>'

  10) Collection allows multiple criteria
     Failure/Error: songs(name: "'Round Midnight", tags: 'bebop').map(&:artist).should eq ['Thelonious Monk']
     NoMethodError:
       undefined method `artist' for [:name, "'Round Midnight"]:Array
     # /tmp/d20111115-13548-8y2d06/spec.rb:73:in `each'
     # /tmp/d20111115-13548-8y2d06/spec.rb:73:in `map'
     # /tmp/d20111115-13548-8y2d06/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)>'

  11) Collection allows all criteria
     Failure/Error: ).map(&:artist).should eq ['Thelonious Monk']
     NoMethodError:
       undefined method `artist' for [:name, "'Round Midnight"]:Array
     # /tmp/d20111115-13548-8y2d06/spec.rb:82:in `each'
     # /tmp/d20111115-13548-8y2d06/spec.rb:82:in `map'
     # /tmp/d20111115-13548-8y2d06/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)>'

  12) Collection constructs an object for each song
     Failure/Error: song.name.should      eq 'Tutu'
     NoMethodError:
       undefined method `name' for [:name, "Tutu"]:Array
     # /tmp/d20111115-13548-8y2d06/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.54207 seconds
12 examples, 12 failures

Failed examples:

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

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

Деян обнови решението на 31.10.2011 16:45 (преди около 13 години)

+class Song
+ def initialize(song_attributes,artist_tags)
+ @name = song_attributes[0].strip
+ @artist = song_attributes[1].strip
+ n = song_attributes[2].split(", ")
+ @genre = n[0].strip
+ if n[1].nil? then @subgenre = nil
+ else @subgenre = n[1].chomp
+ end
+ @tags = []
+ if song_attributes[3] != nil then @tags << song_attributes[3].split(", ")
+ end
+ @tags << @genre.downcase << @subgenre
+ @tags.delete_if {|x| x == nil}.flatten!
+ @tags.each {|n| n.strip!}.map{|n| n.downcase!}
+ artist_tags.each do|key,value| if key==@artist then @tags<<value.flatten!
+ end
+ end
+ end
+
+ def name
+ @name
+ end
+
+ def artist
+ @artist
+ end
+
+ def genre
+ @genre
+ end
+
+ def subgenre
+ @subgenre
+ end
+
+ def tags
+ @tags
+ end
+
+end
+
+class ResultSong
+ def initialize(song_attributes)
+ @name = song_attributes.name
+ @artist = song_attributes.artist
+ @genre = song_attributes.genre
+ @subgenre = song_attributes.subgenre
+ @tags = song_attributes.tags
+ 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)
+ @array_songs = []
+ i = 0
+ songs_as_string.lines.map do |n|
+ @array_songs[i] = Song.new(n.split("."),artist_tags)
+ i = i + 1
+ end
+ end
+
+
+
+ def find(criteria)
+ criteria.each do |key,value|
+ if key == :tags then find_tags(Array(value))
+ end
+ if key == :name then find_name(value)
+ end
+ if key == :artist then find_artist(value)
+ end
+ end
+ end
+
+ def find_name(string)
+ result = []
+ @array_songs.select{|n| n.name == string}.each{ |n|
+ result << ResultSong.new(n)
+ }
+ result
+ end
+
+ def find_artist(string)
+ result = []
+ @array_songs.select {|n| n.artist == string}.each{ |n|
+ result<<ResultSong.new(n)
+ }
+ result
+ end
+
+ def find_tags(string_array)
+ result = []
+ string_array.each do |tag|
+ if tag.end_with?("!")
+ @array_songs.select{|n| not n.tags.include?(tag.chomp)}.each{ |n|
+ result<<ResultSong.new(n)
+ }
+ else
+ @array_songs.select {|n| n.tags.include?(tag)}.each { |n|
+ result<<ResultSong.new(n)
+ }
+ end
+ end
+ result
+ end
+
+
+end