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

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

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

Резултати

  • 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

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

▸ Покажи лога

История (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