Решение на Втора задача от Георги Пачов

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

Към профила на Георги Пачов

Резултати

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

Код

class Song
attr_reader :name, :artist, :album
def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
include Enumerable
attr_reader :song_list
def initialize(array)
@song_list = array
end
def names
@song_list.map {|song| song.name}.uniq
end
def artists
@song_list.map {|song| song.artist}.uniq
end
def albums
@song_list.map {|song| song.album}.uniq
end
def Collection.parse_file(file)
file_contents = File.read(file)
parse(file_contents)
end
def Collection.parse(text)
#TODO: Map 3 lines -> a song instance?
# Split-> map
result = []
text.each_line("\n\n") { |song_info| result << parse_song(song_info) }
Collection.new result
end
def filter(criteria)
filtered = @song_list.select {|song| criteria.accept? song }
Collection.new filtered
end
def filter(criteria)
filtered = @song_list.select {|song| criteria.accept? song }
Collection.new filtered
end
def each
@song_list.each {|song| yield song}
end
def adjoin(collection)
Collection.new (@song_list | collection.song_list)
end
private
def Collection.parse_song(song_record_text)
song_info_array = song_record_text.split("\n")
Song.new song_info_array[0],song_info_array[1],song_info_array[2]
end
end
class Criteria
def initialize(lambda)
@criteria = lambda
end
def accept? song
@criteria.call song
end
def Criteria.name(song_name)
Criteria.new lambda {|song| song.name == song_name}
end
def Criteria.artist(artist_name)
Criteria.new lambda {|song| song.artist == artist_name}
end
def Criteria.album(album_name)
Criteria.new lambda {|song| song.album == album_name}
end
def |(other_criteria)
Criteria.new lambda {|song| accept? song or other_criteria.accept? song}
end
def &(other_criteria)
Criteria.new lambda {|song| accept? song and other_criteria.accept? song}
end
def !@
Criteria.new lambda {|song| !accept? song}
end
end

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

...........

Finished in 0.01028 seconds
11 examples, 0 failures

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

Георги обнови решението на 29.10.2012 23:32 (преди над 11 години)

+class Song
+ attr_accessor :name, :artist, :album
+ def initialize (name, artist, album)
+ @name = name
+ @artist = artist
+ @album = album
+ end
+end
+
+class Collection
+ include Enumerable
+
+ attr_accessor :song_list
+ def initialize(array)
+ @song_list = array
+ end
+
+ def names
+ @song_list.map {|song| song.name}.uniq
+ end
+
+ def artists
+ @song_list.map {|song| song.artist}.uniq
+ end
+
+ def albums
+ @song_list.map {|song| song.album}.uniq
+ end
+
+ def Collection.parse_file(file)
+ file_contents = File.read(file)
+ parse(file_contents)
+ end
+
+ def Collection.parse(text)
+ #TODO: Map 3 lines -> a song instance?
+ # Split-> map
+ result = []
+ text.each_line("\n\n") { |song_info| result << parse_song(song_info) }
+ Collection.new(result)
+ end
+
+ def filter (criteria)
+ filtered = @song_list.select {|song| criteria.accept? song }
+ Collection.new(filtered)
+ end
+
+ def filter (criteria)
+ filtered = @song_list.select {|song| criteria.accept? song }
+ Collection.new(filtered)
+ end
+
+ def each
+ @song_list.each {|song| yield song}
+ end
+
+ def adjoin(collection)
+ Collection.new(@song_list | collection.song_list)
+ end
+ private
+ def Collection.parse_song(song_record_text)
+ song_info_array = song_record_text.split("\n")
+ Song.new(song_info_array[0],song_info_array[1],song_info_array[2])
+ end
+
+end
+
+class Criteria
+ def initialize(lambda)
+ @criteria = lambda
+ end
+
+ def accept? song
+ @criteria.call song
+ end
+
+ def Criteria.name(song_name)
+ Criteria.new(lambda {|song| song.name == song_name})
+ end
+ def Criteria.artist(artist_name)
+ Criteria.new(lambda {|song| song.artist == artist_name})
+ end
+ def Criteria.album(album_name)
+ Criteria.new(lambda {|song| song.album == album_name})
+ end
+
+ def | (other_criteria)
+ Criteria.new(lambda {|song| accept? song or (other_criteria.accept? song)})
+ end
+ def & (other_criteria)
+ Criteria.new(lambda {|song| accept? song and (other_criteria.accept? song)})
+ end
+
+ def !@
+ Criteria.new (lambda {|song| !(accept? song)})
+ end
+
+end
+

Георги обнови решението на 29.10.2012 23:44 (преди над 11 години)

class Song
+
attr_accessor :name, :artist, :album
+
def initialize (name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
include Enumerable
attr_accessor :song_list
+
def initialize(array)
@song_list = array
end
def names
@song_list.map {|song| song.name}.uniq
end
def artists
@song_list.map {|song| song.artist}.uniq
end
def albums
@song_list.map {|song| song.album}.uniq
end
def Collection.parse_file(file)
file_contents = File.read(file)
parse(file_contents)
end
def Collection.parse(text)
#TODO: Map 3 lines -> a song instance?
# Split-> map
result = []
text.each_line("\n\n") { |song_info| result << parse_song(song_info) }
Collection.new(result)
end
def filter (criteria)
filtered = @song_list.select {|song| criteria.accept? song }
Collection.new(filtered)
end
def filter (criteria)
filtered = @song_list.select {|song| criteria.accept? song }
Collection.new(filtered)
end
def each
@song_list.each {|song| yield song}
end
def adjoin(collection)
Collection.new(@song_list | collection.song_list)
end
+
private
def Collection.parse_song(song_record_text)
song_info_array = song_record_text.split("\n")
Song.new(song_info_array[0],song_info_array[1],song_info_array[2])
end
end
class Criteria
def initialize(lambda)
@criteria = lambda
end
def accept? song
@criteria.call song
end
def Criteria.name(song_name)
Criteria.new(lambda {|song| song.name == song_name})
end
+
def Criteria.artist(artist_name)
Criteria.new(lambda {|song| song.artist == artist_name})
end
+
def Criteria.album(album_name)
Criteria.new(lambda {|song| song.album == album_name})
end
def | (other_criteria)
Criteria.new(lambda {|song| accept? song or (other_criteria.accept? song)})
end
+
def & (other_criteria)
Criteria.new(lambda {|song| accept? song and (other_criteria.accept? song)})
end
def !@
Criteria.new (lambda {|song| !(accept? song)})
end
end

Георги обнови решението на 30.10.2012 00:15 (преди над 11 години)

class Song
attr_accessor :name, :artist, :album
def initialize (name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
include Enumerable
attr_accessor :song_list
def initialize(array)
@song_list = array
end
def names
@song_list.map {|song| song.name}.uniq
end
def artists
@song_list.map {|song| song.artist}.uniq
end
def albums
@song_list.map {|song| song.album}.uniq
end
def Collection.parse_file(file)
file_contents = File.read(file)
parse(file_contents)
end
def Collection.parse(text)
#TODO: Map 3 lines -> a song instance?
# Split-> map
result = []
text.each_line("\n\n") { |song_info| result << parse_song(song_info) }
Collection.new(result)
end
def filter (criteria)
filtered = @song_list.select {|song| criteria.accept? song }
Collection.new(filtered)
end
def filter (criteria)
filtered = @song_list.select {|song| criteria.accept? song }
Collection.new(filtered)
end
def each
@song_list.each {|song| yield song}
end
def adjoin(collection)
Collection.new(@song_list | collection.song_list)
end
private
def Collection.parse_song(song_record_text)
song_info_array = song_record_text.split("\n")
Song.new(song_info_array[0],song_info_array[1],song_info_array[2])
end
end
class Criteria
def initialize(lambda)
@criteria = lambda
end
def accept? song
@criteria.call song
end
def Criteria.name(song_name)
Criteria.new(lambda {|song| song.name == song_name})
end
def Criteria.artist(artist_name)
Criteria.new(lambda {|song| song.artist == artist_name})
end
def Criteria.album(album_name)
Criteria.new(lambda {|song| song.album == album_name})
end
def | (other_criteria)
- Criteria.new(lambda {|song| accept? song or (other_criteria.accept? song)})
+ Criteria.new(lambda {|song| accept? song or other_criteria.accept? song})
end
def & (other_criteria)
- Criteria.new(lambda {|song| accept? song and (other_criteria.accept? song)})
+ Criteria.new(lambda {|song| accept? song and other_criteria.accept? song})
end
def !@
- Criteria.new (lambda {|song| !(accept? song)})
+ Criteria.new (lambda {|song| !accept? song})
end
end

Георги обнови решението на 30.10.2012 00:22 (преди над 11 години)

class Song
attr_accessor :name, :artist, :album
def initialize (name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
include Enumerable
attr_accessor :song_list
def initialize(array)
@song_list = array
end
def names
@song_list.map {|song| song.name}.uniq
end
def artists
@song_list.map {|song| song.artist}.uniq
end
def albums
@song_list.map {|song| song.album}.uniq
end
def Collection.parse_file(file)
file_contents = File.read(file)
parse(file_contents)
end
def Collection.parse(text)
#TODO: Map 3 lines -> a song instance?
# Split-> map
result = []
text.each_line("\n\n") { |song_info| result << parse_song(song_info) }
Collection.new(result)
end
def filter (criteria)
filtered = @song_list.select {|song| criteria.accept? song }
Collection.new(filtered)
end
def filter (criteria)
filtered = @song_list.select {|song| criteria.accept? song }
Collection.new(filtered)
end
def each
@song_list.each {|song| yield song}
end
def adjoin(collection)
Collection.new(@song_list | collection.song_list)
end
private
def Collection.parse_song(song_record_text)
song_info_array = song_record_text.split("\n")
Song.new(song_info_array[0],song_info_array[1],song_info_array[2])
end
end
class Criteria
def initialize(lambda)
@criteria = lambda
end
def accept? song
@criteria.call song
end
def Criteria.name(song_name)
- Criteria.new(lambda {|song| song.name == song_name})
+ Criteria.new lambda {|song| song.name == song_name}
end
def Criteria.artist(artist_name)
- Criteria.new(lambda {|song| song.artist == artist_name})
+ Criteria.new lambda {|song| song.artist == artist_name}
end
def Criteria.album(album_name)
- Criteria.new(lambda {|song| song.album == album_name})
+ Criteria.new lambda {|song| song.album == album_name}
end
def | (other_criteria)
- Criteria.new(lambda {|song| accept? song or other_criteria.accept? song})
+ Criteria.new lambda {|song| accept? song or other_criteria.accept? song}
end
def & (other_criteria)
- Criteria.new(lambda {|song| accept? song and other_criteria.accept? song})
+ Criteria.new lambda {|song| accept? song and other_criteria.accept? song}
end
def !@
- Criteria.new (lambda {|song| !accept? song})
+ Criteria.new lambda {|song| !accept? song}
end
end

Георги обнови решението на 30.10.2012 20:16 (преди над 11 години)

class Song
- attr_accessor :name, :artist, :album
-
- def initialize (name, artist, album)
+ attr_reader :name, :artist, :album
+ def initialize(name, artist, album)
@name = name
@artist = artist
@album = album
end
end
class Collection
include Enumerable
- attr_accessor :song_list
+ attr_reader :song_list
def initialize(array)
@song_list = array
end
def names
@song_list.map {|song| song.name}.uniq
end
def artists
@song_list.map {|song| song.artist}.uniq
end
def albums
@song_list.map {|song| song.album}.uniq
end
def Collection.parse_file(file)
file_contents = File.read(file)
parse(file_contents)
end
def Collection.parse(text)
#TODO: Map 3 lines -> a song instance?
# Split-> map
result = []
text.each_line("\n\n") { |song_info| result << parse_song(song_info) }
- Collection.new(result)
+ Collection.new result
end
- def filter (criteria)
+ def filter(criteria)
filtered = @song_list.select {|song| criteria.accept? song }
- Collection.new(filtered)
+ Collection.new filtered
end
- def filter (criteria)
+ def filter(criteria)
filtered = @song_list.select {|song| criteria.accept? song }
- Collection.new(filtered)
+ Collection.new filtered
end
def each
@song_list.each {|song| yield song}
end
def adjoin(collection)
- Collection.new(@song_list | collection.song_list)
+ Collection.new (@song_list | collection.song_list)
end
private
def Collection.parse_song(song_record_text)
song_info_array = song_record_text.split("\n")
- Song.new(song_info_array[0],song_info_array[1],song_info_array[2])
+ Song.new song_info_array[0],song_info_array[1],song_info_array[2]
end
end
class Criteria
def initialize(lambda)
@criteria = lambda
end
def accept? song
@criteria.call song
end
def Criteria.name(song_name)
Criteria.new lambda {|song| song.name == song_name}
end
def Criteria.artist(artist_name)
Criteria.new lambda {|song| song.artist == artist_name}
end
def Criteria.album(album_name)
Criteria.new lambda {|song| song.album == album_name}
end
- def | (other_criteria)
+ def |(other_criteria)
Criteria.new lambda {|song| accept? song or other_criteria.accept? song}
end
- def & (other_criteria)
+ def &(other_criteria)
Criteria.new lambda {|song| accept? song and other_criteria.accept? song}
end
def !@
Criteria.new lambda {|song| !accept? song}
end
end