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

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

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

Резултати

  • 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

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

........F.F.

Failures:

  1) Collection adds the artist tags to the songs
     Failure/Error: songs(tags: 'polyphone').map(&:name).should =~ ['Toccata e Fuga', 'Goldberg Variations']
       expected collection contained:  ["Goldberg Variations", "Toccata e Fuga"]
       actual collection contained:    []
       the missing elements were:      ["Goldberg Variations", "Toccata e Fuga"]
     # /tmp/d20111115-13548-1i6wwdy/spec.rb:69:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Collection allows all criteria
     Failure/Error: ).map(&:artist).should eq ['Thelonious Monk']
       
       expected: ["Thelonious Monk"]
            got: ["John Coltrane", "John Coltrane", "John Coltrane", "John Coltrane", "John Coltrane", "John Coltrane", "Miles Davis", "Miles Davis", "Miles Davis", "Miles Davis", "Bill Evans", "Bill Evans", "Thelonious Monk", "Thelonious Monk"]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,15 @@
       -["Thelonious Monk"]
       +["John Coltrane",
       + "John Coltrane",
       + "John Coltrane",
       + "John Coltrane",
       + "John Coltrane",
       + "John Coltrane",
       + "Miles Davis",
       + "Miles Davis",
       + "Miles Davis",
       + "Miles Davis",
       + "Bill Evans",
       + "Bill Evans",
       + "Thelonious Monk",
       + "Thelonious Monk"]
     # /tmp/d20111115-13548-1i6wwdy/spec.rb:82:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.54404 seconds
12 examples, 2 failures

Failed examples:

rspec /tmp/d20111115-13548-1i6wwdy/spec.rb:68 # Collection adds the artist tags to the songs
rspec /tmp/d20111115-13548-1i6wwdy/spec.rb:76 # Collection allows all criteria

История (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