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

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

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

Резултати

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

Код

class Song
attr_accessor :name, :artist, :genre, :subgenre, :tags
def initialize (string_line, artist_tag)
split_string_line = string_line.split(".")
genre_subgenre = split_string_line[2].split(",")
name = split_string_line[0].gsub(" ","")
artist = split_string_line[1].gsub(" ","")
genre = genre_subgenre[0].gsub(" ","")
subgenre = genre_subgenre.fetch(1,nil).gsub(" ","")
tags = split_string_line[3].split(", ").gsub(" ","")
tags << genre
tags << subgenre
tags << artist_tag[name]
end
def has_tags? (tags)
tags.each do |tag|
if self.tags().include?(tag) then next
else false
end
end
true
end
def check_tag
tags.each! do |tag|
if tag.end_with?("!") do
p "Illegal argument in tags, 'tag!' change with 'tag' "
tag.gsub("!","")
end
end
end
end
end
class Collection
attr_accessor :collection
def initialize (song_as_string,artist_tag)
collection = song_as_string.lines.map { |line| Song.new(line,artist_tag) }
end
def find (criteria)
song_t = tag_filter(criteria.fetch(:tags,""))
song_n = collection.select {|song| song.name == criteria.fetch(:name,"")}
song_a = collection.select {|song| song.art == criteria.fetch(:artist,"")}
song_f = collection.select {|song| criteria[:filter].call song}
section = song_t & song_n & song_a & song_f
section
end
private
def tag_filter(tags)
hasnt_tag, has_tag = [],[]
tags.each do |tag|
if tag.end_with?("!") then hasnt_tag<<tag
else has_tag<<tag
end
end
song_has_tag(has_tag) & song_hasnt_tag(hasnt_tag)
end
def song_has_tag(tags)
songs_has_tag = []
songs_has_tag = collection.select {|song| song.has_tags?(tags)}
songs_has_tag
end
def song_hasnt_tag(tags)
songs_hasnt_tag = []
songs_hasnt_tag = collection.select {|song| !song.has_tags?(tags)}
songs_hasnt_tag
end
end

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

FFFFFFFFFFFF

Failures:

  1) Collection returns all entries if called without parameters
     Failure/Error: let(:collection) { Collection.new input, additional_tags }
     NoMethodError:
       undefined method `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:100:in `song'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-a9w3xf/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 `gsub' for ["        popular", "cover\n"]:Array
     # /tmp/d20111115-13548-a9w3xf/solution.rb:12:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `new'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `block in initialize'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `lines'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `each'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `map'
     # /tmp/d20111115-13548-a9w3xf/solution.rb:47:in `initialize'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `new'
     # /tmp/d20111115-13548-a9w3xf/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-a9w3xf/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.53853 seconds
12 examples, 12 failures

Failed examples:

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

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

Красимир обнови решението на 31.10.2011 15:58 (преди около 13 години)

+class Song
+
+ attr_accessor :name, :artist, :genre, :subgenre, :tags
+
+ def initialize (string_line, artist_tag)
+ split_string_line = string_line.split(".")
+ genre_subgenre = split_string_line[2].split(",")
+ name = split_string_line[0].gsub(" ","")
+ artist = split_string_line[1].gsub(" ","")
+ genre = genre_subgenre[0].gsub(" ","")
+ subgenre = genre_subgenre.fetch(1,nil).gsub(" ","")
+ tags = split_string_line[3].split(", ").gsub(" ","")
+
+ tags << genre
+ tags << subgenre
+ tags << artist_tag[name]
+
+ end
+
+ def has_tags? (tags)
+ tags.each do |tag|
+ if self.tags().include?(tag) then next
+ else false
+ end
+ end
+ true
+ end
+
+ def check_tag
+ tags.each! do |tag|
+ if tag.end_with?("!") do
+ p "Illegal argument in tags, 'tag!' change with 'tag' "
+ tag.gsub("!","")
+ end
+ end
+ end
+ end
+
+end
+
+
+class Collection
+
+ attr_accessor :collection
+
+ def initialize (song_as_string,artist_tag)
+ collection = song_as_string.lines.map { |line| Song.new(line,artist_tag) }
+ end
+
+ def find (criteria)
+ song_t = tag_filter(criteria.fetch(:tags,""))
+ song_n = collection.select {|song| song.name == criteria.fetch(:name,"")}
+ song_a = collection.select {|song| song.art == criteria.fetch(:artist,"")}
+ song_f = collection.select {|song| criteria[:filter].call song}
+
+
+ section = song_t & song_n & song_a & song_f
+ section
+ end
+
+ private
+
+ def tag_filter(tags)
+ hasnt_tag, has_tag = [],[]
+
+ tags.each do |tag|
+ if tag.end_with?("!") then hasnt_tag<<tag
+ else has_tag<<tag
+ end
+ end
+
+ song_has_tag(has_tag) & song_hasnt_tag(hasnt_tag)
+ end
+
+ def song_has_tag(tags)
+ songs_has_tag = []
+ songs_has_tag = collection.select {|song| song.has_tags?(tags)}
+ songs_has_tag
+ end
+
+
+ def song_hasnt_tag(tags)
+ songs_hasnt_tag = []
+ songs_hasnt_tag = collection.select {|song| !song.has_tags?(tags)}
+ songs_hasnt_tag
+ end
+
+end