Ростислав обнови решението на 31.10.2011 15:12 (преди около 13 години)
+#!/usr/bin/env ruby
+
+class Song
+
+ def initialize(line)
+ arr = []
+ line.each_line('.') { |s| arr<<s }
+ @s_name = arr[0].delete(".").rstrip.lstrip
+ @s_artist = arr[1].delete(".").lstrip
+ @s_genre = arr[2].partition(",").first.delete(".").lstrip
+ @s_subgenre = arr[2]=="" ? nil:arr[2].partition(",").last.delete(".").lstrip
+ @s_tags=Array.new([@s_genre.to_s.downcase,@s_subgenre.to_s.downcase])
+ if !(arr[2].include?("\n"))
+ @s_tags<<arr[3].lstrip.tr!("\n", "")
+ @s_tags.flatten!
+ end
+ @s_tags.delete_if {|x| x=="" || x==nil}
+ end
+
+ def full_name
+ str = name+artist+genre+subgenre+tags.to_s
+ return str
+ end
+
+ def name
+ return @s_name
+ end
+
+ def artist
+ return @s_artist
+ end
+
+ def genre
+ return @s_genre
+ end
+
+ def subgenre
+ return @s_subgenre
+ end
+
+ def tags
+ return @s_tags
+ end
+
+ def add_tags(more_tags)
+ @s_tags<<more_tags
+ @s_tags.flatten!
+ @s_tags.each do |tag|
+ tag.delete!("\n")
+ end
+ end
+
+ def ret(what)
+ case what
+ when "name"
+ self.name
+ when "artist"
+ self.artist
+ when "genre"
+ self.genre
+ when "subgenre"
+ self.subgenre
+ when "tags"
+ self.tags
+ end
+ end
+end
+
+class Collection
+
+ def initialize(songs_as_string, artist_tags)
+ arr=[]
+ songs_as_string.each_line {|s| arr<<Song.new(s)}
+ artist_tags.each do |key, value|
+ arr.each do |song|
+ if key==song.artist
+ song.add_tags(value)
+ end
+ end
+ end
+ @songs = arr
+ end
+
+ def check(song, hsh)
+ arr=[]
+ hsh.each do |key, value|
+ arr<<value
+ end
+ arr.flatten!
+ arr.each do |str|
+ if str.include?("!") && song.include?(str.delete("!"))
+ return false
+ elsif !(song.include?(str)) && !(str.include?("!"))
+ return false
+ end
+ end
+ return true
+ end
+
+ def find(criteria)
+ arr=[]
+ @songs.each do |song|
+ if check(song.full_name, criteria)==true
+ arr<<song
+ end
+ end
+ return arr
+ end
+
+end