Решение на Втора задача от Тихомир Янев

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

Към профила на Тихомир Янев

Резултати

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

Код

class Core
include Enumerable
attr_writer :dbase
def filter(criteria)
key, value = criteria.keys[0].to_i, criteria.values[0]
sub_list = Playlist.new
sub_list.dbase = subset(@dbase[key].select { |x| x.first == value }, key)
end
private
def get_nth_line(n = 1)
@dbase[n].inject([]) { |array, elem| array << elem.first }.uniq
end
def subset(array, index)
storage = []
array.each do |elem|
storage.push(item(elem.last), item(elem.last + 1), item(elem.last + 2))
end
storage.each_with_index.group_by { |val, key| key % 3 }
end
def songs_list
@dbase[0].inject([]) do |array, row|
array << Song.new(row.first, item(row.last + 1), item(row.last + 2))
end
end
def item(n)
@dbase[n % 3][(n / 3).to_i].first
end
end
class Collection < Core
def self.parse(string, fields_in_song = 3)
playlist = Playlist.new
playlist.dbase = string.split(/\n/).reject { |elem| elem.empty? }
.each_with_index.group_by { |val, key| key % fields_in_song }
playlist
end
def artists
get_nth_line(1)
end
def names
songs_list
end
def albums
get_nth_line(2)
end
end
class Playlist < Collection
def each
# TODO Enumerable implementation
end
end
class Song
attr_accessor :name, :artist, :album
def initialize(*args)
args[2] = '' if args.count < 3
@name, @artist, @album = args[0..2]
end
end
class Criteria
def self.name(string = '')
Hash[0 => string]
end
def self.artist(string = '')
Hash[1 => string]
end
def self.album(string = '')
Hash[2 => string]
end
end

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

.F.FFF.FFFF

Failures:

  1) Collection can find all the names of all songs in the collection
     Failure/Error: ]
       expected collection contained:  ["Acknowledgment", "Autumn Leaves", "Brain of J.F.K", "Come Away With Me", "Fields of Gold", "Jeremy", "Mad About You", "Ruby, My Dear"]
       actual collection contained:    [#<Song:0x87d79c4 @name="Fields of Gold", @artist="Sting", @album="Ten Summoner's Tales">, #<Song:0x87d780c @name="Mad About You", @artist="Sting", @album="The Soul Cages">, #<Song:0x87d76e0 @name="Fields of Gold", @artist="Eva Cassidy", @album="Live at Blues Alley">, #<Song:0x87d7460 @name="Autumn Leaves", @artist="Eva Cassidy", @album="Live at Blues Alley">, #<Song:0x87d726c @name="Autumn Leaves", @artist="Bill Evans", @album="Portrait in Jazz">, #<Song:0x87d6f9c @name="Brain of J.F.K", @artist="Pearl Jam", @album="Yield">, #<Song:0x87d6d30 @name="Jeremy", @artist="Pearl Jam", @album="Ten">, #<Song:0x87d63f8 @name="Come Away With Me", @artist="Norah Johnes", @album="One">, #<Song:0x87d613c @name="Acknowledgment", @artist="John Coltrane", @album="A Love Supreme">, #<Song:0x87d5ac0 @name="Ruby, My Dear", @artist="Thelonious Monk", @album="Mysterioso">]
       the missing elements were:      ["Acknowledgment", "Autumn Leaves", "Brain of J.F.K", "Come Away With Me", "Fields of Gold", "Jeremy", "Mad About You", "Ruby, My Dear"]
       the extra elements were:        [#<Song:0x87d79c4 @name="Fields of Gold", @artist="Sting", @album="Ten Summoner's Tales">, #<Song:0x87d780c @name="Mad About You", @artist="Sting", @album="The Soul Cages">, #<Song:0x87d76e0 @name="Fields of Gold", @artist="Eva Cassidy", @album="Live at Blues Alley">, #<Song:0x87d7460 @name="Autumn Leaves", @artist="Eva Cassidy", @album="Live at Blues Alley">, #<Song:0x87d726c @name="Autumn Leaves", @artist="Bill Evans", @album="Portrait in Jazz">, #<Song:0x87d6f9c @name="Brain of J.F.K", @artist="Pearl Jam", @album="Yield">, #<Song:0x87d6d30 @name="Jeremy", @artist="Pearl Jam", @album="Ten">, #<Song:0x87d63f8 @name="Come Away With Me", @artist="Norah Johnes", @album="One">, #<Song:0x87d613c @name="Acknowledgment", @artist="John Coltrane", @album="A Love Supreme">, #<Song:0x87d5ac0 @name="Ruby, My Dear", @artist="Thelonious Monk", @album="Mysterioso">]
     # /tmp/d20130203-23049-9dt4m0/spec.rb:26: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(&:artist).should =~ ['Eva Cassidy', 'Sting']
     NoMethodError:
       undefined method `artist' for [0, [["Fields of Gold", 0], ["Fields of Gold", 3]]]:Array
     # /tmp/d20130203-23049-9dt4m0/spec.rb:45:in `each'
     # /tmp/d20130203-23049-9dt4m0/spec.rb:45:in `map'
     # /tmp/d20130203-23049-9dt4m0/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)>'

  3) Collection can be filtered by song name
     Failure/Error: filtered.map(&:album).should =~ ['The Soul Cages', "Ten Summoner's Tales"]
     NoMethodError:
       undefined method `album' for [0, [["Sting", 0], ["Sting", 3]]]:Array
     # /tmp/d20130203-23049-9dt4m0/spec.rb:50:in `each'
     # /tmp/d20130203-23049-9dt4m0/spec.rb:50:in `map'
     # /tmp/d20130203-23049-9dt4m0/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)>'

  4) Collection can be filtered by album
     Failure/Error: filtered.map(&:name).should =~ ['Fields of Gold', 'Autumn Leaves']
     NoMethodError:
       undefined method `name' for [0, [["Live at Blues Alley", 0], ["Live at Blues Alley", 3]]]:Array
     # /tmp/d20130203-23049-9dt4m0/spec.rb:55:in `each'
     # /tmp/d20130203-23049-9dt4m0/spec.rb:55:in `map'
     # /tmp/d20130203-23049-9dt4m0/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)>'

  5) Collection supports a conjuction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & Criteria.name('Fields of Gold')
     NoMethodError:
       undefined method `&' for {1=>"Sting"}:Hash
     # /tmp/d20130203-23049-9dt4m0/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 `|' for {1=>"Sting"}:Hash
     # /tmp/d20130203-23049-9dt4m0/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')
     NoMethodError:
       undefined method `&' for {1=>"Sting"}:Hash
     # /tmp/d20130203-23049-9dt4m0/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 = sting.adjoin(eva)
     NoMethodError:
       undefined method `adjoin' for #<Hash:0x9051c30>
     # /tmp/d20130203-23049-9dt4m0/spec.rb:85: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.01155 seconds
11 examples, 8 failures

Failed examples:

rspec /tmp/d20130203-23049-9dt4m0/spec.rb:16 # Collection can find all the names of all songs in the collection
rspec /tmp/d20130203-23049-9dt4m0/spec.rb:43 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-9dt4m0/spec.rb:48 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-9dt4m0/spec.rb:53 # Collection can be filtered by album
rspec /tmp/d20130203-23049-9dt4m0/spec.rb:63 # Collection supports a conjuction of filters
rspec /tmp/d20130203-23049-9dt4m0/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-9dt4m0/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-9dt4m0/spec.rb:82 # Collection can be adjoined with another collection

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

Тихомир обнови решението на 31.10.2012 16:20 (преди над 12 години)

+class Core
+ include Enumerable
+ attr_writer :dbase
+
+ def filter(criteria)
+ key, value = criteria.keys[0].to_i, criteria.values[0]
+ sub_list = Playlist.new
+ sub_list.dbase = subset(@dbase[key].select { |x| x.first == value }, key)
+ end
+
+ private
+
+ def get_nth_line(n = 1)
+ @dbase[n].inject([]) { |array, elem| array << elem.first }.uniq
+ end
+
+ def subset(array, index)
+ storage = []
+ array.each do |elem|
+ storage.push(item(elem.last), item(elem.last + 1), item(elem.last + 2))
+ end
+ storage.each_with_index.group_by { |val, key| key % 3 }
+ end
+
+ def songs_list
+ @dbase[0].inject([]) do |array, row|
+ array << Song.new(row.first, item(row.last + 1), item(row.last + 2))
+ end
+ end
+
+ def item(n)
+ @dbase[n % 3][(n / 3).to_i].first
+ end
+end
+
+class Collection < Core
+ def self.parse(string, fields_in_song = 3)
+ playlist = Playlist.new
+ playlist.dbase = string.split(/\n/).reject { |elem| elem.empty? }
+ .each_with_index.group_by { |val, key| key % fields_in_song }
+ playlist
+ end
+
+ def artists
+ get_nth_line(1)
+ end
+
+ def names
+ songs_list
+ end
+
+ def albums
+ get_nth_line(2)
+ end
+end
+
+class Playlist < Collection
+ def each
+ # TODO Enumerable implementation
+ end
+end
+
+class Song
+ attr_accessor :name, :artist, :album
+
+ def initialize(*args)
+ args[2] = '' if args.count < 3
+ @name, @artist, @album = args[0..2]
+ end
+end
+
+class Criteria
+ def self.name(string = '')
+ Hash[0 => string]
+ end
+
+ def self.artist(string = '')
+ Hash[1 => string]
+ end
+
+ def self.album(string = '')
+ Hash[2 => string]
+ end
+end