Решение на Втора задача от Румен Ангелов

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

Към профила на Румен Ангелов

Резултати

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

Код

class Song
def initialize(name_, artist_, genre_, subgenre_, tags_)
@name = name_
@artist = artist_
@genre = genre_
@subgenre = subgenre_
@tags = tags_
@tags = add_tags(artist_, genre_, subgenre_).map do |i|
i !=nil ? i.downcase : i
end
end
def add_tags(*tags_)
(@tags << tags_).flatten
end
def is_this_song?(given_name)
@name == given_name ? true : false
end
def is_from_artist?(given_name)
@artirst == given_name ? true : false
end
def contain?(letter)
@name.include? letter
end
def is_in_tags?(args)
args.each do |i|
false if not @tags.include? i
# elsif i.end_with? "!" and @tags.include? i.chop
#false
end
true
end
end
class Collection
attr_accessor :songs_array
def initialize(str, dict)
@catalog = str
@dictionary = dict
@songs_array = []
parse
init_songs_array
end
def parse
arr = Array(@catalog.lines("\n"))
arr.map { |x| Array( x.lines(".")).map {|y| y.chop.strip} }
end
def init_songs_array
tmp_array = self.parse
tmp_array.each do |x|
arr = Array(x[2].lines(","))
arr2 = Array(x[3].downcase.lines(",")).map do |i|
(i.end_with? ',') ? i.chop.strip : i.strip
end
@songs_array << Song.new(x[0], x[1], arr[0].chop, arr[1], arr2)
end
end
def find(criteria)
@songs_array.select do |x|
if criteria.has_key? :artist
x.is_from_artist? criteria[:artist]
elsif criteria.has_key? :name
x.is_this_song? criteria[:name]
elsif criteria.has_key? :tags
x.is_in_tags? criteria[:tags]
elsif criteria.has_key? :filter
x.contain? criteria[:filter]
end
end
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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/spec.rb:100:in `song'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-gyiwam/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 `downcase' for nil:NilClass
     # /tmp/d20111115-13548-gyiwam/solution.rb:58:in `block in init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `each'
     # /tmp/d20111115-13548-gyiwam/solution.rb:56:in `init_songs_array'
     # /tmp/d20111115-13548-gyiwam/solution.rb:46:in `initialize'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `new'
     # /tmp/d20111115-13548-gyiwam/spec.rb:34:in `block (2 levels) in <top (required)>'
     # /tmp/d20111115-13548-gyiwam/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.54339 seconds
12 examples, 12 failures

Failed examples:

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

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

Румен обнови решението на 31.10.2011 16:58 (преди над 12 години)

+class Song
+ def initialize(name_, artist_, genre_, subgenre_, tags_)
+ @name = name_
+ @artist = artist_
+ @genre = genre_
+ @subgenre = subgenre_
+ @tags = tags_
+ @tags = add_tags(artist_, genre_, subgenre_).map do |i|
+ i !=nil ? i.downcase : i
+ end
+ end
+
+ def add_tags(*tags_)
+ (@tags << tags_).flatten
+ end
+
+ def is_this_song?(given_name)
+ @name == given_name ? true : false
+ end
+
+ def is_from_artist?(given_name)
+ @artirst == given_name ? true : false
+ end
+
+ def contain?(letter)
+ @name.include? letter
+ end
+
+ def is_in_tags?(args)
+ args.each do |i|
+ false if not @tags.include? i
+ # elsif i.end_with? "!" and @tags.include? i.chop
+ #false
+ end
+ true
+ end
+end
+
+class Collection
+ attr_accessor :songs_array
+ def initialize(str, dict)
+ @catalog = str
+ @dictionary = dict
+ @songs_array = []
+ parse
+ init_songs_array
+ end
+
+ def parse
+ arr = Array(@catalog.lines("\n"))
+ arr.map { |x| Array( x.lines(".")).map {|y| y.chop.strip} }
+ end
+
+ def init_songs_array
+ tmp_array = self.parse
+ tmp_array.each do |x|
+ arr = Array(x[2].lines(","))
+ arr2 = Array(x[3].downcase.lines(",")).map do |i|
+ (i.end_with? ',') ? i.chop.strip : i.strip
+ end
+ @songs_array << Song.new(x[0], x[1], arr[0].chop, arr[1], arr2)
+ end
+ end
+
+ def find(criteria)
+ @songs_array.select do |x|
+ if criteria.has_key? :artist
+ x.is_from_artist? criteria[:artist]
+ elsif criteria.has_key? :name
+ x.is_this_song? criteria[:name]
+ elsif criteria.has_key? :tags
+ x.is_in_tags? criteria[:tags]
+ elsif criteria.has_key? :filter
+ x.contain? criteria[:filter]
+ end
+ end
+ end
+
+end