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

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

Към профила на Атанас Пройчев

Резултати

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

Код

class Collection
include Enumerable
attr_accessor :songs
def initialize songs
@songs = songs
end
def Collection.parse text
songs, help_array = [], []
text.each_line { |line| help_array << line.chomp }
help_array.each_slice(4) { |l1| songs << Song.new(l1) }
Collection.new songs
end
def artists
artists = @songs.map { |song| song.artist }
artists.uniq
end
def names
names = @songs.map { |song| song.name }
names.uniq
end
def albums
albums = @songs.map { |song| song.album }
albums.uniq
end
def each
yield
self
end
def filter criteria
new_collection = @songs.select do |song|
song.send(criteria.type) == criteria.name
end
Collection.new new_collection
end
def adjoin other
collection = @songs.concat other.songs
Collection.new collection.uniq
end
end
class Song
attr_reader :name, :artist, :album
def initialize new_song
@name = new_song[0]
@artist = new_song[1]
@album = new_song[2]
end
end
class Criteria < Collection
attr_accessor :type, :name
def initialize type, name
@type = type
@name = name
end
def Criteria.name name_of_song
type = :name
name = name_of_song
Criteria.new type, name
end
def Criteria.artist name_of_artist
type = :artist
name = name_of_artist
Criteria.new type, name
end
def Criteria.album name_of_album
type = :album
name = name_of_album
Criteria.new type, name
end
def | other
puts self
#songs.select do |song|
# puts song.send(type) == name || song.send(other.type) == other.name
# end
#type << criteria.type
# name << criteria.name
#Criteria.new type, name
end
def & criteria
Criteria.new [type, criteria.type], [name, criteria.name]
end
def !
Criteria.new ['!', type], [name]
end
end

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

...FFFFF#<Criteria:0x9b412a8>
FFF

Failures:

  1) Collection can be filtered by song name
     Failure/Error: filtered.map(&:artist).should =~ ['Eva Cassidy', 'Sting']
     ArgumentError:
       no receiver given
     # /tmp/d20130203-23049-zhm90i/solution.rb:33:in `each'
     # /tmp/d20130203-23049-zhm90i/spec.rb:45:in `map'
     # /tmp/d20130203-23049-zhm90i/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)>'

  2) Collection can be filtered by song name
     Failure/Error: filtered.map(&:album).should =~ ['The Soul Cages', "Ten Summoner's Tales"]
     ArgumentError:
       no receiver given
     # /tmp/d20130203-23049-zhm90i/solution.rb:33:in `each'
     # /tmp/d20130203-23049-zhm90i/spec.rb:50:in `map'
     # /tmp/d20130203-23049-zhm90i/spec.rb:50: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 album
     Failure/Error: filtered.map(&:name).should =~ ['Fields of Gold', 'Autumn Leaves']
     ArgumentError:
       no receiver given
     # /tmp/d20130203-23049-zhm90i/solution.rb:33:in `each'
     # /tmp/d20130203-23049-zhm90i/spec.rb:55:in `map'
     # /tmp/d20130203-23049-zhm90i/spec.rb:55: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 return an empty result
     Failure/Error: filtered.to_a.should eq []
       
       expected: []
            got: [nil]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[]
       +[nil]
     # /tmp/d20130203-23049-zhm90i/spec.rb:60: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 supports a conjuction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & Criteria.name('Fields of Gold')
     TypeError:
       [:artist, :name] is not a symbol
     # /tmp/d20130203-23049-zhm90i/solution.rb:39:in `block in filter'
     # /tmp/d20130203-23049-zhm90i/solution.rb:38:in `select'
     # /tmp/d20130203-23049-zhm90i/solution.rb:38:in `filter'
     # /tmp/d20130203-23049-zhm90i/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)>'

  6) Collection supports a disjunction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') | Criteria.name('Fields of Gold')
     NoMethodError:
       undefined method `type' for nil:NilClass
     # /tmp/d20130203-23049-zhm90i/solution.rb:39:in `block in filter'
     # /tmp/d20130203-23049-zhm90i/solution.rb:38:in `select'
     # /tmp/d20130203-23049-zhm90i/solution.rb:38:in `filter'
     # /tmp/d20130203-23049-zhm90i/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)>'

  7) Collection supports negation of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & !Criteria.name('Fields of Gold')
     TypeError:
       [:artist, ["!", :name]] is not a symbol
     # /tmp/d20130203-23049-zhm90i/solution.rb:39:in `block in filter'
     # /tmp/d20130203-23049-zhm90i/solution.rb:38:in `select'
     # /tmp/d20130203-23049-zhm90i/solution.rb:38:in `filter'
     # /tmp/d20130203-23049-zhm90i/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)>'

  8) Collection can be adjoined with another collection
     Failure/Error: adjoined.count.should eq 4
       
       expected: 4
            got: 1
       
       (compared using ==)
     # /tmp/d20130203-23049-zhm90i/spec.rb:87: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.01674 seconds
11 examples, 8 failures

Failed examples:

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

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

Атанас обнови решението на 31.10.2012 16:33 (преди над 11 години)

+class Collection
+ include Enumerable
+
+ attr_accessor :songs
+
+ def initialize songs
+ @songs = songs
+ end
+
+ def Collection.parse text
+ songs, help_array = [], []
+ text.each_line { |line| help_array << line.chomp }
+ help_array.each_slice(4) { |l1| songs << Song.new(l1) }
+ Collection.new songs
+ end
+
+ def artists
+ artists = @songs.map { |song| song.artist }
+ artists.uniq
+ end
+
+ def names
+ names = @songs.map { |song| song.name }
+ names.uniq
+ end
+
+ def albums
+ albums = @songs.map { |song| song.album }
+ albums.uniq
+ end
+
+ def each
+ yield
+ self
+ end
+
+ def filter criteria
+ new_collection = @songs.select do |song|
+ song.send(criteria.type) == criteria.name
+ end
+ Collection.new new_collection
+ end
+
+ def adjoin other
+ collection = @songs.concat other.songs
+ Collection.new collection.uniq
+ end
+end
+
+class Song
+ attr_reader :name, :artist, :album
+
+ def initialize new_song
+ @name = new_song[0]
+ @artist = new_song[1]
+ @album = new_song[2]
+ end
+end
+
+class Criteria < Collection
+ attr_accessor :type, :name
+
+ def initialize type, name
+ @type = type
+ @name = name
+ end
+
+ def Criteria.name name_of_song
+ type = :name
+ name = name_of_song
+ Criteria.new type, name
+ end
+
+ def Criteria.artist name_of_artist
+ type = :artist
+ name = name_of_artist
+ Criteria.new type, name
+ end
+
+ def Criteria.album name_of_album
+ type = :album
+ name = name_of_album
+ Criteria.new type, name
+ end
+
+ def | other
+ puts self
+ #songs.select do |song|
+ # puts song.send(type) == name || song.send(other.type) == other.name
+ # end
+ #type << criteria.type
+ # name << criteria.name
+ #Criteria.new type, name
+ end
+
+ def & criteria
+ Criteria.new [type, criteria.type], [name, criteria.name]
+ end
+
+ def !
+ Criteria.new ['!', type], [name]
+ end
+end