Решение на Втора задача от Томислав Иванов

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

Към профила на Томислав Иванов

Резултати

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

Код

class Song
def initialize(string2)
arr=[]
string2.concat('.').each_line('.') { |n| arr.push n.strip }
@name = arr[0].gsub('.','')
@artist = arr[1].gsub('.','')
@genre = arr[2].split(',')
if(@genre[1] != nil)
@subgenre = @genre[1].strip.gsub('.','')
else
@subgenre = 'none'
end
@genre = @genre[0].gsub('.','')
if(arr[3] != '.' && arr[3] != nil)
@tags = arr[3].gsub('.','')
else
@tags = 'none'
end
end
def change_tags (tags)
if @tags != '' and @tags != 'none'
@tags = @tags.concat(', ').concat(tags).gsub('[','').gsub(']','')
else
@tags = tags.gsub('[','').gsub(']','').gsub('"','')
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(string, stringTags)
@arrSongs = []
arrArtist = []
i=0
string.each_line { |n| @arrSongs.push Song.new(n) }
@arrSongs.each do |n|
arrArtist.push n.artist
end
arrArtist.each do |value|
if stringTags.has_key? (value)
@arrSongs[i].change_tags (stringTags[value].to_s.gsub('"',''))
end
i+=1
end
end
def arraySongs
@arrSongs
end
def find(criteria)
arraySongs.select do |k|
puts k.artist == criteria['artist:']
end
end
end

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

false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
Ffalse
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
F

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-ehj0df/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']
       expected collection contained:  ["Autumn Leaves", "Waltz for Debbie"]
       actual collection contained:    []
       the missing elements were:      ["Autumn Leaves", "Waltz for Debbie"]
     # /tmp/d20111115-13548-ehj0df/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']
       expected collection contained:  ["John Coltrane", "Thelonious Monk"]
       actual collection contained:    []
       the missing elements were:      ["John Coltrane", "Thelonious Monk"]
     # /tmp/d20111115-13548-ehj0df/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 nil:NilClass
     # /tmp/d20111115-13548-ehj0df/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-ehj0df/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']
       
       expected: ["Eine Kleine Nachtmusik"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Eine Kleine Nachtmusik"]
       +[]
     # /tmp/d20111115-13548-ehj0df/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-ehj0df/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']
       
       expected: ["Autumn Leaves"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Autumn Leaves"]
       +[]
     # /tmp/d20111115-13548-ehj0df/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-ehj0df/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']
       
       expected: ["Thelonious Monk"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Thelonious Monk"]
       +[]
     # /tmp/d20111115-13548-ehj0df/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']
       
       expected: ["Thelonious Monk"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Thelonious Monk"]
       +[]
     # /tmp/d20111115-13548-ehj0df/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 nil:NilClass
     # /tmp/d20111115-13548-ehj0df/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.57473 seconds
12 examples, 12 failures

Failed examples:

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

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

Томислав обнови решението на 31.10.2011 16:56 (преди около 13 години)

+class Song
+ def initialize(string2)
+ arr=[]
+ string2.concat('.').each_line('.') { |n| arr.push n.strip }
+ @name = arr[0].gsub('.','')
+ @artist = arr[1].gsub('.','')
+ @genre = arr[2].split(',')
+ if(@genre[1] != nil)
+ @subgenre = @genre[1].strip.gsub('.','')
+ else
+ @subgenre = 'none'
+ end
+ @genre = @genre[0].gsub('.','')
+ if(arr[3] != '.' && arr[3] != nil)
+ @tags = arr[3].gsub('.','')
+ else
+ @tags = 'none'
+ end
+ end
+ def change_tags (tags)
+ if @tags != '' and @tags != 'none'
+ @tags = @tags.concat(', ').concat(tags).gsub('[','').gsub(']','')
+ else
+ @tags = tags.gsub('[','').gsub(']','').gsub('"','')
+ 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(string, stringTags)
+ @arrSongs = []
+ arrArtist = []
+ i=0
+ string.each_line { |n| @arrSongs.push Song.new(n) }
+ @arrSongs.each do |n|
+ arrArtist.push n.artist
+ end
+ arrArtist.each do |value|
+ if stringTags.has_key? (value)
+ @arrSongs[i].change_tags (stringTags[value].to_s.gsub('"',''))
+ end
+ i+=1
+ end
+ end
+ def arraySongs
+ @arrSongs
+ end
+
+ def find(criteria)
+ arraySongs.select do |k|
+ puts k.artist == criteria['artist:']
+ end
+ end
+
+
+
+
+ end