Чанита обнови решението на 27.10.2012 22:18 (преди около 12 години)
+module Perambulate
+ def is_element(element,array)
+ element if(!array.include?(element))
+ end
+ def simple_check(value_1,value_2)
+ (value_1 == []) ? (value_1 + value_2) : value_1
+ end
+ def reverse_criteria(result,criteria)
+ i=5
+ 0.upto(5).each do |x| result[x][1] = criteria.to_a[i][1]
+ i -=1
+ end
+ result
+ end
+ def get_needed(needed,collection)
+ result=[]
+ collection.map{ |x| x[needed] }.map{ |y| result << (is_element(y,result))}
+ result
+ end
+end
+module Filter_Help
+ def check_include(name,indexes,collection,i)
+ indexes.select{ |y| name == collection[y][i] }
+ end
+ def check_remove(name,indexes,collection,i)
+ indexes.select{ |y| name != collection[y][i] }
+ end
+ def add_songs(indexes,collection,names,i)
+ return indexes if(names == [])
+ result=[]
+ names.map{ |x| result = result + check_include(x,indexes,collection,i) }
+ result
+ end
+ def remove(indexes,collection,names,i)
+ return indexes if(names == [])
+ result=[]
+ names.map{ |x| result = result + check_remove(x,indexes,collection,i) }
+ result
+ end
+ def initialize_indexes(collection)
+ indexes=[]
+ 0.upto(collection.length-1).each do |x| indexes << x end
+ indexes
+ end
+ def switch_between(i,value,indexes,collection)
+ indexes = initialize_indexes(collection) if(indexes == [])
+ if(i<3)
+ add_songs(indexes,collection,value,i)
+ else
+ remove(indexes,collection,value,(i-5).abs)
+ end
+ end
+ def indexes(collection,criteria)
+ indexes, i = [] , 0
+ criteria.criteria.map{|key,value|
+ indexes , i = switch_between(i,value,indexes,collection) , (i +=1) }
+ indexes
+ end
+end
+class Song
+ attr_accessor :song
+ def initialize(array)
+ @song = []
+ array.map{|x| song << x}
+ end
+ def album
+ @song[2]
+ end
+ def name
+ @song[0]
+ end
+ def artist
+ @song[1]
+ end
+ def [](index)
+ @song[index]
+ end
+end
+class Collection
+ include Enumerable
+ include Perambulate
+ include Filter_Help
+ attr_accessor :collection
+ def initialize(text)
+ @collection = text.split("\n\n").map{ |x| Song.new(x.split("\n"))}
+ end
+ def Collection.parse(text)
+ Collection.new(text)
+ end
+ def names
+ get_needed(0,@collection).select{|y| (y)}
+ end
+ def artists
+ get_needed(1,@collection).select{|y| (y)}
+ end
+ def albums
+ get_needed(2,@collection).select{|y| (y)}
+ end
+ def each
+ current = 0
+ while current<@collection.length
+ yield @collection[current]
+ current +=1
+ end
+ end
+ def adjoin(other_collection)
+ result = Collection.new("")
+ result.collection = @collection + (other_collection.collection)
+ result
+ end
+ def filter(criteria)
+ result , indexes = Collection.new("") , indexes(@collection,criteria)
+ result.collection = @collection.select{ |x|
+ (indexes.include?(@collection.index(x)))}
+ result
+ end
+end
+class Criteria
+ include Perambulate
+ attr_accessor :criteria
+ def initialize()
+ @criteria = {name: [], artist: [], album: [],
+ not_album: [], not_artist: [], not_name: []}
+ end
+ def Criteria.name(name)
+ result = Criteria.new
+ result.criteria[:name] << name
+ result
+ end
+ def Criteria.album(album)
+ result = Criteria.new
+ result.criteria[:album] << album
+ result
+ end
+ def Criteria.artist(artist)
+ result = Criteria.new
+ result.criteria[:artist] << artist
+ result
+ end
+ def |(other)
+ result = Criteria.new()
+ @criteria.map{|key,value|
+ result.criteria[key] = @criteria[key] + (other.criteria[key])}
+ result
+ end
+ def &(other)
+ result = Criteria.new()
+ @criteria.map{|key,value|
+ result.criteria[key] = simple_check(value,other.criteria[key])}
+ result
+ end
+ def !@
+ result = Criteria.new
+ result.criteria = Hash[reverse_criteria(result.criteria.to_a,@criteria)]
+ result
+ end
+end