Решение на Втора задача от Виктор Карагяуров

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

Към профила на Виктор Карагяуров

Резултати

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

Код

class Collection
def initialize songs_as_string, artist_tags
songs_attributes = get_songs_attributes(songs_as_string, artist_tags)
@songs = songs_attributes.map { |song_attributes| Song.new song_attributes }
end
def find criteria
@songs.select { |song| song.meets criteria }
end
protected
def get_songs_attributes songs_as_string, extra_tags
songs = songs_as_string.split("\n").map { |song| song.split(".") }
songs.each { |song| song.insert 3, song[2].split(",")[1] }
songs.each { |song| song[2] = song[2].split(",")[0] }
songs.each { |song| song.each { |entry| entry.strip! if entry } }
songs.each { |song| song << "" if song.length < 5 }
songs.each { |song| song[4] = song[4].split(",").map &:strip }
songs.each { |song| song[4] << song[2].downcase }
songs.each { |song| if song[3] then song[4] << song[3].downcase end }
songs.each { |song| song[4] << extra_tags[song[1]] if extra_tags[song[1]] }
songs.each { |song| song[4].flatten! }
end
end
class Song
def initialize song_attributes
@name = song_attributes[0]
@artist = song_attributes[1]
@genre = song_attributes[2]
@subgenre = song_attributes[3]
@tags = song_attributes[4]
end
def name
@name
end
def artist
@artist
end
def genre
@genre
end
def subgenre
@subgenre
end
def tags
@tags
end
def meets criteria
tags = criteria[:tags]
tags = [tags] if tags and !tags.kind_of?(Array)
include_tags = tags ? tags.select { |tag| !tag.end_with? '!' } : []
exlude_tags = tags ? tags.select { |tag| tag.end_with? '!' } : []
exlude_tags.map! { |tag| tag.chop }
return false if criteria[:name] and criteria[:name] != @name
return false if criteria[:artist] and criteria[:artist] != @artist
return false if !include_tags.all? { |tag| @tags.include? tag }
return false if exlude_tags.any? { |tag| @tags.include? tag }
return false if criteria[:filter] and !criteria[:filter].(self)
true
end
end

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

............

Finished in 0.56044 seconds
12 examples, 0 failures

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

Виктор обнови решението на 31.10.2011 05:35 (преди над 12 години)

+class Collection
+ def initialize songs_as_string, artist_tags
+ songs_attributes = get_songs_attributes(songs_as_string, artist_tags)
+ @songs = songs_attributes.map { |song_attributes| Song.new song_attributes }
+ end
+
+ def find criteria
+ @songs.select { |song| song.meets criteria }
+ end
+
+ protected
+ def get_songs_attributes songs_as_string, extra_tags
+ songs = songs_as_string.split("\n").map { |song| song.split(".") }
+ songs.each { |song| song.insert 3, song[2].split(",")[1] }
+ songs.each { |song| song[2] = song[2].split(",")[0] }
+ songs.each { |song| song.each { |entry| entry.strip! if entry } }
+ songs.each { |song| song << "" if song.length < 5 }
+ songs.each { |song| song[4] = song[4].split(",").map &:strip }
+ songs.each { |song| song[4] << song[2].downcase }
+ songs.each { |song| if song[3] then song[4] << song[3].downcase end }
+ songs.each { |song| song[4] << extra_tags[song[1]] if extra_tags[song[1]] }
+ songs.each { |song| song[4].flatten! }
+ end
+end
+
+class Song
+ def initialize song_attributes
+ @name = song_attributes[0]
+ @artist = song_attributes[1]
+ @genre = song_attributes[2]
+ @subgenre = song_attributes[3]
+ @tags = song_attributes[4]
+ end
+
+ def name
+ @name
+ end
+
+ def artist
+ @artist
+ end
+
+ def genre
+ @genre
+ end
+
+ def subgenre
+ @subgenre
+ end
+
+ def tags
+ @tags
+ end
+
+ def meets criteria
+ tags = criteria[:tags]
+ tags = [tags] if tags and !tags.kind_of?(Array)
+ include_tags = tags ? tags.select { |tag| !tag.end_with? '!' } : []
+ exlude_tags = tags ? tags.select { |tag| tag.end_with? '!' } : []
+ exlude_tags.map! { |tag| tag.chop }
+ return false if criteria[:name] and criteria[:name] != @name
+ return false if criteria[:artist] and criteria[:artist] != @artist
+ return false if !include_tags.all? { |tag| @tags.include? tag }
+ return false if exlude_tags.any? { |tag| @tags.include? tag }
+ return false if criteria[:filter] and !criteria[:filter].(self)
+ true
+ end
+end