Решение на Втора задача от Елена Денева

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

Към профила на Елена Денева

Резултати

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

Код

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class Collection
attr_accessor :songs
include Enumerable
def initialize songs
@songs = songs
end
def Collection.parse(text)
songs = []
text.split("\n").each_slice(4) { |x| songs << Song.new(x[0], x[1], x[2]) }
Collection.new songs
end
def artists
songs.map { |x| x.artist }.uniq
end
def albums
songs.map { |x| x.album }.uniq
end
def names
songs.map { |x| x.name }.uniq
end
def each
@songs.each { |item| yield item }
end
def adjoin collection
Collection.new (@songs + collection.songs).uniq
end
def filter criteria
Collection.new songs.select {| item| criteria.desired?(item) }
end
end
class Criteria
attr_accessor :filters
def initialize filters
@filters = filters
end
def Criteria.name name
Criteria.new([{"name" => name}])
end
def Criteria.artist artist
Criteria.new([{"artist" => artist}])
end
def Criteria.album album
Criteria.new([{"album" => album}])
end
def &(other)
Criteria.new(["&"] + [@filters] + [other.filters])
end
def |(other)
Criteria.new(["|"] + [@filters] + [other.filters])
end
def !
Criteria.new(['!'] + [@filters])
end
def desired? song
case @filters.length
when 1 then return match? song
when 2 then return !Criteria.new(filters[1]).desired?(song)
else filterWithTwoConditions song
end
end
def filterWithTwoConditions song
condition1 = Criteria.new(@filters[1]).desired?(song)
condition2 = Criteria.new(@filters[2]).desired?(song)
return true if @filters[0] == "&" && condition1 && condition2
@filters[0] == "|" && (condition1 || condition2)
end
def match? song
return true if @filters[0]["name"] == song.name
return true if @filters[0]["artist"] == song.artist
@filters[0]["album"] == song.album
end
end

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

...........

Finished in 0.01054 seconds
11 examples, 0 failures

История (4 версии и 2 коментара)

Елена обнови решението на 26.10.2012 07:57 (преди около 12 години)

+class Song
+ attr_accessor :name, :artist, :album
+
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+end
+
+class Collection
+ attr_accessor :songs
+
+ include Enumerable
+
+ def initialize songs
+ @songs = songs
+ end
+
+ def Collection.parse(text)
+ songs = []
+ text.split("\n").each_slice(4) {|x| songs << Song.new(x[0], x[1], x[2])}
+ Collection.new songs
+ end
+
+ def artists
+ artistsArray = songs.map {|x| x.artist}.uniq
+ end
+
+ def albums
+ albumsArray = songs.map {|x| x.album}.uniq
+ end
+
+ def names
+ namesArray = songs.map {|x| x.name}.uniq
+ end
+
+ def each
+ @songs.each {|item| yield item}
+ end
+
+ def adjoin collection
+ return Collection.new (@songs + collection.songs).uniq
+ end
+
+ def filter criteria
+ Collection.new songs.select{|item| criteria.desired?(item)}
+ end
+end
+
+class Criteria
+ attr_accessor :filters
+
+ def initialize filters
+ @filters = filters
+ end
+
+ def Criteria.name name
+ Criteria.new([{"name" => name}])
+ end
+
+ def Criteria.artist artist
+ Criteria.new([{"artist" => artist}])
+ end
+
+ def Criteria.album album
+ Criteria.new([{"album" => album}])
+ end
+
+ def & other
+ Criteria.new(["&"] + @filters + other.filters)
+ end
+
+ def | other
+ Criteria.new(["|"] + @filters + other.filters)
+ end
+
+ def !
+ Criteria.new(['!'] + @filters)
+ end
+
+ def desired? song
+ case @filters.length
+ when 1 then return match? song
+ when 2 then return !Criteria.new([@filters[1]]).desired?(song)
+ else filterWithTwoConditions song
+ end
+ end
+
+ def filterWithTwoConditions song
+ condition1 = Criteria.new([@filters[1]]).desired?(song)
+ condition2 = Criteria.new([@filters[2]]).desired?(song)
+ return true if @filters[0] == "&" && condition1 && condition2
+ return @filters[0] == "|" && (condition1 || condition2)
+ end
+
+ private
+ def match? song
+ return true if @filters[0]["name"] == song.name
+ return true if @filters[0]["artist"] == song.artist
+ return true if @filters[0]["album"] == song.album
+ false
+ end
+end

Елена обнови решението на 26.10.2012 16:46 (преди около 12 години)

class Song
attr_accessor :name, :artist, :album
- def initialize(name, artist, album)
- @name, @artist, @album = name, artist, album
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
end
end
class Collection
attr_accessor :songs
include Enumerable
def initialize songs
@songs = songs
end
def Collection.parse(text)
songs = []
- text.split("\n").each_slice(4) {|x| songs << Song.new(x[0], x[1], x[2])}
+ text.split("\n").each_slice(4) { |x| songs << Song.new(x[0], x[1], x[2]) }
Collection.new songs
end
def artists
- artistsArray = songs.map {|x| x.artist}.uniq
+ songs.map { |x| x.artist }.uniq
end
def albums
- albumsArray = songs.map {|x| x.album}.uniq
+ songs.map { |x| x.album }.uniq
end
def names
- namesArray = songs.map {|x| x.name}.uniq
+ songs.map { |x| x.name }.uniq
end
def each
- @songs.each {|item| yield item}
+ @songs.each { |item| yield item }
end
def adjoin collection
- return Collection.new (@songs + collection.songs).uniq
+ Collection.new (@songs + collection.songs).uniq
end
def filter criteria
- Collection.new songs.select{|item| criteria.desired?(item)}
+ Collection.new songs.select {| item| criteria.desired?(item) }
end
end
class Criteria
attr_accessor :filters
def initialize filters
@filters = filters
end
def Criteria.name name
Criteria.new([{"name" => name}])
end
def Criteria.artist artist
- Criteria.new([{"artist" => artist}])
+ Criteria.new([{ "artist" => artist }])
end
def Criteria.album album
- Criteria.new([{"album" => album}])
+ Criteria.new([{ "album" => album }])
end
- def & other
+ def &(other)
Criteria.new(["&"] + @filters + other.filters)
end
- def | other
+ def |(other)
Criteria.new(["|"] + @filters + other.filters)
end
def !
Criteria.new(['!'] + @filters)
end
def desired? song
case @filters.length
- when 1 then return match? song
- when 2 then return !Criteria.new([@filters[1]]).desired?(song)
- else filterWithTwoConditions song
- end
+ when 1 then return match? song
+ when 2 then return !Criteria.new([@filters[1]]).desired?(song)
+ else filterWithTwoConditions song
+ end
end
def filterWithTwoConditions song
- condition1 = Criteria.new([@filters[1]]).desired?(song)
+ condition1 = Criteria.new([@filters[1]]).desired?(song)
condition2 = Criteria.new([@filters[2]]).desired?(song)
- return true if @filters[0] == "&" && condition1 && condition2
- return @filters[0] == "|" && (condition1 || condition2)
+ return true if @filters[0] == "&" && condition1 && condition2
+ @filters[0] == "|" && (condition1 || condition2)
end
- private
def match? song
return true if @filters[0]["name"] == song.name
return true if @filters[0]["artist"] == song.artist
- return true if @filters[0]["album"] == song.album
- false
+ true if @filters[0]["album"] == song.album
end
end

Елена обнови решението на 26.10.2012 20:16 (преди около 12 години)

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class Collection
attr_accessor :songs
include Enumerable
def initialize songs
@songs = songs
end
def Collection.parse(text)
songs = []
text.split("\n").each_slice(4) { |x| songs << Song.new(x[0], x[1], x[2]) }
Collection.new songs
end
def artists
songs.map { |x| x.artist }.uniq
end
def albums
songs.map { |x| x.album }.uniq
end
def names
songs.map { |x| x.name }.uniq
end
def each
@songs.each { |item| yield item }
end
def adjoin collection
Collection.new (@songs + collection.songs).uniq
end
def filter criteria
Collection.new songs.select {| item| criteria.desired?(item) }
end
end
class Criteria
attr_accessor :filters
def initialize filters
@filters = filters
end
def Criteria.name name
Criteria.new([{"name" => name}])
end
def Criteria.artist artist
Criteria.new([{ "artist" => artist }])
end
def Criteria.album album
Criteria.new([{ "album" => album }])
end
def &(other)
Criteria.new(["&"] + @filters + other.filters)
end
def |(other)
Criteria.new(["|"] + @filters + other.filters)
end
def !
Criteria.new(['!'] + @filters)
end
def desired? song
case @filters.length
when 1 then return match? song
when 2 then return !Criteria.new([@filters[1]]).desired?(song)
else filterWithTwoConditions song
end
end
def filterWithTwoConditions song
condition1 = Criteria.new([@filters[1]]).desired?(song)
condition2 = Criteria.new([@filters[2]]).desired?(song)
return true if @filters[0] == "&" && condition1 && condition2
@filters[0] == "|" && (condition1 || condition2)
end
def match? song
return true if @filters[0]["name"] == song.name
return true if @filters[0]["artist"] == song.artist
- true if @filters[0]["album"] == song.album
+ @filters[0]["album"] == song.album
end
end

Елена обнови решението на 29.10.2012 23:49 (преди около 12 години)

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
- @name, @artist, @album = name, artist, album
+ @name, @artist, @album = name, artist, album
end
end
class Collection
attr_accessor :songs
include Enumerable
def initialize songs
@songs = songs
end
def Collection.parse(text)
songs = []
text.split("\n").each_slice(4) { |x| songs << Song.new(x[0], x[1], x[2]) }
Collection.new songs
end
def artists
songs.map { |x| x.artist }.uniq
end
def albums
songs.map { |x| x.album }.uniq
end
def names
songs.map { |x| x.name }.uniq
end
def each
@songs.each { |item| yield item }
end
def adjoin collection
Collection.new (@songs + collection.songs).uniq
end
def filter criteria
Collection.new songs.select {| item| criteria.desired?(item) }
end
end
class Criteria
attr_accessor :filters
def initialize filters
@filters = filters
end
def Criteria.name name
Criteria.new([{"name" => name}])
end
def Criteria.artist artist
- Criteria.new([{ "artist" => artist }])
+ Criteria.new([{"artist" => artist}])
end
def Criteria.album album
- Criteria.new([{ "album" => album }])
+ Criteria.new([{"album" => album}])
end
def &(other)
- Criteria.new(["&"] + @filters + other.filters)
+ Criteria.new(["&"] + [@filters] + [other.filters])
end
def |(other)
- Criteria.new(["|"] + @filters + other.filters)
+ Criteria.new(["|"] + [@filters] + [other.filters])
end
def !
- Criteria.new(['!'] + @filters)
+ Criteria.new(['!'] + [@filters])
end
def desired? song
case @filters.length
when 1 then return match? song
- when 2 then return !Criteria.new([@filters[1]]).desired?(song)
+ when 2 then return !Criteria.new(filters[1]).desired?(song)
else filterWithTwoConditions song
end
end
def filterWithTwoConditions song
- condition1 = Criteria.new([@filters[1]]).desired?(song)
- condition2 = Criteria.new([@filters[2]]).desired?(song)
+ condition1 = Criteria.new(@filters[1]).desired?(song)
+ condition2 = Criteria.new(@filters[2]).desired?(song)
return true if @filters[0] == "&" && condition1 && condition2
@filters[0] == "|" && (condition1 || condition2)
end
def match? song
return true if @filters[0]["name"] == song.name
return true if @filters[0]["artist"] == song.artist
@filters[0]["album"] == song.album
end
end