Решение на Втора задача от Лъчезар Цанов

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

Към профила на Лъчезар Цанов

Резултати

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

Код

#!/usr/bin/env ruby
class Song
attr_reader :name
attr_reader :artist
attr_reader :genre
attr_reader :subgenre
attr_accessor :tags
def initialize(inputString)
inputString.chomp!
@str=inputString.split(".")
formatStr(@str)
@name=@str[0]
@artist=@str[1]
constructGenreAndSubgenre
constructTags
end
def formatStr(target)
target.each {|value| value.rstrip!}
target.each {|value| value.lstrip!}
end
def constructTags
@tags=[]
@tags=@str[3].split(",") if @str[3]
formatStr(@tags)
@tags<<@genre.downcase
@tags<<@subgenre.downcase if @subgenre!=nil
end
def constructGenreAndSubgenre
temporary=[]
temporary=@str[2].split(",")
formatStr(temporary)
@genre=temporary[0]
@subgenre=temporary[1]
end
end
class Collection
def initialize(songs_as_string,dictionaryOut)
temporary=songs_as_string.each_line.map {|s| s}
@collection=temporary.each.map {|s| Song.new(s)}
mergeDictionaryInCollect(dictionaryOut) if dictionaryOut!=nil
end
def mergeDictionaryInCollect(dictionary)
dictionary.each_key do |key|
@collection.each { |el| el.tags<<dictionary[key] if el.artist==key}
@collection.each { |el| el.tags.flatten}
end
end
def searchToInclude(key,result,value)
if(key.to_s == "tags")
result=result.each.select { |el| (el.send key).include?value }
else
result=result.each.select { |el| (el.send key) == value }
end
end
def searchToExclude(key,result,value)
value.chop!
result=result.each.reject { |el| (el.send key).include?value }
end
def createResultList(key,result,criteria)
tempArray=[]
tempArray<<criteria[key]
tempArray.flatten!
tempArray.each do |value|
if value.end_with?('!')
result=searchToExclude(key,result,value)
else
result=searchToInclude(key,result,value)
end
end
result
end
def findWithFilter(lambdaFunc)
result=@collection.each.select {|el| lambdaFunc.call el}
return result
end
def find(criteria)
return @collection if criteria == nil
if criteria[:filter]!=nil
return findWithFilter(criteria[:filter])
end
result=@collection
criteria.each_key do |key|
result=createResultList(key,result,criteria)
end
result
end
end

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

▸ Покажи лога

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

Лъчезар обнови решението на 31.10.2011 16:16 (преди над 13 години)

▸ Покажи разликите
+#!/usr/bin/env ruby
+class Song
+ attr_reader :name
+ attr_reader :artist
+ attr_reader :genre
+ attr_reader :subgenre
+ attr_accessor :tags
+ def initialize(inputString)
+ inputString.chomp!
+ @str=inputString.split(".")
+ formatStr(@str)
+ @name=@str[0]
+ @artist=@str[1]
+ constructGenreAndSubgenre
+ constructTags
+ end
+
+ def formatStr(target)
+ target.each {|value| value.rstrip!}
+ target.each {|value| value.lstrip!}
+ end
+
+ def constructTags
+ @tags=[]
+ @tags=@str[3].split(",") if @str[3]
+ formatStr(@tags)
+ @tags<<@genre.downcase
+ @tags<<@subgenre.downcase if @subgenre!=nil
+ end
+
+ def constructGenreAndSubgenre
+ temporary=[]
+ temporary=@str[2].split(",")
+ formatStr(temporary)
+ @genre=temporary[0]
+ @subgenre=temporary[1]
+ end
+
+end
+
+ class Collection
+ def initialize(songs_as_string,dictionaryOut)
+ temporary=songs_as_string.each_line.map {|s| s}
+ @collection=temporary.each.map {|s| Song.new(s)}
+ mergeDictionaryInCollect(dictionaryOut) if dictionaryOut!=nil
+ end
+
+ def mergeDictionaryInCollect(dictionary)
+ dictionary.each_key do |key|
+ @collection.each { |el| el.tags<<dictionary[key] if el.artist==key}
+ @collection.each { |el| el.tags.flatten}
+ end
+ end
+
+ def searchToInclude(key,result,value)
+ if(key.to_s == "tags")
+ result=result.each.select { |el| (el.send key).include?value }
+ else
+ result=result.each.select { |el| (el.send key) == value }
+ end
+ end
+
+ def searchToExclude(key,result,value)
+ value.chop!
+ result=result.each.reject { |el| (el.send key).include?value }
+ end
+
+ def createResultList(key,result,criteria)
+ tempArray=[]
+ tempArray<<criteria[key]
+ tempArray.flatten!
+ tempArray.each do |value|
+ if value.end_with?('!')
+ result=searchToExclude(key,result,value)
+ else
+ result=searchToInclude(key,result,value)
+ end
+ end
+ result
+ end
+
+ def findWithFilter(lambdaFunc)
+ result=@collection.each.select {|el| lambdaFunc.call el}
+ return result
+ end
+
+ def find(criteria)
+ return @collection if criteria == nil
+ if criteria[:filter]!=nil
+ return findWithFilter(criteria[:filter])
+ end
+ result=@collection
+ criteria.each_key do |key|
+ result=createResultList(key,result,criteria)
+ end
+ result
+ end
+end