Решение на Втора задача от Георги Лозев

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

Към профила на Георги Лозев

Резултати

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

Код

class Song
def initialize(name, artist, genre, subgenre, tags)
@name = name
@artist = artist
@genre = genre
@subgenre = subgenre
@tags = tags
end
attr_accessor :name, :artist, :genre, :subgenre, :tags
def has_tags?(tags_array)
with_excl = tags_array.select{ |tag| tag.end_with?("!") }
without_excl = tags_array - with_excl
(without_excl == (@tags & without_excl)) && ([] == (@tags & with_excl))
end
end
class Collection
def initialize(songs_as_string, artist_tags)
@all_songs = songs_factory(songs_as_string, artist_tags)
@found_songs = []
end
def find(criteria)
if criteria.empty?
return @all_songs
end
@found_songs = []
if criteria.has_key?(:name)
find_song_by_name(criteria[:name])
end
if criteria.has_key?(:artist)
find_song_by_artist(criteria[:artist])
end
if criteria.has_key?(:tags)
find_song_by_tags(criteria[:tags])
end
if criteria.has_key?(:filter)
find_song_by_filter(criteria[:filter])
end
@found_songs
end
private
def songs_factory(songs_as_string, artist_tags)
all_songs = []
songs_as_string.each_line do |song_line|
song = create_song_from_line(song_line, artist_tags)
all_songs << song
end
all_songs
end
def create_song_from_line(song_line, artist_tags)
song_fields = song_line.split(".").collect(&:strip)
song_name, song_artist = song_fields[0], song_fields[1]
genre, subgenre = song_fields[2].split(",").collect(&:strip)
if song_fields[3] != nil
all_tags = Array(song_fields[3].split(",")).collect(&:strip)
else
all_tags = []
end
all_tags << genre.downcase
all_tags << subgenre.downcase if subgenre != nil
if artist_tags.has_key?(song_artist)
all_tags += artist_tags[song_artist]
end
Song.new(song_name, song_artist, genre, subgenre, all_tags)
end
def find_song_by_name(name)
if @found_songs.empty?
@all_songs.each{ |song| @found_songs << song if song.name == name }
else
@found_songs = @found_songs.select{ |song| song.name == name }
end
end
def find_song_by_artist(artist)
if @found_songs.empty?
@all_songs.each{ |song| @found_songs << song if song.artist == artist }
else
@found_songs = @found_songs.select{ |song| song.artist == artist }
end
end
def find_song_by_tags(tags)
tags_array = Array[tags]
if @found_songs.empty?
@all_songs.each{ |sg| @found_songs << sg if sg.has_tags?(tags_array) }
else
@found_songs = @found_songs.select{ |song| song.has_tags?(tags_array) }
end
end
def find_song_by_filter(filter)
if @found_songs.empty?
@all_songs.each{ |song| @found_songs << song if filter.(song) }
else
@found_songs = @found_songs.select{ |song| filter.(song) }
end
end
end

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

.....FF.....

Failures:

  1) Collection can find songs by multiple tags
     Failure/Error: collection.find(options)
     NoMethodError:
       undefined method `end_with?' for ["popular", "violin"]:Array
     # /tmp/d20111115-13548-w7dx5r/solution.rb:13:in `block in has_tags?'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:13:in `select'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:13:in `has_tags?'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:92:in `block in find_song_by_tags'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:92:in `each'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:92:in `find_song_by_tags'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:38:in `find'
     # /tmp/d20111115-13548-w7dx5r/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-w7dx5r/spec.rb:57: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 that don't have a tag
     Failure/Error: collection.find(options)
     NoMethodError:
       undefined method `end_with?' for ["weird", "cool!"]:Array
     # /tmp/d20111115-13548-w7dx5r/solution.rb:13:in `block in has_tags?'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:13:in `select'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:13:in `has_tags?'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:92:in `block in find_song_by_tags'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:92:in `each'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:92:in `find_song_by_tags'
     # /tmp/d20111115-13548-w7dx5r/solution.rb:38:in `find'
     # /tmp/d20111115-13548-w7dx5r/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-w7dx5r/spec.rb:61: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.56082 seconds
12 examples, 2 failures

Failed examples:

rspec /tmp/d20111115-13548-w7dx5r/spec.rb:56 # Collection can find songs by multiple tags
rspec /tmp/d20111115-13548-w7dx5r/spec.rb:60 # Collection can find songs that don't have a tag

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

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

+class Song
+ def initialize(name, artist, genre, subgenre, tags)
+ @name = name
+ @artist = artist
+ @genre = genre
+ @subgenre = subgenre
+ @tags = tags
+ end
+
+ attr_accessor :name, :artist, :genre, :subgenre, :tags
+
+ def has_tags?(tags_array)
+ with_excl = tags_array.select{ |tag| tag.end_with?("!") }
+ without_excl = tags_array - with_excl
+ (without_excl == (@tags & without_excl)) && ([] == (@tags & with_excl))
+ end
+end
+
+
+class Collection
+ def initialize(songs_as_string, artist_tags)
+ @all_songs = songs_factory(songs_as_string, artist_tags)
+ @found_songs = []
+ end
+
+ def find(criteria)
+ if criteria.empty?
+ return @all_songs
+ end
+ @found_songs = []
+ if criteria.has_key?(:name)
+ find_song_by_name(criteria[:name])
+ end
+ if criteria.has_key?(:artist)
+ find_song_by_artist(criteria[:artist])
+ end
+ if criteria.has_key?(:tags)
+ find_song_by_tags(criteria[:tags])
+ end
+ if criteria.has_key?(:filter)
+ find_song_by_filter(criteria[:filter])
+ end
+ @found_songs
+ end
+
+ private
+ def songs_factory(songs_as_string, artist_tags)
+ all_songs = []
+ songs_as_string.each_line do |song_line|
+ song = create_song_from_line(song_line, artist_tags)
+ all_songs << song
+ end
+ all_songs
+ end
+
+ def create_song_from_line(song_line, artist_tags)
+ song_fields = song_line.split(".").collect(&:strip)
+ song_name, song_artist = song_fields[0], song_fields[1]
+ genre, subgenre = song_fields[2].split(",").collect(&:strip)
+ if song_fields[3] != nil
+ all_tags = Array(song_fields[3].split(",")).collect(&:strip)
+ else
+ all_tags = []
+ end
+ all_tags << genre.downcase
+ all_tags << subgenre.downcase if subgenre != nil
+ if artist_tags.has_key?(song_artist)
+ all_tags += artist_tags[song_artist]
+ end
+ Song.new(song_name, song_artist, genre, subgenre, all_tags)
+ end
+
+ def find_song_by_name(name)
+ if @found_songs.empty?
+ @all_songs.each{ |song| @found_songs << song if song.name == name }
+ else
+ @found_songs = @found_songs.select{ |song| song.name == name }
+ end
+ end
+
+ def find_song_by_artist(artist)
+ if @found_songs.empty?
+ @all_songs.each{ |song| @found_songs << song if song.artist == artist }
+ else
+ @found_songs = @found_songs.select{ |song| song.artist == artist }
+ end
+ end
+
+ def find_song_by_tags(tags)
+ tags_array = Array[tags]
+ if @found_songs.empty?
+ @all_songs.each{ |sg| @found_songs << sg if sg.has_tags?(tags_array) }
+ else
+ @found_songs = @found_songs.select{ |song| song.has_tags?(tags_array) }
+ end
+ end
+
+ def find_song_by_filter(filter)
+ if @found_songs.empty?
+ @all_songs.each{ |song| @found_songs << song if filter.(song) }
+ else
+ @found_songs = @found_songs.select{ |song| filter.(song) }
+ end
+ end
+end