Решение на Втора задача от Свилен Андонов

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

Към профила на Свилен Андонов

Резултати

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

Код

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def same_name?(name)
@name == name
end
def same_artist?(artist)
@artist == artist
end
def same_album?(album)
@album == album
end
def same_song?(song)
self == song
end
end
class Collection
include Enumerable
attr_accessor :collection
def initialize(songs = [])
@collection = songs
end
def each
@collection.each do |index|
yield index
end
end
def Collection.parse(patch)
songs = []
patch.each_line.each_slice(4) do |song|
songs << Song.new(song[0].chomp, song[1].chomp, song[2].chomp)
end
Collection.new(songs)
end
def names
result = @collection.map { |song| song.name }
result.uniq
end
def artists
result = @collection.map { |song| song.artist }
result.uniq
end
def albums
result = @collection.map { |arr| arr.album }
result.uniq
end
def filter(criteria)
songs = @collection.select{ |song| criteria.result.call song }
Collection.new(songs)
end
def adjoin(criteria)
Collection.new( @collection | criteria.collection )
end
end
class Criteria
attr_accessor :result
def initialize(result)
@result = result
end
def &(criteria)
Criteria.new(lambda { |song| (@result.call(song) &&
criteria.result.call(song)) })
end
def |(criteria)
Criteria.new(lambda { |song| (@result.call(song) ||
criteria.result.call(song)) })
end
def !
Criteria.new(lambda { |song| (!@result.call (song)) })
end
def Criteria.name(name)
Criteria.new(lambda { |song| name == song.name })
end
def Criteria.album(album)
Criteria.new(lambda { |song| album == song.album })
end
def Criteria.artist(artist)
Criteria.new(lambda { |song| artist == song.artist })
end
end

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

...........

Finished in 0.01006 seconds
11 examples, 0 failures

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

Свилен обнови решението на 30.10.2012 17:33 (преди около 12 години)

+lass Song
+
+ attr_accessor :name, :artist, :album
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+
+ def same_name?(name)
+ @name == name
+ end
+
+ def same_artist?(artist)
+ @artist == artist
+ end
+
+ def same_album?(album)
+ @album == album
+ end
+
+ def same_song?(song)
+ self == song
+ end
+
+end
+
+class Collection
+ attr_accessor :all
+ def initialize(songs=0)
+ @all = songs
+ end
+
+ def Collection.parse(patch)
+ songs = []
+ patch.each_line.each_slice(4) do |song|
+ songs<<Song.new(song[0].chomp,song[1].chomp, song[2].chomp)
+ end
+ Collection.new(songs)
+ end
+
+ def names
+ result = @all.map { |song| song.name }
+ result.uniq
+ end
+
+ def artists
+ result = @all.map { |song| song.artist }
+ result.uniq
+ end
+
+ def albums
+ result = @all.map { |arr| arr.album }
+ result.uniq
+ end
+
+ def filter(arg)
+ songs = @all.select{ |song| arg.result.call song }
+ Collection.new(songs)
+ end
+
+ def adjoin(arg)
+ Collection.new( @all | arg.all )
+ end
+
+end
+
+class Criteria
+ attr_accessor :result
+ def initialize(result)
+ # p result
+ @result = result
+ end
+
+ def &(arg)
+ Criteria.new(lambda {|song| (@result.call(song) && arg.result.call(song))})
+ end
+
+ def |(arg)
+ Criteria.new(lambda {|song| (@result.call(song) || arg.result.call(song))})
+ end
+
+ def !
+ Criteria.new(lambda {|song| (!@result.call (song))})
+ end
+
+ def Criteria.name(name)
+ Criteria.new(lambda {|song| name == song.name})
+ end
+
+ def Criteria.album(album)
+ Criteria.new(lambda {|song| album == song.album})
+ end
+
+ def Criteria.artist(artist)
+ return Criteria.new(lambda {|song| artist == song.artist })
+ end
+
+end

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

-lass Song
+class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def same_name?(name)
@name == name
end
def same_artist?(artist)
@artist == artist
end
def same_album?(album)
@album == album
end
def same_song?(song)
self == song
end
end
class Collection
attr_accessor :all
def initialize(songs=0)
@all = songs
end
def Collection.parse(patch)
songs = []
patch.each_line.each_slice(4) do |song|
songs<<Song.new(song[0].chomp,song[1].chomp, song[2].chomp)
end
Collection.new(songs)
end
def names
result = @all.map { |song| song.name }
result.uniq
end
def artists
result = @all.map { |song| song.artist }
result.uniq
end
def albums
result = @all.map { |arr| arr.album }
result.uniq
end
def filter(arg)
songs = @all.select{ |song| arg.result.call song }
Collection.new(songs)
end
def adjoin(arg)
Collection.new( @all | arg.all )
end
end
class Criteria
attr_accessor :result
def initialize(result)
# p result
@result = result
end
def &(arg)
Criteria.new(lambda {|song| (@result.call(song) && arg.result.call(song))})
end
def |(arg)
Criteria.new(lambda {|song| (@result.call(song) || arg.result.call(song))})
end
def !
Criteria.new(lambda {|song| (!@result.call (song))})
end
def Criteria.name(name)
Criteria.new(lambda {|song| name == song.name})
end
def Criteria.album(album)
Criteria.new(lambda {|song| album == song.album})
end
def Criteria.artist(artist)
return Criteria.new(lambda {|song| artist == song.artist })
end
end

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

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def same_name?(name)
@name == name
end
def same_artist?(artist)
@artist == artist
end
def same_album?(album)
@album == album
end
- def same_song?(song)
+ def same_song?(song)
self == song
end
end
class Collection
attr_accessor :all
def initialize(songs=0)
@all = songs
end
def Collection.parse(patch)
songs = []
patch.each_line.each_slice(4) do |song|
songs<<Song.new(song[0].chomp,song[1].chomp, song[2].chomp)
end
Collection.new(songs)
end
def names
result = @all.map { |song| song.name }
result.uniq
end
def artists
result = @all.map { |song| song.artist }
result.uniq
end
def albums
result = @all.map { |arr| arr.album }
result.uniq
end
def filter(arg)
songs = @all.select{ |song| arg.result.call song }
Collection.new(songs)
end
def adjoin(arg)
Collection.new( @all | arg.all )
end
end
class Criteria
attr_accessor :result
def initialize(result)
# p result
@result = result
end
def &(arg)
Criteria.new(lambda {|song| (@result.call(song) && arg.result.call(song))})
end
def |(arg)
Criteria.new(lambda {|song| (@result.call(song) || arg.result.call(song))})
end
def !
Criteria.new(lambda {|song| (!@result.call (song))})
end
def Criteria.name(name)
Criteria.new(lambda {|song| name == song.name})
end
def Criteria.album(album)
Criteria.new(lambda {|song| album == song.album})
end
def Criteria.artist(artist)
return Criteria.new(lambda {|song| artist == song.artist })
end
end

Свилен обнови решението на 30.10.2012 22:10 (преди около 12 години)

class Song
- attr_accessor :name, :artist, :album
+ attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def same_name?(name)
@name == name
end
def same_artist?(artist)
@artist == artist
end
- def same_album?(album)
- @album == album
- end
+ def same_album?(album)
+ @album == album
+ end
- def same_song?(song)
+ def same_song?(song)
self == song
- end
+ end
end
class Collection
+
+ include Enumerable
attr_accessor :all
def initialize(songs=0)
@all = songs
end
+ def each
+ @all.each do |value|
+ yield value
+ end
+ end
+
def Collection.parse(patch)
songs = []
patch.each_line.each_slice(4) do |song|
songs<<Song.new(song[0].chomp,song[1].chomp, song[2].chomp)
end
Collection.new(songs)
end
def names
result = @all.map { |song| song.name }
result.uniq
end
def artists
result = @all.map { |song| song.artist }
result.uniq
end
def albums
result = @all.map { |arr| arr.album }
result.uniq
end
def filter(arg)
songs = @all.select{ |song| arg.result.call song }
Collection.new(songs)
end
def adjoin(arg)
Collection.new( @all | arg.all )
end
end
class Criteria
attr_accessor :result
def initialize(result)
- # p result
@result = result
end
def &(arg)
Criteria.new(lambda {|song| (@result.call(song) && arg.result.call(song))})
end
def |(arg)
Criteria.new(lambda {|song| (@result.call(song) || arg.result.call(song))})
end
def !
Criteria.new(lambda {|song| (!@result.call (song))})
end
def Criteria.name(name)
Criteria.new(lambda {|song| name == song.name})
end
def Criteria.album(album)
Criteria.new(lambda {|song| album == song.album})
end
def Criteria.artist(artist)
return Criteria.new(lambda {|song| artist == song.artist })
end
-end
+end

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

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def same_name?(name)
@name == name
end
def same_artist?(artist)
@artist == artist
end
def same_album?(album)
@album == album
end
def same_song?(song)
self == song
end
end
class Collection
include Enumerable
attr_accessor :all
- def initialize(songs=0)
+ def initialize(songs = [])
@all = songs
end
def each
@all.each do |value|
yield value
end
end
def Collection.parse(patch)
songs = []
patch.each_line.each_slice(4) do |song|
- songs<<Song.new(song[0].chomp,song[1].chomp, song[2].chomp)
+ songs<<Song.new(song[0].chomp, song[1].chomp, song[2].chomp)
end
Collection.new(songs)
end
def names
result = @all.map { |song| song.name }
result.uniq
end
def artists
result = @all.map { |song| song.artist }
result.uniq
end
def albums
result = @all.map { |arr| arr.album }
result.uniq
end
def filter(arg)
songs = @all.select{ |song| arg.result.call song }
Collection.new(songs)
end
def adjoin(arg)
Collection.new( @all | arg.all )
end
end
class Criteria
attr_accessor :result
def initialize(result)
@result = result
end
def &(arg)
Criteria.new(lambda {|song| (@result.call(song) && arg.result.call(song))})
end
def |(arg)
Criteria.new(lambda {|song| (@result.call(song) || arg.result.call(song))})
end
def !
Criteria.new(lambda {|song| (!@result.call (song))})
end
def Criteria.name(name)
Criteria.new(lambda {|song| name == song.name})
end
def Criteria.album(album)
Criteria.new(lambda {|song| album == song.album})
end
def Criteria.artist(artist)
- return Criteria.new(lambda {|song| artist == song.artist })
+ Criteria.new(lambda {|song| artist == song.artist})
end
end

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

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
def same_name?(name)
@name == name
end
def same_artist?(artist)
@artist == artist
end
def same_album?(album)
@album == album
end
def same_song?(song)
self == song
end
end
class Collection
include Enumerable
- attr_accessor :all
+ attr_accessor :collection
def initialize(songs = [])
- @all = songs
+ @collection = songs
end
def each
- @all.each do |value|
- yield value
+ @collection.each do |index|
+ yield index
end
end
def Collection.parse(patch)
songs = []
patch.each_line.each_slice(4) do |song|
- songs<<Song.new(song[0].chomp, song[1].chomp, song[2].chomp)
+ songs << Song.new(song[0].chomp, song[1].chomp, song[2].chomp)
end
Collection.new(songs)
end
def names
- result = @all.map { |song| song.name }
+ result = @collection.map { |song| song.name }
result.uniq
end
def artists
- result = @all.map { |song| song.artist }
+ result = @collection.map { |song| song.artist }
result.uniq
end
def albums
- result = @all.map { |arr| arr.album }
+ result = @collection.map { |arr| arr.album }
result.uniq
end
- def filter(arg)
- songs = @all.select{ |song| arg.result.call song }
+ def filter(criteria)
+ songs = @collection.select{ |song| criteria.result.call song }
Collection.new(songs)
end
- def adjoin(arg)
- Collection.new( @all | arg.all )
+ def adjoin(criteria)
+ Collection.new( @collection | criteria.collection )
end
end
class Criteria
attr_accessor :result
def initialize(result)
@result = result
end
- def &(arg)
- Criteria.new(lambda {|song| (@result.call(song) && arg.result.call(song))})
+ def &(criteria)
+ Criteria.new(lambda { |song| (@result.call(song) &&
+ criteria.result.call(song)) })
end
- def |(arg)
- Criteria.new(lambda {|song| (@result.call(song) || arg.result.call(song))})
+ def |(criteria)
+ Criteria.new(lambda { |song| (@result.call(song) ||
+ criteria.result.call(song)) })
end
def !
- Criteria.new(lambda {|song| (!@result.call (song))})
+ Criteria.new(lambda { |song| (!@result.call (song)) })
end
def Criteria.name(name)
- Criteria.new(lambda {|song| name == song.name})
+ Criteria.new(lambda { |song| name == song.name })
end
def Criteria.album(album)
- Criteria.new(lambda {|song| album == song.album})
+ Criteria.new(lambda { |song| album == song.album })
end
def Criteria.artist(artist)
- Criteria.new(lambda {|song| artist == song.artist})
+ Criteria.new(lambda { |song| artist == song.artist })
end
end