Решение на Втора задача от Константин Добрев

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

Към профила на Константин Добрев

Резултати

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

Код

class Song
attr_accessor :name, :artist, :genre, :subgenre, :tags
def initialize(name, artist, genre, subgenre, tags)
@name, @artist, @genre = name, artist, genre
@subgenre, @tags = subgenre, tags
end
def initialize song_as_string
song_array = song_as_string.strip.split(".")
@name = song_array[0]
@artist = song_array[1]
@genre = song_array[2].split(",")[0]
@subgenre = song_array[2].split(",")[1]
unless song_array[3] == nil
@tags = song_array[3].split(",")
end
proper_format
end
def proper_format
unless @subgenre == nil
@subgenre.strip!
end
unless @tags == nil
@tags.each do |tag|
tag.strip!
tag.downcase!
end
end
end
def to_string
song = @name + '.' + @artist + '.' + @genre
unless @subgenre == nil
song += (". " + @subgenre)
end
unless @tags == nil
song += (", " + @tags.join(", "))
end
song
end
def has_tag? tag
if @tags == nil
return tag.end_with? '!'
end
if tag.end_with? '!'
return (not (@tags.include? tag.chop.downcase))
end
@tags.include? tag.downcase
end
def has_tags? tags
if tags.kind_of?(Array) == true
tags.each do |tag|
if (has_tag? tag) == false
return false
end
end
else
return has_tag?(tags)
end
true
end
def single_criteria?(key, value = nil)
result = case key
when :name then (@name == value)
when :artist then (@artist == value)
when :tags then (self.has_tags? value)
when :filter then value.call(self)
end
result
end
def song_fits?(criteria)
criteria.each_pair do |key, value|
if single_criteria?(key, value) == false
return false
end
end
true
end
end
class Collection
attr_accessor :songs_as_string, :artist_tags
def initialize(songs_as_string, artist_tags)
@songs_as_string, @artist_tags = songs_as_string, artist_tags
end
def songs_array
result = []
@songs_as_string.each_line do |line|
result.push(Song.new(line.squeeze(" ").gsub(". ", ".")))
end
result
end
def find(criteria)
songs_array.select { |song| song.song_fits?(criteria) == true}
end
end

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

...FF...FFF.

Failures:

  1) Collection uses the genre and subgenre as tags
     Failure/Error: song(name: 'Miles Runs the Voodoo Down').tags.should include('jazz', 'fusion')
       expected ["weird"] to include "jazz" and "fusion"
       Diff:
       @@ -1,2 +1,2 @@
       -jazz
       +["weird"]
     # /tmp/d20111115-13548-1g5jqkf/spec.rb:49: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 can find songs by tag
     Failure/Error: songs(tags: 'baroque').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-1g5jqkf/spec.rb:53: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)>'

  3) 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-1g5jqkf/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)>'

  4) Collection allows multiple criteria
     Failure/Error: songs(name: "'Round Midnight", tags: 'bebop').map(&:artist).should eq ['Thelonious Monk']
       
       expected: ["Thelonious Monk"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Thelonious Monk"]
       +[]
     # /tmp/d20111115-13548-1g5jqkf/spec.rb:73: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)>'

  5) Collection allows all criteria
     Failure/Error: ).map(&:artist).should eq ['Thelonious Monk']
       
       expected: ["Thelonious Monk"]
            got: []
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -["Thelonious Monk"]
       +[]
     # /tmp/d20111115-13548-1g5jqkf/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.55028 seconds
12 examples, 5 failures

Failed examples:

rspec /tmp/d20111115-13548-1g5jqkf/spec.rb:48 # Collection uses the genre and subgenre as tags
rspec /tmp/d20111115-13548-1g5jqkf/spec.rb:52 # Collection can find songs by tag
rspec /tmp/d20111115-13548-1g5jqkf/spec.rb:68 # Collection adds the artist tags to the songs
rspec /tmp/d20111115-13548-1g5jqkf/spec.rb:72 # Collection allows multiple criteria
rspec /tmp/d20111115-13548-1g5jqkf/spec.rb:76 # Collection allows all criteria

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

Константин обнови решението на 31.10.2011 16:45 (преди над 12 години)

+class Song
+
+attr_accessor :name, :artist, :genre, :subgenre, :tags
+
+def initialize(name, artist, genre, subgenre, tags)
+ @name, @artist, @genre = name, artist, genre
+ @subgenre, @tags = subgenre, tags
+end
+
+def initialize song_as_string
+ song_array = song_as_string.strip.split(".")
+ @name = song_array[0]
+ @artist = song_array[1]
+ @genre = song_array[2].split(",")[0]
+ @subgenre = song_array[2].split(",")[1]
+ unless song_array[3] == nil
+ @tags = song_array[3].split(",")
+ end
+ proper_format
+end
+
+def proper_format
+ unless @subgenre == nil
+ @subgenre.strip!
+ end
+ unless @tags == nil
+ @tags.each do |tag|
+ tag.strip!
+ tag.downcase!
+ end
+ end
+end
+
+def to_string
+ song = @name + '.' + @artist + '.' + @genre
+ unless @subgenre == nil
+ song += (". " + @subgenre)
+ end
+ unless @tags == nil
+ song += (", " + @tags.join(", "))
+ end
+ song
+end
+
+def has_tag? tag
+ if @tags == nil
+ return tag.end_with? '!'
+ end
+ if tag.end_with? '!'
+ return (not (@tags.include? tag.chop.downcase))
+ end
+ @tags.include? tag.downcase
+end
+
+def has_tags? tags
+ if tags.kind_of?(Array) == true
+ tags.each do |tag|
+ if (has_tag? tag) == false
+ return false
+ end
+ end
+ else
+ return has_tag?(tags)
+ end
+ true
+end
+
+def single_criteria?(key, value = nil)
+ result = case key
+ when :name then (@name == value)
+ when :artist then (@artist == value)
+ when :tags then (self.has_tags? value)
+ when :filter then value.call(self)
+ end
+ result
+end
+
+def song_fits?(criteria)
+ criteria.each_pair do |key, value|
+ if single_criteria?(key, value) == false
+ return false
+ end
+ end
+ true
+end
+
+end
+
+class Collection
+
+attr_accessor :songs_as_string, :artist_tags
+
+def initialize(songs_as_string, artist_tags)
+ @songs_as_string, @artist_tags = songs_as_string, artist_tags
+end
+
+def songs_array
+ result = []
+ @songs_as_string.each_line do |line|
+ result.push(Song.new(line.squeeze(" ").gsub(". ", ".")))
+ end
+ result
+end
+
+def find(criteria)
+ songs_array.select { |song| song.song_fits?(criteria) == true}
+end
+
+end