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

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

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

Резултати

  • 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 Criteria
def initialize(proc)
@proc = proc
end
def Criteria.name(song_name)
Criteria.new(lambda { |song| song_name == song.name })
end
def Criteria.artist(artist_name)
Criteria.new(lambda { |song| artist_name == song.artist })
end
def Criteria.album(album_name)
Criteria.new(lambda { |song| album_name == song.album })
end
def [](song)
@proc[song]
end
def &(criteria)
Criteria.new(lambda{ |song| self[song] && criteria[song] })
end
def |(criteria)
Criteria.new(lambda{ |song| self[song] || criteria[song] })
end
def !
Criteria.new(lambda{ |song| !self[song] })
end
end
class Collection
include Enumerable
def each
0.upto(@songs.size-1).each do |index|
yield @songs[index]
end
end
def initialize(songs)
@songs = []
@songs += songs
end
def Collection.parse(text)
songs = []
text.split("\n").each_slice(4) do |slice|
songs << Song.new(slice[0], slice[1], slice[2])
end
Collection.new(songs)
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
def filter(criteria)
Collection.new(@songs.select{ |song| criteria[song] })
end
def adjoin(sub_collection)
sub_collection.each do |song|
@songs << song
end
Collection.new(@songs.uniq)
end
end

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

...........

Finished in 0.00998 seconds
11 examples, 0 failures

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

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

+class Song
+ attr_accessor :name, :artist, :album
+
+ def initialize(array)
+ @name, @artist, @album = array[0], array[1], array[2]
+ end
+end
+
+class Criteria
+ def initialize(proc)
+ @proc = proc
+ end
+
+ def Criteria.name(song_name)
+ Criteria.new(lambda{|song| song_name == song.name})
+ end
+
+ def Criteria.artist(artist_name)
+ Criteria.new(lambda{|song| artist_name == song.artist})
+ end
+
+ def Criteria.album(album_name)
+ Criteria.new(lambda{|song| album_name == song.album})
+ end
+
+ def [](song)
+ @proc[song]
+ end
+
+ def &(criteria)
+ Criteria.new(lambda{|song| self[song] && criteria[song]})
+ end
+
+ def |(criteria)
+ Criteria.new(lambda{|song| self[song] || criteria[song]})
+ end
+
+ def !
+ Criteria.new(lambda{|song| !self[song]})
+ end
+end
+
+class Collection
+ include Enumerable
+
+ def each
+ index = 0
+
+ while index < @songs.size
+ yield @songs[index]
+ index += 1
+ end
+ end
+
+ def initialize (songs)
+ @songs = []
+ songs.each do |song|
+ @songs << song
+ end
+ @songs
+ end
+
+ def Collection.parse (text)
+ songs = []
+ text.split("\n").each_slice(4) do |slice|
+ songs << Song.new(slice)
+ end
+ Collection.new(songs)
+ 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
+
+ def filter (criteria)
+ Collection.new(@songs.select{|song| criteria[song]})
+ end
+
+ def adjoin (sub_collection)
+ sub_collection.each do |song|
+ @songs << song
+ end
+ Collection.new(@songs.uniq)
+ end
+end

Защо итерираш с while на 49 ред? Обяснявахме, че while се ползва, когато броя цикли, които трябва да направиш е неизвестен/неограничен.

Не оставяй интервал между името на метода и скобите (def adjoin(criteria)).

Странно е Song.new да приема масив. По-нормално е да приема три позиционни аргумента. Оправи го.

Отделно, ако искаш да правиш копие на нещо в Ruby (не, че има нужда в твоя код), това става с метода #dup или #clone.

Отделно, има ред места, на които не използваш методи на Enumrable, което както говорихме преди две лекции, е неприемливо.

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

class Song
attr_accessor :name, :artist, :album
- def initialize(array)
- @name, @artist, @album = array[0], array[1], array[2]
+ def initialize(name, artist, album)
+ @name, @artist, @album = name, artist, album
end
end
class Criteria
def initialize(proc)
@proc = proc
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| artist_name == song.artist})
+ Criteria.new(lambda {|song| artist_name == song.artist})
end
def Criteria.album(album_name)
- Criteria.new(lambda{|song| album_name == song.album})
+ Criteria.new(lambda {|song| album_name == song.album})
end
def [](song)
@proc[song]
end
def &(criteria)
Criteria.new(lambda{|song| self[song] && criteria[song]})
end
def |(criteria)
Criteria.new(lambda{|song| self[song] || criteria[song]})
end
def !
Criteria.new(lambda{|song| !self[song]})
end
end
class Collection
include Enumerable
def each
- index = 0
-
- while index < @songs.size
+ 0.upto(@songs.size-1).each do |index|
yield @songs[index]
- index += 1
end
end
- def initialize (songs)
+ def initialize(songs)
@songs = []
songs.each do |song|
@songs << song
end
@songs
end
- def Collection.parse (text)
+ def Collection.parse(text)
songs = []
text.split("\n").each_slice(4) do |slice|
- songs << Song.new(slice)
+ songs << Song.new(slice[0], slice[1], slice[2])
end
Collection.new(songs)
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
- def filter (criteria)
+ def filter(criteria)
Collection.new(@songs.select{|song| criteria[song]})
end
- def adjoin (sub_collection)
+ def adjoin(sub_collection)
sub_collection.each do |song|
@songs << song
end
Collection.new(@songs.uniq)
end
end

Ще можеш ли да ми посочиш поне едно място където е трябвало да ползвам метод на Enumerable, а не съм. Причината да не ги използвам е, че не съм забелязала, че това е възможно и честно казано и сега продължавам да не виждам къде мога да направя промяна.

Можеш да събираш списъци с + или +=, между другото.

Конвенциите ти за интервали са нетипични за Руби (и неконсистентни дори само в рамките на това решение), опитай да ги оправиш (основно около скобите на блокове; трябва да е така: foo { |bar| baz }).

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

class Song
attr_accessor :name, :artist, :album
def initialize(name, artist, album)
@name, @artist, @album = name, artist, album
end
end
class Criteria
def initialize(proc)
@proc = proc
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| artist_name == song.artist})
+ Criteria.new(lambda { |song| artist_name == song.artist })
end
def Criteria.album(album_name)
- Criteria.new(lambda {|song| album_name == song.album})
+ Criteria.new(lambda { |song| album_name == song.album })
end
def [](song)
@proc[song]
end
def &(criteria)
- Criteria.new(lambda{|song| self[song] && criteria[song]})
+ Criteria.new(lambda{ |song| self[song] && criteria[song] })
end
def |(criteria)
- Criteria.new(lambda{|song| self[song] || criteria[song]})
+ Criteria.new(lambda{ |song| self[song] || criteria[song] })
end
def !
- Criteria.new(lambda{|song| !self[song]})
+ Criteria.new(lambda{ |song| !self[song] })
end
end
class Collection
include Enumerable
def each
0.upto(@songs.size-1).each do |index|
yield @songs[index]
end
end
def initialize(songs)
@songs = []
- songs.each do |song|
- @songs << song
- end
- @songs
+ @songs += songs
end
def Collection.parse(text)
songs = []
text.split("\n").each_slice(4) do |slice|
songs << Song.new(slice[0], slice[1], slice[2])
end
Collection.new(songs)
end
def names
- @songs.map{|song| song.name}.uniq
+ @songs.map{ |song| song.name }.uniq
end
def artists
- @songs.map{|song| song.artist}.uniq
+ @songs.map{ |song| song.artist }.uniq
end
def albums
- @songs.map{|song| song.album}.uniq
+ @songs.map{ |song| song.album }.uniq
end
def filter(criteria)
- Collection.new(@songs.select{|song| criteria[song]})
+ Collection.new(@songs.select{ |song| criteria[song] })
end
def adjoin(sub_collection)
sub_collection.each do |song|
@songs << song
end
Collection.new(@songs.uniq)
end
end