Решение на Втора задача от Йосиф Йосифов

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

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

Резултати

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

Код

class SongInfo
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class SongInfoCollection
include Enumerable
attr_accessor :song_info_collection
def initialize() @song_info_collection = [] end
def add(name, artist, album)
@song_info_collection.push SongInfo.new(name, artist, album)
end
def artists
@song_info_collection..group_by {|song| song.artist }.map { |key,song| key }
end
def names
@song_info_collection.group_by {|song| song.name }.map { |key,song| key }
end
def albums
@song_info_collection.group_by {|song| song.album }.map { |key,song| key }
end
def each
@song_info_collection.each { |song_info| yield song_info }
end
def filter criteria
@song_info_collection.select(&criteria)
end
end
class Collection
attr_accessor :collection
def self.parse(text)
@collection = SongInfoCollection.new
text.split(/\n/).each_slice(4) do |slice|
@collection.add(slice[0],slice[1],slice[2])
end
return @collection
end
end

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

F..FFFFFFFF

Failures:

  1) Collection can find all the artists in the collection
     Failure/Error: collection.artists.should =~ [
     ArgumentError:
       bad value for range
     # /tmp/d20130203-23049-n75j33/solution.rb:20:in `artists'
     # /tmp/d20130203-23049-n75j33/spec.rb:5: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 be filtered by song name
     Failure/Error: filtered = collection.filter Criteria.name('Fields of Gold')
     NameError:
       uninitialized constant Criteria
     # /tmp/d20130203-23049-n75j33/spec.rb:44: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 be filtered by song name
     Failure/Error: filtered = collection.filter Criteria.artist('Sting')
     NameError:
       uninitialized constant Criteria
     # /tmp/d20130203-23049-n75j33/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)>'

  4) Collection can be filtered by album
     Failure/Error: filtered = collection.filter Criteria.album('Live at Blues Alley')
     NameError:
       uninitialized constant Criteria
     # /tmp/d20130203-23049-n75j33/spec.rb:54: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 return an empty result
     Failure/Error: filtered = collection.filter Criteria.album('The Dark Side of the Moon')
     NameError:
       uninitialized constant Criteria
     # /tmp/d20130203-23049-n75j33/spec.rb:59: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 supports a conjuction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & Criteria.name('Fields of Gold')
     NameError:
       uninitialized constant Criteria
     # /tmp/d20130203-23049-n75j33/spec.rb:64: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 supports a disjunction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') | Criteria.name('Fields of Gold')
     NameError:
       uninitialized constant Criteria
     # /tmp/d20130203-23049-n75j33/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)>'

  8) Collection supports negation of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & !Criteria.name('Fields of Gold')
     NameError:
       uninitialized constant Criteria
     # /tmp/d20130203-23049-n75j33/spec.rb:78: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 can be adjoined with another collection
     Failure/Error: sting    = collection.filter Criteria.artist('Sting')
     NameError:
       uninitialized constant Criteria
     # /tmp/d20130203-23049-n75j33/spec.rb:83: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.01874 seconds
11 examples, 9 failures

Failed examples:

rspec /tmp/d20130203-23049-n75j33/spec.rb:4 # Collection can find all the artists in the collection
rspec /tmp/d20130203-23049-n75j33/spec.rb:43 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-n75j33/spec.rb:48 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-n75j33/spec.rb:53 # Collection can be filtered by album
rspec /tmp/d20130203-23049-n75j33/spec.rb:58 # Collection can return an empty result
rspec /tmp/d20130203-23049-n75j33/spec.rb:63 # Collection supports a conjuction of filters
rspec /tmp/d20130203-23049-n75j33/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-n75j33/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-n75j33/spec.rb:82 # Collection can be adjoined with another collection

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

Йосиф обнови решението на 31.10.2012 14:02 (преди над 11 години)

+class SongInfo
+ attr_accessor :name, :artist, :album
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+end
+
+class SongInfoCollection
+ include Enumerable
+
+ attr_accessor :song_info_collection
+
+ def initialize() @song_info_collection = [] end
+
+ def add(name, artist, album)
+ @song_info_collection.push SongInfo.new(name, artist, album)
+ end
+
+ def artists
+ @song_info_collection..group_by {|song| song.artist }.map { |key,song| key }
+ end
+
+ def names
+ @song_info_collection.group_by {|song| song.name }.map { |key,song| key }
+ end
+
+ def albums
+ @song_info_collection.group_by {|song| song.album }.map { |key,song| key }
+ end
+
+ def each
+ @song_info_collection.each { |song_info| yield song_info }
+ end
+
+ def filter criteria
+ @song_info_collection.select(&criteria)
+ end
+end
+
+class Collection
+ attr_accessor :collection
+ def self.parse(text)
+ @collection = SongInfoCollection.new
+ text.split(/\n/).each_slice(4) do |slice|
+ @collection.add(slice[0],slice[1],slice[2])
+ end
+ return @collection
+ end
+end