Решение на Втора задача от Антоний Цветков

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

Към профила на Антоний Цветков

Резултати

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

Код

class Collection
def initialize(dictionary={}, song_collection=%w[])
@dictionary={}
dictionary.each{|key, value| @dictionary[key]=value}
@song_collection=song_collection
end
def add_song(song_info)
new_song = song_info.split(/\./)
song={}
song[:name]=new_song[0]
song[:artist]=new_song[1]
make_genres(song,new_song[2])
if new_song[3]
song[:tags]=new_song[3].split(/,/)
make_tags(song)
end
add_song_to_collection(song)
end
def make_tags(song)
song[:tags].push(song[:genre]) unless song[:tags].include? song[:genre]
song[:tags].push(song[:subgenre]) unless song[:tags].include? song[:subgenre]
if @dictionary[song[:artist]]
@dictionary[song[:artist]].each do
|tag| song[:tags].push(tag) unless song[:tags].include? tag
end
end
end
def make_genres(song,genres)
if genres =~ /,/
genres=genres.split(/,/)
song[:genre]=genres.shift
song[:subgenre]=genres.shift
else
song[:genre]=genres
end
end
def add_song_to_collection(song)
@song_collection.push(song)
end
def find(criteria)
requested_list=@song_collection
find_tags(requested_list,criteria[:tags]) if criteria[:tags]
find_name(requested_list,criteria[:name]) if criteria[:name]
find_artist(requested_list,criteria[:artist]) if criteria[:artist]
#find_filter(requested_list,criteria[:filter]) if criteria[:filter]
puts "#{requested_list}\n\n"
end
def find_tags(requested_list,requested_tags)
requested_list.select {|song| requested_tags.each do
|tag| song[:tags].include? tags
end
}
end
def find_name(requested_list,name)
requested_list.select {|song| song[:name] == name}
end
def find_artist(requested_list,artist)
requested_list.select {|song| song[:artist] == artist}
end
end
dictionary={
'John Coltrane' => %w[saxophone],
'Bach' => %w[piano polyphone],
}
playlist=Collection.new dictionary
string=""
songlist=string.split(/\n/)
songlist.each{|line| playlist.add_song(line)}
playlist.find artist: 'Bach'

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

[]

FFFFFFFFFFFF

Failures:

  1) Collection returns all entries if called without parameters
     Failure/Error: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa904278>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa903788>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa902d60>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa902338>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:100:in `song'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa8d53ec>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa8d499c>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa8d3f4c>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa8d3510>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa8d2ae8>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa8d20ac>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa8a44e0>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-1mc2lth/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: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `each' for #<String:0xa8a3ae0>
     # /tmp/d20111115-13548-1mc2lth/solution.rb:5:in `initialize'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `new'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-1mc2lth/spec.rb:86: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.53984 seconds
12 examples, 12 failures

Failed examples:

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

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

Антоний обнови решението на 31.10.2011 08:22 (преди около 13 години)

+class Collection
+
+ def initialize(dictionary={}, song_collection=%w[])
+ @dictionary={}
+ dictionary.each{|key, value| @dictionary[key]=value}
+ @song_collection=song_collection
+ end
+
+ def add_song(song_info)
+ new_song = song_info.split(/\./)
+ song={}
+ song[:name]=new_song[0]
+ song[:artist]=new_song[1]
+ make_genres(song,new_song[2])
+ if new_song[3]
+ song[:tags]=new_song[3].split(/,/)
+ make_tags(song)
+ end
+ add_song_to_collection(song)
+ end
+
+ def make_tags(song)
+ song[:tags].push(song[:genre]) unless song[:tags].include? song[:genre]
+ song[:tags].push(song[:subgenre]) unless song[:tags].include? song[:subgenre]
+ if @dictionary[song[:artist]]
+ @dictionary[song[:artist]].each do
+ |tag| song[:tags].push(tag) unless song[:tags].include? tag
+ end
+ end
+ end
+
+ def make_genres(song,genres)
+ if genres =~ /,/
+ genres=genres.split(/,/)
+ song[:genre]=genres.shift
+ song[:subgenre]=genres.shift
+ else
+ song[:genre]=genres
+ end
+ end
+
+ def add_song_to_collection(song)
+ @song_collection.push(song)
+ end
+
+ def find(criteria)
+ requested_list=@song_collection
+ find_tags(requested_list,criteria[:tags]) if criteria[:tags]
+ find_name(requested_list,criteria[:name]) if criteria[:name]
+ find_artist(requested_list,criteria[:artist]) if criteria[:artist]
+ #find_filter(requested_list,criteria[:filter]) if criteria[:filter]
+ puts "#{requested_list}\n\n"
+ end
+
+ def find_tags(requested_list,requested_tags)
+ requested_list.select {|song| requested_tags.each do
+ |tag| song[:tags].include? tags
+ end
+ }
+ end
+
+ def find_name(requested_list,name)
+ requested_list.select {|song| song[:name] == name}
+ end
+
+ def find_artist(requested_list,artist)
+ requested_list.select {|song| song[:artist] == artist}
+ end
+end
+
+dictionary={
+ 'John Coltrane' => %w[saxophone],
+ 'Bach' => %w[piano polyphone],
+}
+
+playlist=Collection.new dictionary
+string=""
+songlist=string.split(/\n/)
+songlist.each{|line| playlist.add_song(line)}
+playlist.find artist: 'Bach'