Решение на Втора задача от Йоана Тодорова

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

Към профила на Йоана Тодорова

Резултати

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

Код

#!usr/bin/ruby -w
class Song
attr_accessor :s_name, :s_artist, :s_album
def initialize(name, artist, album)
@s_name, @s_artist, @s_album = name, artist, album
end
def name
@s_name
end
def artist
@s_artist
end
def album
@s_album
end
def keep_song?(array_of_criteria)
result = false
array_of_criteria.each do |hash_criteria|
result = (matches_criteria?(hash_criteria, true) or result)
end
result
end
def matches_criteria?(hash_criteria, result)
hash_criteria.each do |key, value|
a = method(key[0]).call == key[1]
b = value == true
result = (( b and a) or (not b and not a)) and result
end
end
end
class Collection
include Enumerable
def each
@collection.each { |obj| yield obj }
end
def initialize(songs)
@collection = Array.new.concat(songs)
@collection
end
def self.parse(text)
text = (text).split("\n\n").map { |obj| (obj).split("\n") }
@collection = text.map { |obj| Song.new(obj[0],obj[1],obj[2]) }
Collection.new(@collection)
end
def self.names
names_array = @collection.map { |song| song.name }
Collection.new(names_array.compact.uniq)
end
def self.artists
Collection.new(artists_array.compact.uniq)
print artists_array
end
def self.albums
albums_array = @collection.map { |song| song.album }
Collection.new(albums_array.compact.uniq)
end
def filter(values)
(array_of_songs = @collection).keep_if { |song| song.keep_song?(values) }
Collection.new(array_of_songs)
print array_of_songs
end
def adjoin(collection2)
Collection.new((@collection.concat(collection2)).uniq)
end
end
module MyModule
def help_and(crit)
@criteria.each { |hash_crit_other| crit = crit.merge(hash_crit_other) }
crit
end
def help_not(hash)
newhash = {}
hash.map { |key, value| newhash[key] = (not value) }
newhash
end
end
class Criteria
include Enumerable
include MyModule
attr_accessor :truth, :type, :value
def each
@criteria.each { |obj| yield obj }
end
def initialize(values)
@criteria = []
values.each { |value| @criteria = @criteria << value }
end
def self.name(text)
Criteria.new([{[:name, text] => true}])
end
def self.artist(text)
@truth ||="true"
Criteria.new([{[:artist, text] => true }])
end
def self.album(text)
@truth ||="true"
Criteria.new([{[:album, text] =>true}])
end
def &(other)
arr = []
@criteria.each { |hash_criteria| arr << other.help_and(hash_criteria) }
Criteria.new(arr)
end
def |(other)
other.each {|ob| puts ob.instance_of? Hash }
other.each { |hash_criteria| @criteria = @criteria << hash_criteria}
Criteria.new(@criteria)
end
def !@
arr = []
@criteria.each { |ob| arr = arr << help_not(ob) }
Criteria.new(arr)
end
end

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

▸ Покажи лога

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

Йоана обнови решението на 31.10.2012 15:34 (преди над 12 години)

▸ Покажи разликите
+#!usr/bin/ruby -w
+
+class Song
+ attr_accessor :s_name, :s_artist, :s_album
+
+ def initialize(name, artist, album)
+ @s_name, @s_artist, @s_album = name, artist, album
+ end
+
+ def name
+ @s_name
+ end
+
+ def artist
+ @s_artist
+ end
+
+ def album
+ @s_album
+ end
+
+ def keep_song?(array_of_criteria)
+ result = false
+ array_of_criteria.each do |hash_criteria|
+ result = (matches_criteria?(hash_criteria, true) or result)
+ end
+ result
+ end
+
+ def matches_criteria?(hash_criteria, result)
+ hash_criteria.each do |key, value|
+ a = method(key[0]).call == key[1]
+ b = value == true
+ result = (( b and a) or (not b and not a)) and result
+ end
+ end
+end
+
+class Collection
+ include Enumerable
+
+ def each
+ @collection.each { |obj| yield obj }
+ end
+
+ def initialize(songs)
+ @collection = Array.new.concat(songs)
+ @collection
+ end
+
+ def self.parse(text)
+ text = (text).split("\n\n").map { |obj| (obj).split("\n") }
+ @collection = text.map { |obj| Song.new(obj[0],obj[1],obj[2]) }
+ Collection.new(@collection)
+ end
+
+ def self.names
+ names_array = @collection.map { |song| song.name }
+ Collection.new(names_array.compact.uniq)
+ end
+
+ def self.artists
+ Collection.new(artists_array.compact.uniq)
+ print artists_array
+ end
+
+ def self.albums
+ albums_array = @collection.map { |song| song.album }
+ Collection.new(albums_array.compact.uniq)
+ end
+
+ def filter(values)
+ (array_of_songs = @collection).keep_if { |song| song.keep_song?(values) }
+ Collection.new(array_of_songs)
+ print array_of_songs
+ end
+
+ def adjoin(collection2)
+ Collection.new((@collection.concat(collection2)).uniq)
+ end
+end
+
+module MyModule
+ def help_and(crit)
+ @criteria.each { |hash_crit_other| crit = crit.merge(hash_crit_other) }
+ crit
+ end
+
+ def help_not(hash)
+ newhash = {}
+ hash.map { |key, value| newhash[key] = (not value) }
+ newhash
+ end
+end
+
+class Criteria
+ include Enumerable
+ include MyModule
+
+ attr_accessor :truth, :type, :value
+
+ def each
+ @criteria.each { |obj| yield obj }
+ end
+
+ def initialize(values)
+ @criteria = []
+ values.each { |value| @criteria = @criteria << value }
+ end
+
+ def self.name(text)
+ Criteria.new([{[:name, text] => true}])
+ end
+
+ def self.artist(text)
+ @truth ||="true"
+ Criteria.new([{[:artist, text] => true }])
+ end
+
+ def self.album(text)
+ @truth ||="true"
+ Criteria.new([{[:album, text] =>true}])
+ end
+
+ def &(other)
+ arr = []
+ @criteria.each { |hash_criteria| arr << other.help_and(hash_criteria) }
+ Criteria.new(arr)
+ end
+
+ def |(other)
+ other.each {|ob| puts ob.instance_of? Hash }
+ other.each { |hash_criteria| @criteria = @criteria << hash_criteria}
+ Criteria.new(@criteria)
+ end
+
+ def !@
+ arr = []
+ @criteria.each { |ob| arr = arr << help_not(ob) }
+ Criteria.new(arr)
+ end
+end

Допълнително:

  • s_name, s_* не се прави в Ruby като се именуват променливи (т.е. типът им не се вкарва по този начин в името)
  • Променливи с имена arr, ob, MyModule, help_and, collection2, obj, a, b и т.н. са лошо избрани номера; това вече сме го казвали няколко пъти и ще взимаме точки за него; трябва да се вижда старание за добро именоване; изобщо, почти всичките ти имена са или твърде генерични, или зле избрани; например text в Collection.parse всъщност не е текст а списък от списъци с полета на песни; songs или нещо такова щеше да е по-подходящо
  • Ред 53, ако list = [:a, :b, :c] то вместо това foo(list[0], list[1], list[2]) може да направиш така: foo(*list) (има го в слайдовете)
  • Имаш редица проблеми с дизайна; след час-два като приключи крайния срок, ще може да видиш решения на колеги, които са успели да измислят хитри начини за справяне с проблема; силно препоръчвам да ги разгледаш и/или да се обадиш в някоя почивка, понеже коментарът ми е тесен да опиша всичките проблеми и, честно казано, ме мързи да пиша толкова :)