Решение на Втора задача от Николай Димитров

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

Към профила на Николай Димитров

Резултати

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

Код

class Collection
include Enumerable
attr_reader :songs, :names, :artists, :albums
def initialize(songs)
@names = songs.map { |song| song.name }.uniq
@artists = songs.map { |song| song.artist }.uniq
@albums = songs.map { |song| song.album }.uniq
@songs = songs
end
def each
@songs.map { |song| yield song }
end
def self.parse(text)
sliced_array = text.split("\n").select { |line| !line.empty? }.each_slice(3)
songs = sliced_array.map { |song| Song.new(song[0], song[1], song[2]) }
Collection.new(songs)
end
def filter(criteria = Criteria.new { true })
new_subset = @songs.select { |song| song.match? criteria }
Collection.new(new_subset)
end
def adjoin(other_subset)
Collection.new(@songs.to_a | other_subset.to_a)
end
end
class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def match?(criteria)
criteria.call(self)
end
end
class Criteria
def initialize(criteria = Proc.new)
@criteria = criteria
end
def call(parameter)
@criteria.call(parameter)
end
def self.name(name)
Criteria.new { |song| song.name == name }
end
def self.artist(artist)
Criteria.new { |song| song.artist == artist }
end
def self.album(album)
Criteria.new { |song| song.album == album }
end
def &(other)
Criteria.new { |song| song.match?(self) && song.match?(other) }
end
def |(other)
Criteria.new { |song| song.match?(self) || song.match?(other) }
end
def !
Criteria.new { |song| !song.match?(self) }
end
end

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

...........

Finished in 0.01154 seconds
11 examples, 0 failures

История (7 версии и 6 коментара)

Николай обнови решението на 25.10.2012 01:15 (преди около 12 години)

+class Collection
+ def Collection.parse(text)
+ a = text.split("\n").select { |row| row!='' }
+ @names = 0.step(a.size-1, 3).map { |number| a[number] }
+ @artists = 1.step(a.size-1, 3).map { |number| a[number] }
+ @albums = 2.step(a.size-1, 3).map { |number| a[number] }
+ end
+
+end

Николай обнови решението на 25.10.2012 23:32 (преди около 12 години)

class Collection
- def Collection.parse(text)
- a = text.split("\n").select { |row| row!='' }
- @names = 0.step(a.size-1, 3).map { |number| a[number] }
- @artists = 1.step(a.size-1, 3).map { |number| a[number] }
- @albums = 2.step(a.size-1, 3).map { |number| a[number] }
+ include Enumerable
+
+ attr_reader :songs
+
+ def initialize(songs)
+ @songs = songs
+ #self
end
+ def each
+ current = 0
+ while current < @songs.size
+ yield @songs[current]
+ current += 1
+ end
+ end
+
+ def self.parse(text)
+ sliced_array = text.split("\n").select { |line| line!='' }.each_slice(3)
+ songs = sliced_array.map { |song| Song.new(song[0], song[1], song[2]) }
+ Collection.new(songs)
+ end
+
+ def filter(c = Criteria.new)
+ Collection.new(subset = @songs.select do |song|
+ (!(c.criteria.key? :name) ||c.criteria[:name].include?(song.name)) &&
+ (!(c.criteria.key? :artist)||c.criteria[:artist].include?(song.artist)) &&
+ (!(c.criteria.key? :album) ||c.criteria[:album].include?(song.album))
+ end)
+ end
+
+ def adjoin(other_subset)
+ Collection.new(@songs.to_a|other_subset.to_a)
+ end
+
+ def names
+ @songs.map { |song| song.name }.uniq
+ end
+
+ def artists
+ @songs.map { |song| song.artist }.uniq
+ end
+
+ def albums
+ @songs.map { |song| song.album }.uniq
+ end
+end
+
+class Song
+ attr_reader :name, :artist, :album
+
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+
+ def inspect
+ "%-20s %-20s %-20s\n" % [@name, @artist, @album]
+ end
+end
+
+
+class Criteria
+ attr_reader :criteria
+
+ def initialize(criteria = {})
+ @criteria = criteria
+ end
+
+ def &(other)
+ new_criteria = @criteria.merge(other.criteria) { | k, v1, v2| v1|v2 }
+ Criteria.new(new_criteria)
+ end
+
+ def self.name(name)
+ Criteria.new({:name => [name]})
+ end
+
+ def self.artist(artist)
+ Criteria.new({:artist => [artist]})
+ end
+
+ def self.album(album)
+ Criteria.new({:album => [album]})
+ end
end

Не подавай толкова дълги неща на конструктора на collection на 26-27ми ред. Изнеси го на променлива и подай променливата. Ако не се събира в изискванията, помисли дали този код не е по-подходящ за някъде другаде.

За тоя while ще ти взема поне две точки.

Николай обнови решението на 26.10.2012 09:47 (преди около 12 години)

class Collection
include Enumerable
attr_reader :songs
def initialize(songs)
@songs = songs
- #self
end
def each
- current = 0
- while current < @songs.size
- yield @songs[current]
- current += 1
- end
+ @songs.map { |song| yield song }
end
def self.parse(text)
sliced_array = text.split("\n").select { |line| line!='' }.each_slice(3)
songs = sliced_array.map { |song| Song.new(song[0], song[1], song[2]) }
Collection.new(songs)
end
def filter(c = Criteria.new)
Collection.new(subset = @songs.select do |song|
- (!(c.criteria.key? :name) ||c.criteria[:name].include?(song.name)) &&
- (!(c.criteria.key? :artist)||c.criteria[:artist].include?(song.artist)) &&
- (!(c.criteria.key? :album) ||c.criteria[:album].include?(song.album))
+ (!c.criteria.key?(:name) ||c.criteria[:name].include?(song.name)) &&
+ (!c.criteria.key?(:artist)||c.criteria[:artist].include?(song.artist)) &&
+ (!c.criteria.key?(:album) ||c.criteria[:album].include?(song.album))
end)
- end
+end
def adjoin(other_subset)
- Collection.new(@songs.to_a|other_subset.to_a)
+ Collection.new(@songs.to_a | other_subset.to_a)
end
def names
@songs.map { |song| song.name }.uniq
end
def artists
@songs.map { |song| song.artist }.uniq
end
def albums
@songs.map { |song| song.album }.uniq
end
end
class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def inspect
"%-20s %-20s %-20s\n" % [@name, @artist, @album]
end
end
class Criteria
attr_reader :criteria
def initialize(criteria = {})
@criteria = criteria
end
def &(other)
- new_criteria = @criteria.merge(other.criteria) { | k, v1, v2| v1|v2 }
- Criteria.new(new_criteria)
+ new_criteria = @criteria.merge(other.criteria) { |k, v1, v2| v1 | v2 }
+ Criteria.new(new_criteria)
end
def self.name(name)
Criteria.new({:name => [name]})
end
def self.artist(artist)
Criteria.new({:artist => [artist]})
end
def self.album(album)
Criteria.new({:album => [album]})
end
end

Николай обнови решението на 26.10.2012 12:32 (преди около 12 години)

class Collection
include Enumerable
- attr_reader :songs
+ attr_reader :songs, :names, :artists, :albums
def initialize(songs)
- @songs = songs
+ @names = songs.map { |song| song.name }.uniq
+ @artists = songs.map { |song| song.artist }.uniq
+ @albums = songs.map { |song| song.album }.uniq
+ @songs = songs
end
def each
@songs.map { |song| yield song }
end
def self.parse(text)
- sliced_array = text.split("\n").select { |line| line!='' }.each_slice(3)
+ sliced_array = text.split("\n").select { |line| !line.empty? }.each_slice(3)
songs = sliced_array.map { |song| Song.new(song[0], song[1], song[2]) }
Collection.new(songs)
end
- def filter(c = Criteria.new)
- Collection.new(subset = @songs.select do |song|
- (!c.criteria.key?(:name) ||c.criteria[:name].include?(song.name)) &&
- (!c.criteria.key?(:artist)||c.criteria[:artist].include?(song.artist)) &&
- (!c.criteria.key?(:album) ||c.criteria[:album].include?(song.album))
- end)
-end
+ def filter(criteria = Criteria.new)
+ new_subset = @songs.select do |song|
+ song.match? criteria
+ end
+ Collection.new(new_subset)
+ end
def adjoin(other_subset)
Collection.new(@songs.to_a | other_subset.to_a)
end
- def names
- @songs.map { |song| song.name }.uniq
- end
-
- def artists
- @songs.map { |song| song.artist }.uniq
- end
-
- def albums
- @songs.map { |song| song.album }.uniq
- end
end
class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
+ def match?(criteria)
+ criteria.all? do |key, value|
+ value.include? send(key)
+ end
+ end
+
def inspect
"%-20s %-20s %-20s\n" % [@name, @artist, @album]
end
end
class Criteria
attr_reader :criteria
def initialize(criteria = {})
@criteria = criteria
end
- def &(other)
- new_criteria = @criteria.merge(other.criteria) { |k, v1, v2| v1 | v2 }
- Criteria.new(new_criteria)
+ def method_missing(name, *args, &block)
+ @criteria.send(name, *args, &block)
+ end
+
+ def &(other_criteria)
+ merged_criteria = @criteria.merge(other_criteria) { |k, v1, v2| v1 | v2 }
+ Criteria.new(merged_criteria)
end
def self.name(name)
Criteria.new({:name => [name]})
end
def self.artist(artist)
Criteria.new({:artist => [artist]})
end
def self.album(album)
Criteria.new({:album => [album]})
end
end

Николай обнови решението на 27.10.2012 21:39 (преди около 12 години)

class Collection
include Enumerable
attr_reader :songs, :names, :artists, :albums
def initialize(songs)
@names = songs.map { |song| song.name }.uniq
@artists = songs.map { |song| song.artist }.uniq
@albums = songs.map { |song| song.album }.uniq
@songs = songs
end
def each
@songs.map { |song| yield song }
end
def self.parse(text)
sliced_array = text.split("\n").select { |line| !line.empty? }.each_slice(3)
songs = sliced_array.map { |song| Song.new(song[0], song[1], song[2]) }
Collection.new(songs)
end
- def filter(criteria = Criteria.new)
- new_subset = @songs.select do |song|
- song.match? criteria
- end
+ def filter(criteria = Criteria.new { true })
+ new_subset = @songs.select { |song| song.match? criteria }
Collection.new(new_subset)
end
def adjoin(other_subset)
Collection.new(@songs.to_a | other_subset.to_a)
end
+ def inspect
+ @songs.map { |song| song.inspect }.each_slice(1) { |a| puts a.join("\n")}
+ end
end
+
class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def match?(criteria)
- criteria.all? do |key, value|
- value.include? send(key)
- end
+ criteria.(self)
end
def inspect
- "%-20s %-20s %-20s\n" % [@name, @artist, @album]
+ "%-25s %-25s %-25s" % [@name, @artist, @album]
end
end
class Criteria
+
attr_reader :criteria
- def initialize(criteria = {})
+ def initialize(criteria = Proc.new)
@criteria = criteria
end
def method_missing(name, *args, &block)
@criteria.send(name, *args, &block)
end
- def &(other_criteria)
- merged_criteria = @criteria.merge(other_criteria) { |k, v1, v2| v1 | v2 }
- Criteria.new(merged_criteria)
- end
-
def self.name(name)
- Criteria.new({:name => [name]})
+ Criteria.new { |song| song.name == name }
end
def self.artist(artist)
- Criteria.new({:artist => [artist]})
+ Criteria.new { |song| song.artist == artist }
end
def self.album(album)
- Criteria.new({:album => [album]})
+ Criteria.new { |song| song.album == album }
end
-end
+
+ def &(other)
+ Criteria.new { |song| song.match?(self) && song.match?(other) }
+ end
+
+ def |(other)
+ Criteria.new { |song| song.match?(self) || song.match?(other) }
+ end
+
+ def !
+ Criteria.new { |song| !song.match?(self) }
+ end
+end
+
+

Николай обнови решението на 27.10.2012 21:54 (преди около 12 години)

class Collection
include Enumerable
attr_reader :songs, :names, :artists, :albums
def initialize(songs)
@names = songs.map { |song| song.name }.uniq
@artists = songs.map { |song| song.artist }.uniq
@albums = songs.map { |song| song.album }.uniq
@songs = songs
end
def each
@songs.map { |song| yield song }
end
def self.parse(text)
sliced_array = text.split("\n").select { |line| !line.empty? }.each_slice(3)
songs = sliced_array.map { |song| Song.new(song[0], song[1], song[2]) }
Collection.new(songs)
end
def filter(criteria = Criteria.new { true })
new_subset = @songs.select { |song| song.match? criteria }
Collection.new(new_subset)
end
def adjoin(other_subset)
Collection.new(@songs.to_a | other_subset.to_a)
end
- def inspect
+ def to_s
@songs.map { |song| song.inspect }.each_slice(1) { |a| puts a.join("\n")}
end
end
class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def match?(criteria)
criteria.(self)
end
- def inspect
- "%-25s %-25s %-25s" % [@name, @artist, @album]
+ def to_s
+ "%-25s %-25s %-26s" % [@name, @artist, @album]
end
end
class Criteria
-
- attr_reader :criteria
-
def initialize(criteria = Proc.new)
@criteria = criteria
end
def method_missing(name, *args, &block)
@criteria.send(name, *args, &block)
end
def self.name(name)
Criteria.new { |song| song.name == name }
end
def self.artist(artist)
Criteria.new { |song| song.artist == artist }
end
def self.album(album)
Criteria.new { |song| song.album == album }
end
def &(other)
Criteria.new { |song| song.match?(self) && song.match?(other) }
end
def |(other)
Criteria.new { |song| song.match?(self) || song.match?(other) }
end
def !
Criteria.new { |song| !song.match?(self) }
end
end
-
-

Николай обнови решението на 31.10.2012 09:49 (преди около 12 години)

class Collection
include Enumerable
attr_reader :songs, :names, :artists, :albums
def initialize(songs)
@names = songs.map { |song| song.name }.uniq
@artists = songs.map { |song| song.artist }.uniq
@albums = songs.map { |song| song.album }.uniq
@songs = songs
end
def each
@songs.map { |song| yield song }
end
def self.parse(text)
sliced_array = text.split("\n").select { |line| !line.empty? }.each_slice(3)
songs = sliced_array.map { |song| Song.new(song[0], song[1], song[2]) }
Collection.new(songs)
end
def filter(criteria = Criteria.new { true })
new_subset = @songs.select { |song| song.match? criteria }
Collection.new(new_subset)
end
def adjoin(other_subset)
Collection.new(@songs.to_a | other_subset.to_a)
end
-
- def to_s
- @songs.map { |song| song.inspect }.each_slice(1) { |a| puts a.join("\n")}
- end
end
class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def match?(criteria)
- criteria.(self)
+ criteria.call(self)
end
-
- def to_s
- "%-25s %-25s %-26s" % [@name, @artist, @album]
- end
end
class Criteria
def initialize(criteria = Proc.new)
@criteria = criteria
end
- def method_missing(name, *args, &block)
- @criteria.send(name, *args, &block)
+ def call(parameter)
+ @criteria.call(parameter)
end
def self.name(name)
Criteria.new { |song| song.name == name }
end
def self.artist(artist)
Criteria.new { |song| song.artist == artist }
end
def self.album(album)
Criteria.new { |song| song.album == album }
end
def &(other)
Criteria.new { |song| song.match?(self) && song.match?(other) }
end
def |(other)
Criteria.new { |song| song.match?(self) || song.match?(other) }
end
def !
Criteria.new { |song| !song.match?(self) }
end
-end
+end