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

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

Към профила на Недислав Стойчев

Резултати

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

Код

class Collection
attr_reader :songs
def initialize(songs_as_text, artists_tags)
songs_as_strings = []
coll = []
@songs = []
songs_as_lines = songs_as_text.lines("\n") { |s| songs_as_strings << s }
songs_as_strings.each { |s| coll << s.split(".") }
coll.each { |s| @songs << Song.new(s) }
end
def find(criteria)
output = Hash[]
if criteria != {}
if criteria.has_key? :name
criteria_name(criteria,output)
end
if criteria.has_key? :artist
criteria_artist(criteria,output)
end
if criteria.has_key? :tags
criteria_tags(criteria,output)
end
if criteria.has_key? :filter
criteria_filter(criteria, output)
end
end
return output
end
def criteria_name(criteria, output)
songs.each do |s|
if find_name(criteria[:name], s) then output[s.name] = s end
end
end
def criteria_artist(criteria, output)
songs.each do |s|
if find_artist(criteria[:artist], s) then output[s.name] = s end
end
end
def criteria_tags(criteria, output)
songs.each do |s|
if find_tags(criteria[:tags], s) then output[s.name] = s end
end
end
def criteria_filter(criteria, output)
songs.each do |s|
if yield s then output[s.name] = s end
end
end
def find_tags(tags, s)
if s.tags != nil then return s.tags.eql? tags end
false
end
def find_name(name, s)
return s.name.eql? name
end
def find_artist(artist, s)
return s.artist.eql? artist
end
end
class Song
attr_accessor :name
attr_accessor :artist
attr_accessor :genre
attr_accessor :subgenre
attr_accessor :tags
def initialize(song)
@name = song[0].strip
@artist = song[1].strip
@genre = if song[2] != nil
song[2].partition(",")[0].strip
end
@subgenre = if song[2] != nil
song[2].partition(",")[2].strip
end
@tags = if song[3] != nil
song[3] = song[3].strip
Array(song[3].split(", "))
end
end
def print
p "Name: #{@name}"
p "Artist: #{@artist}"
p "Genre: #{@genre}"
p "Subgenre: #{@subgenre}"
p "Tags: #{@tags}"
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-rzpfl0/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 #<Array:0xaab4b18>
     # /tmp/d20111115-13548-rzpfl0/spec.rb:41:in `each'
     # /tmp/d20111115-13548-rzpfl0/spec.rb:41:in `map'
     # /tmp/d20111115-13548-rzpfl0/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 #<Array:0xaac344c>
     # /tmp/d20111115-13548-rzpfl0/spec.rb:45:in `each'
     # /tmp/d20111115-13548-rzpfl0/spec.rb:45:in `map'
     # /tmp/d20111115-13548-rzpfl0/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 #<Array:0xa802870>
     # /tmp/d20111115-13548-rzpfl0/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']
       expected collection contained:  ["Goldberg Variations", "Toccata e Fuga"]
       actual collection contained:    []
       the missing elements were:      ["Goldberg Variations", "Toccata e Fuga"]
     # /tmp/d20111115-13548-rzpfl0/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 #<Array:0xa81e1d8>
     # /tmp/d20111115-13548-rzpfl0/spec.rb:57:in `each'
     # /tmp/d20111115-13548-rzpfl0/spec.rb:57:in `map'
     # /tmp/d20111115-13548-rzpfl0/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']
       
       expected: ["Miles Runs the Voodoo Down"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Miles Runs the Voodoo Down"]
       +[]
     # /tmp/d20111115-13548-rzpfl0/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: collection.find(options)
     LocalJumpError:
       no block given (yield)
     # /tmp/d20111115-13548-rzpfl0/solution.rb:52:in `block in criteria_filter'
     # /tmp/d20111115-13548-rzpfl0/solution.rb:51:in `each'
     # /tmp/d20111115-13548-rzpfl0/solution.rb:51:in `criteria_filter'
     # /tmp/d20111115-13548-rzpfl0/solution.rb:26:in `find'
     # /tmp/d20111115-13548-rzpfl0/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-rzpfl0/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']
       expected collection contained:  ["Goldberg Variations", "Toccata e Fuga"]
       actual collection contained:    []
       the missing elements were:      ["Goldberg Variations", "Toccata e Fuga"]
     # /tmp/d20111115-13548-rzpfl0/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 #<Array:0xab28edc>
     # /tmp/d20111115-13548-rzpfl0/spec.rb:73:in `each'
     # /tmp/d20111115-13548-rzpfl0/spec.rb:73:in `map'
     # /tmp/d20111115-13548-rzpfl0/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: collection.find(options)
     LocalJumpError:
       no block given (yield)
     # /tmp/d20111115-13548-rzpfl0/solution.rb:52:in `block in criteria_filter'
     # /tmp/d20111115-13548-rzpfl0/solution.rb:51:in `each'
     # /tmp/d20111115-13548-rzpfl0/solution.rb:51:in `criteria_filter'
     # /tmp/d20111115-13548-rzpfl0/solution.rb:26:in `find'
     # /tmp/d20111115-13548-rzpfl0/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-rzpfl0/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 #<Array:0xaccb014>
     # /tmp/d20111115-13548-rzpfl0/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.53949 seconds
12 examples, 12 failures

Failed examples:

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

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

Недислав обнови решението на 31.10.2011 14:46 (преди около 13 години)

+class Collection
+ attr_reader :songs
+
+ def initialize(songs_as_text, artists_tags)
+ songs_as_strings = []
+ coll = []
+ @songs = []
+ songs_as_lines = songs_as_text.lines("\n") { |s| songs_as_strings << s }
+ songs_as_strings.each { |s| coll << s.split(".") }
+ coll.each { |s| @songs << Song.new(s) }
+ end
+
+ def find(criteria)
+ output = Hash[]
+ if criteria != {}
+ if criteria.has_key? :name
+ criteria_name(criteria,output)
+ end
+ if criteria.has_key? :artist
+ criteria_artist(criteria,output)
+ end
+ if criteria.has_key? :tags
+ criteria_tags(criteria,output)
+ end
+ if criteria.has_key? :filter
+ criteria_filter(criteria, output)
+ end
+ end
+ return output
+ end
+
+ def criteria_name(criteria, output)
+ songs.each do |s|
+ if find_name(criteria[:name], s) then output[s.name] = s end
+ end
+ end
+
+ def criteria_artist(criteria, output)
+ songs.each do |s|
+ if find_artist(criteria[:artist], s) then output[s.name] = s end
+ end
+ end
+
+ def criteria_tags(criteria, output)
+ songs.each do |s|
+ if find_tags(criteria[:tags], s) then output[s.name] = s end
+ end
+ end
+
+ def criteria_filter(criteria, output)
+ songs.each do |s|
+ if yield s then output[s.name] = s end
+ end
+ end
+
+ def find_tags(tags, s)
+ if s.tags != nil then return s.tags.eql? tags end
+ false
+ end
+
+ def find_name(name, s)
+ return s.name.eql? name
+ end
+
+ def find_artist(artist, s)
+ return s.artist.eql? artist
+ end
+end
+
+class Song
+ attr_accessor :name
+ attr_accessor :artist
+ attr_accessor :genre
+ attr_accessor :subgenre
+ attr_accessor :tags
+
+ def initialize(song)
+ @name = song[0].strip
+ @artist = song[1].strip
+ @genre = if song[2] != nil
+ song[2].partition(",")[0].strip
+ end
+ @subgenre = if song[2] != nil
+ song[2].partition(",")[2].strip
+ end
+ @tags = if song[3] != nil
+ song[3] = song[3].strip
+ Array(song[3].split(", "))
+ end
+ end
+
+ def print
+ p "Name: #{@name}"
+ p "Artist: #{@artist}"
+ p "Genre: #{@genre}"
+ p "Subgenre: #{@subgenre}"
+ p "Tags: #{@tags}"
+ end
+end