Решение на Втора задача от Орлин Христов

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

Към профила на Орлин Христов

Резултати

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

Код

class Track
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
def <=>(other)
name <=> other.name
end
end
class Criteria
def self.name(track_name)
Filter.new { |track| track.name == track_name }
end
def self.artist(track_artist)
Filter.new { |track| track.artist == track_artist }
end
def self.album(track_album)
Filter.new { |track| track.album == track_album }
end
end
class Filter
def initialize
@validator = Proc.new
end
def match?(track)
@validator.call track
end
def !
Filter.new { |track| !match? track }
end
def &(other)
Filter.new { |track| match?(track) & other.match?(track) }
end
def |(other)
Filter.new { |track| match?(track) | other.match?(track) }
end
end
class Collection
def self.parse(data)
tracks = data.split("\n\n").map do |track_data|
Track.new *(track_data.split "\n")
end
new tracks
end
include Enumerable
def initialize(tracks)
@tracks = tracks
end
def each(&block)
@tracks.each &block
end
def <<(track)
@tracks << track
end
def filter(track_filter)
Collection.new select { |track| track_filter.match? track }
end
def adjoin(collection)
collection.each { |track| self << track }
self
end
def names
map { |track| track.name }.uniq
end
def artists
map { |track| track.artist }.uniq
end
def albums
map { |track| track.album }.uniq
end
end

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

...........

Finished in 0.01003 seconds
11 examples, 0 failures

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

Орлин обнови решението на 30.10.2012 20:56 (преди около 12 години)

+class Track
+ attr_reader :name, :artist, :album
+
+ def initialize(track_data)
+ @name, @artist, @album = track_data.split("\n")
+ end
+
+ def to_s
+ "#{artist} - #{name}"
+ end
+end
+
+class Criteria
+ def self.album(track_album)
+ new Proc.new { |track| track.album == track_album }
+ end
+
+ def self.name(track_name)
+ new Proc.new { |track| track.name == track_name }
+ end
+
+ def self.artist(track_artist)
+ new Proc.new { |track| track.artist == track_artist }
+ end
+
+ def initialize(filter)
+ @filter = filter
+ end
+
+ def meet(track)
+ @filter.call(track)
+ end
+
+ def !
+ Criteria.new Proc.new { |track| !meet track }
+ end
+
+ def &(other)
+ Criteria.new Proc.new { |track| meet(track) & other.meet(track) }
+ end
+
+ def |(other)
+ Criteria.new Proc.new { |track| meet(track) | other.meet(track) }
+ end
+end
+
+
+class Collection < Array
+ def self.parse(data)
+ data.split("\n\n").each_with_object(Collection.new) do |track_info, tracks|
+ tracks << Track.new(track_info)
+ end
+ end
+
+ def filter(criteria)
+ Collection.new.adjoin select { |track| criteria.meet track }
+ end
+
+ def adjoin(collection)
+ collection.each { |track| self << track }
+ self
+ end
+end

Орлин обнови решението на 30.10.2012 21:05 (преди около 12 години)

class Track
attr_reader :name, :artist, :album
def initialize(track_data)
@name, @artist, @album = track_data.split("\n")
end
- def to_s
- "#{artist} - #{name}"
- end
+# def to_s
+# "#{artist} - #{name}"
+# end
end
class Criteria
def self.album(track_album)
- new Proc.new { |track| track.album == track_album }
+ new { |track| track.album == track_album }
end
def self.name(track_name)
- new Proc.new { |track| track.name == track_name }
+ new { |track| track.name == track_name }
end
def self.artist(track_artist)
- new Proc.new { |track| track.artist == track_artist }
+ new { |track| track.artist == track_artist }
end
- def initialize(filter)
- @filter = filter
+ def initialize
+ @filter = Proc.new
end
def meet(track)
@filter.call(track)
end
def !
- Criteria.new Proc.new { |track| !meet track }
+ Criteria.new { |track| !meet track }
end
def &(other)
- Criteria.new Proc.new { |track| meet(track) & other.meet(track) }
+ Criteria.new { |track| meet(track) & other.meet(track) }
end
def |(other)
- Criteria.new Proc.new { |track| meet(track) | other.meet(track) }
+ Criteria.new { |track| meet(track) | other.meet(track) }
end
end
class Collection < Array
def self.parse(data)
data.split("\n\n").each_with_object(Collection.new) do |track_info, tracks|
tracks << Track.new(track_info)
end
end
def filter(criteria)
Collection.new.adjoin select { |track| criteria.meet track }
end
def adjoin(collection)
collection.each { |track| self << track }
self
end
end

Орлин обнови решението на 30.10.2012 21:23 (преди около 12 години)

class Track
attr_reader :name, :artist, :album
def initialize(track_data)
@name, @artist, @album = track_data.split("\n")
end
# def to_s
# "#{artist} - #{name}"
# end
end
class Criteria
- def self.album(track_album)
- new { |track| track.album == track_album }
- end
-
def self.name(track_name)
new { |track| track.name == track_name }
end
def self.artist(track_artist)
new { |track| track.artist == track_artist }
end
+ def self.album(track_album)
+ new { |track| track.album == track_album }
+ end
+
+
def initialize
@filter = Proc.new
end
def meet(track)
@filter.call(track)
end
def !
Criteria.new { |track| !meet track }
end
def &(other)
Criteria.new { |track| meet(track) & other.meet(track) }
end
def |(other)
Criteria.new { |track| meet(track) | other.meet(track) }
end
end
class Collection < Array
def self.parse(data)
- data.split("\n\n").each_with_object(Collection.new) do |track_info, tracks|
- tracks << Track.new(track_info)
+ data.split("\n\n").each_with_object(Collection.new) do |track_data, tracks|
+ tracks << Track.new(track_data)
end
end
def filter(criteria)
Collection.new.adjoin select { |track| criteria.meet track }
end
def adjoin(collection)
collection.each { |track| self << track }
self
+ end
+
+ def names
+ map { |track| track.name }.uniq
+ end
+
+ def artists
+ map { |track| track.artist }.uniq
+ end
+
+ def albums
+ map { |track| track.album }.uniq
end
end

Орлин обнови решението на 30.10.2012 21:25 (преди около 12 години)

class Track
attr_reader :name, :artist, :album
def initialize(track_data)
- @name, @artist, @album = track_data.split("\n")
+ @name, @artist, @album = track_data.split "\n"
end
# def to_s
# "#{artist} - #{name}"
# end
end
class Criteria
def self.name(track_name)
new { |track| track.name == track_name }
end
def self.artist(track_artist)
new { |track| track.artist == track_artist }
end
def self.album(track_album)
new { |track| track.album == track_album }
end
def initialize
@filter = Proc.new
end
def meet(track)
- @filter.call(track)
+ @filter.call track
end
def !
Criteria.new { |track| !meet track }
end
def &(other)
Criteria.new { |track| meet(track) & other.meet(track) }
end
def |(other)
Criteria.new { |track| meet(track) | other.meet(track) }
end
end
class Collection < Array
def self.parse(data)
data.split("\n\n").each_with_object(Collection.new) do |track_data, tracks|
tracks << Track.new(track_data)
end
end
def filter(criteria)
Collection.new.adjoin select { |track| criteria.meet track }
end
def adjoin(collection)
collection.each { |track| self << track }
self
end
def names
map { |track| track.name }.uniq
end
def artists
map { |track| track.artist }.uniq
end
def albums
map { |track| track.album }.uniq
end
end

Орлин обнови решението на 30.10.2012 21:26 (преди около 12 години)

class Track
attr_reader :name, :artist, :album
def initialize(track_data)
@name, @artist, @album = track_data.split "\n"
end
-
-# def to_s
-# "#{artist} - #{name}"
-# end
end
class Criteria
def self.name(track_name)
new { |track| track.name == track_name }
end
def self.artist(track_artist)
new { |track| track.artist == track_artist }
end
def self.album(track_album)
new { |track| track.album == track_album }
end
def initialize
@filter = Proc.new
end
def meet(track)
@filter.call track
end
def !
Criteria.new { |track| !meet track }
end
def &(other)
Criteria.new { |track| meet(track) & other.meet(track) }
end
def |(other)
Criteria.new { |track| meet(track) | other.meet(track) }
end
end
class Collection < Array
def self.parse(data)
- data.split("\n\n").each_with_object(Collection.new) do |track_data, tracks|
+ data.split("\n\n").each_with_object Collection.new do |track_data, tracks|
tracks << Track.new(track_data)
end
end
def filter(criteria)
Collection.new.adjoin select { |track| criteria.meet track }
end
def adjoin(collection)
collection.each { |track| self << track }
self
end
def names
map { |track| track.name }.uniq
end
def artists
map { |track| track.artist }.uniq
end
def albums
map { |track| track.album }.uniq
end
-end
+end

Орлин обнови решението на 30.10.2012 21:32 (преди около 12 години)

class Track
attr_reader :name, :artist, :album
def initialize(track_data)
- @name, @artist, @album = track_data.split "\n"
+ @name, @artist, @album = track_data
end
end
class Criteria
def self.name(track_name)
new { |track| track.name == track_name }
end
def self.artist(track_artist)
new { |track| track.artist == track_artist }
end
def self.album(track_album)
new { |track| track.album == track_album }
end
def initialize
@filter = Proc.new
end
def meet(track)
@filter.call track
end
def !
Criteria.new { |track| !meet track }
end
def &(other)
Criteria.new { |track| meet(track) & other.meet(track) }
end
def |(other)
Criteria.new { |track| meet(track) | other.meet(track) }
end
end
class Collection < Array
def self.parse(data)
data.split("\n\n").each_with_object Collection.new do |track_data, tracks|
- tracks << Track.new(track_data)
+ tracks << Track.new(track_data.split "\n")
end
end
def filter(criteria)
Collection.new.adjoin select { |track| criteria.meet track }
end
def adjoin(collection)
collection.each { |track| self << track }
self
end
def names
map { |track| track.name }.uniq
end
def artists
map { |track| track.artist }.uniq
end
def albums
map { |track| track.album }.uniq
end
-end
+end

Орлин обнови решението на 30.10.2012 21:34 (преди около 12 години)

class Track
attr_reader :name, :artist, :album
def initialize(track_data)
@name, @artist, @album = track_data
end
end
+
class Criteria
def self.name(track_name)
new { |track| track.name == track_name }
end
def self.artist(track_artist)
new { |track| track.artist == track_artist }
end
def self.album(track_album)
new { |track| track.album == track_album }
end
def initialize
@filter = Proc.new
end
def meet(track)
@filter.call track
end
def !
Criteria.new { |track| !meet track }
end
def &(other)
Criteria.new { |track| meet(track) & other.meet(track) }
end
def |(other)
Criteria.new { |track| meet(track) | other.meet(track) }
end
end
class Collection < Array
def self.parse(data)
data.split("\n\n").each_with_object Collection.new do |track_data, tracks|
tracks << Track.new(track_data.split "\n")
end
end
def filter(criteria)
Collection.new.adjoin select { |track| criteria.meet track }
end
def adjoin(collection)
collection.each { |track| self << track }
self
end
def names
map { |track| track.name }.uniq
end
def artists
map { |track| track.artist }.uniq
end
def albums
map { |track| track.album }.uniq
end
end

Орлин обнови решението на 31.10.2012 12:03 (преди около 12 години)

class Track
attr_reader :name, :artist, :album
def initialize(track_data)
@name, @artist, @album = track_data
end
end
class Criteria
def self.name(track_name)
- new { |track| track.name == track_name }
+ Filter.new { |track| track.name == track_name }
end
def self.artist(track_artist)
- new { |track| track.artist == track_artist }
+ Filter.new { |track| track.artist == track_artist }
end
def self.album(track_album)
- new { |track| track.album == track_album }
+ Filter.new { |track| track.album == track_album }
end
+end
+class Filter
def initialize
- @filter = Proc.new
+ @validator = Proc.new
end
- def meet(track)
- @filter.call track
+ def match?(track)
+ @validator.call track
end
def !
- Criteria.new { |track| !meet track }
+ Filter.new { |track| !match? track }
end
def &(other)
- Criteria.new { |track| meet(track) & other.meet(track) }
+ Filter.new { |track| match?(track) & other.match?(track) }
end
def |(other)
- Criteria.new { |track| meet(track) | other.meet(track) }
+ Filter.new { |track| match?(track) | other.match?(track) }
end
end
class Collection < Array
def self.parse(data)
- data.split("\n\n").each_with_object Collection.new do |track_data, tracks|
+ data.split("\n\n").each_with_object new do |track_data, tracks|
tracks << Track.new(track_data.split "\n")
end
end
- def filter(criteria)
- Collection.new.adjoin select { |track| criteria.meet track }
+ def filter(track_filter)
+ Collection.new.adjoin select { |track| track_filter.match? track }
end
def adjoin(collection)
collection.each { |track| self << track }
self
end
def names
map { |track| track.name }.uniq
end
def artists
map { |track| track.artist }.uniq
end
def albums
map { |track| track.album }.uniq
end
-end
+end

Орлин обнови решението на 31.10.2012 14:14 (преди около 12 години)

class Track
attr_reader :name, :artist, :album
- def initialize(track_data)
- @name, @artist, @album = track_data
+ def initialize(name, artist, album)
+ @name = name
+ @artist = artist
+ @album = album
end
+
+ def <=>(other)
+ name <=> other.name
+ end
end
class Criteria
def self.name(track_name)
Filter.new { |track| track.name == track_name }
end
def self.artist(track_artist)
Filter.new { |track| track.artist == track_artist }
end
def self.album(track_album)
Filter.new { |track| track.album == track_album }
end
end
class Filter
def initialize
@validator = Proc.new
end
def match?(track)
@validator.call track
end
def !
Filter.new { |track| !match? track }
end
def &(other)
Filter.new { |track| match?(track) & other.match?(track) }
end
def |(other)
Filter.new { |track| match?(track) | other.match?(track) }
end
end
-class Collection < Array
+class Collection
def self.parse(data)
- data.split("\n\n").each_with_object new do |track_data, tracks|
- tracks << Track.new(track_data.split "\n")
+ tracks = data.split("\n\n").map do |track_data|
+ Track.new *(track_data.split "\n")
end
+ new tracks
end
+ include Enumerable
+
+ def initialize(tracks)
+ @tracks = tracks
+ end
+
+ def each(&block)
+ @tracks.each &block
+ end
+
+ def <<(track)
+ @tracks << track
+ end
+
def filter(track_filter)
- Collection.new.adjoin select { |track| track_filter.match? track }
+ Collection.new select { |track| track_filter.match? track }
end
def adjoin(collection)
collection.each { |track| self << track }
self
end
def names
map { |track| track.name }.uniq
end
def artists
map { |track| track.artist }.uniq
end
def albums
map { |track| track.album }.uniq
end
-end
+end