Решение на Втора задача от Славена Василева

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

Към профила на Славена Василева

Резултати

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

Код

class Song
attr_accessor :name, :artist, :genre, :subgenre, :tags
def initialize(song_as_string)
songParams = song_as_string.strip.split(%r{\.\s*})
@name, @artist = songParams
genre_subgenre = songParams[2].split(%r{,\s})
@genre, @subgenre = genre_subgenre
@tags = (songParams[3] || "").split(%r{,\s})
@tags += genre_subgenre.map(&:downcase)
end
end
class Collection
def initialize(songs_as_string, artist_tags)
@songs = songs_as_string.lines.map { |song| Song.new(song)}
@songs.each do |song|
song.tags += artist_tags.fetch(song.artist, [])
puts song
end
end
def find criteria
@songs.select do |song|
isMatch = true
criteria.each {| key, value | isMatch &&= match?(song, key, value)}
isMatch
end
end
def match? song, type, value
return case type
when :name
value == song.name
when :artist
value == song.artist
when :filter
value[song]
when :tags
matchTags?(song.tags, value)
end
end
def matchTags? tags, criteriaTags
Array(criteriaTags).all? do |tag|
if tag[-1] == '!'
not(tags.member? tag.chop)
else
tags.member? tag
end
end
end
end

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

#<Song:0xa3ece48>
#<Song:0xa3ecc04>
#<Song:0xa3ec9fc>
#<Song:0xa3ec830>
#<Song:0xa3ec678>
#<Song:0xa3ec4fc>
#<Song:0xa3ec394>
#<Song:0xa3ec218>
#<Song:0xa3ec010>
#<Song:0xa3ebe44>
#<Song:0xa3ebc8c>
#<Song:0xa3ebb10>
#<Song:0xa3eb9a8>
#<Song:0xa3eb7f0>
#<Song:0xa3eb674>
#<Song:0xa3eb4f8>
#<Song:0xa3eb37c>
#<Song:0xa3eb214>
#<Song:0xa3eb048>
#<Song:0xa3eae90>
.#<Song:0xa3ff2dc>
#<Song:0xa3ff0d4>
#<Song:0xa3feecc>
#<Song:0xa3fed00>
#<Song:0xa3feb48>
#<Song:0xa3fe9cc>
#<Song:0xa3fe864>
#<Song:0xa3fe6e8>
#<Song:0xa3fe4e0>
#<Song:0xa3fe314>
#<Song:0xa3fe15c>
#<Song:0xa3fdfe0>
#<Song:0xa3fde78>
#<Song:0xa3fdcc0>
#<Song:0xa3fdb44>
#<Song:0xa3fd9c8>
#<Song:0xa3fd84c>
#<Song:0xa3fd6e4>
#<Song:0xa3fd518>
#<Song:0xa3fd360>
.#<Song:0xa3fc334>
#<Song:0xa3fc12c>
#<Song:0xa5ed1fc>
#<Song:0xa5ed030>
#<Song:0xa5ece78>
#<Song:0xa5eccfc>
#<Song:0xa5ecb94>
#<Song:0xa5eca18>
#<Song:0xa5ec810>
#<Song:0xa5ec644>
#<Song:0xa5ec48c>
#<Song:0xa5ec310>
#<Song:0xa5ec1a8>
#<Song:0xa5ebff0>
#<Song:0xa5ebe74>
#<Song:0xa5ebcf8>
#<Song:0xa5ebb7c>
#<Song:0xa5eba14>
#<Song:0xa5eb848>
#<Song:0xa5eb690>
.#<Song:0xa5ea664>
#<Song:0xa5ea45c>
#<Song:0xa5ea254>
#<Song:0xa5ea088>
#<Song:0xa5e9ed0>
#<Song:0xa5e9d54>
#<Song:0xa5e9bec>
#<Song:0xa5e9a70>
#<Song:0xa5e9868>
#<Song:0xa5e969c>
#<Song:0xa5e94e4>
#<Song:0xa5e9368>
#<Song:0xa5fa3e8>
#<Song:0xa5fa230>
#<Song:0xa5fa0b4>
#<Song:0xa5f9f38>
#<Song:0xa5f9dbc>
#<Song:0xa5f9c54>
#<Song:0xa5f9a88>
#<Song:0xa5f98d0>
.#<Song:0xa5f8750>
#<Song:0xa5f8548>
#<Song:0xa5f8340>
#<Song:0xa5f8174>
#<Song:0xa5f7fbc>
#<Song:0xa5f7e40>
#<Song:0xa5f7cd8>
#<Song:0xa5f7b5c>
#<Song:0xa5f7954>
#<Song:0xa5f7788>
#<Song:0xa5f75d0>
#<Song:0xa5f7454>
#<Song:0xa5f72ec>
#<Song:0xa5f7134>
#<Song:0xa5f6fb8>
#<Song:0xa5f6e3c>
#<Song:0xa5f6cc0>
#<Song:0xa5f6b58>
#<Song:0xa5f698c>
#<Song:0xa5f67d4>
.#<Song:0xa1aa950>
#<Song:0xa1aa748>
#<Song:0xa1aa540>
#<Song:0xa1aa374>
#<Song:0xa1aa1bc>
#<Song:0xa1aa040>
#<Song:0xa1a9ed8>
#<Song:0xa1a9d5c>
#<Song:0xa1a9b54>
#<Song:0xa1a9988>
#<Song:0xa1a97d0>
#<Song:0xa1a9654>
#<Song:0xa1a94ec>
#<Song:0xa1a9334>
#<Song:0xa1a91b8>
#<Song:0xa1a903c>
#<Song:0xa1a8ec0>
#<Song:0xa1a8d58>
#<Song:0xa1a8b8c>
#<Song:0xa1a89d4>
.#<Song:0xa1b57b0>
#<Song:0xa1b55a8>
#<Song:0xa1b53a0>
#<Song:0xa1b51d4>
#<Song:0xa1b501c>
#<Song:0xa1b4ea0>
#<Song:0xa1b4d38>
#<Song:0xa1b4bbc>
#<Song:0xa1b49b4>
#<Song:0xa1b47e8>
#<Song:0xa1b4630>
#<Song:0xa1b44b4>
#<Song:0xa1b434c>
#<Song:0xa1b4194>
#<Song:0xa1b4018>
#<Song:0xa1b3e9c>
#<Song:0xa1b3d20>
#<Song:0xa1b3bb8>
#<Song:0xa1b39ec>
#<Song:0xa1b3834>
.#<Song:0xa1c28ac>
#<Song:0xa1c26a4>
#<Song:0xa1c249c>
#<Song:0xa1c22d0>
#<Song:0xa1c2118>
#<Song:0xa1c1f9c>
#<Song:0xa1c1e34>
#<Song:0xa1c1cb8>
#<Song:0xa1c1ab0>
#<Song:0xa1c18e4>
#<Song:0xa1c172c>
#<Song:0xa1c15b0>
#<Song:0xa1c1448>
#<Song:0xa1c1290>
#<Song:0xa1c1114>
#<Song:0xa1c0f98>
#<Song:0xa1c0e1c>
#<Song:0xa1c0cb4>
#<Song:0xa1c0ae8>
#<Song:0xa1c0930>
.#<Song:0xa1bf620>
#<Song:0xa22bc44>
#<Song:0xa22ba3c>
#<Song:0xa22b870>
#<Song:0xa22b6b8>
#<Song:0xa22b53c>
#<Song:0xa22b3d4>
#<Song:0xa22b258>
#<Song:0xa22b050>
#<Song:0xa22ae84>
#<Song:0xa22accc>
#<Song:0xa22ab50>
#<Song:0xa22a9e8>
#<Song:0xa22a830>
#<Song:0xa22a6b4>
#<Song:0xa22a538>
#<Song:0xa22a3bc>
#<Song:0xa22a254>
#<Song:0xa22a088>
#<Song:0xa229ed0>
.#<Song:0xa22856c>
#<Song:0xa228364>
#<Song:0xa22815c>
#<Song:0xa227f90>
#<Song:0xa227dd8>
#<Song:0xa237904>
#<Song:0xa23779c>
#<Song:0xa237620>
#<Song:0xa237418>
#<Song:0xa23724c>
#<Song:0xa237094>
#<Song:0xa236f18>
#<Song:0xa236db0>
#<Song:0xa236bf8>
#<Song:0xa236a7c>
#<Song:0xa236900>
#<Song:0xa236784>
#<Song:0xa23661c>
#<Song:0xa236450>
#<Song:0xa236298>
.#<Song:0xa234e0c>
#<Song:0xa234c04>
#<Song:0xa2349fc>
#<Song:0xa234830>
#<Song:0xa234678>
#<Song:0xa2344fc>
#<Song:0xa234394>
#<Song:0xa234218>
#<Song:0xa234010>
#<Song:0xa233e44>
#<Song:0xa233c8c>
#<Song:0xa233b10>
#<Song:0xa2339a8>
#<Song:0xa2451f8>
#<Song:0xa24507c>
#<Song:0xa244f00>
#<Song:0xa244d84>
#<Song:0xa244c1c>
#<Song:0xa244a50>
#<Song:0xa244898>
.#<Song:0xa24318c>
#<Song:0xa242f84>
#<Song:0xa242d7c>
#<Song:0xa242bb0>
#<Song:0xa2429f8>
#<Song:0xa24287c>
#<Song:0xa242714>
#<Song:0xa242598>
#<Song:0xa242390>
#<Song:0xa2421c4>
#<Song:0xa24200c>
#<Song:0xa241e90>
#<Song:0xa241d28>
#<Song:0xa241b70>
#<Song:0xa2419f4>
#<Song:0xa241878>
#<Song:0xa2416fc>
#<Song:0xa241594>
#<Song:0xa2413c8>
#<Song:0xa04978c>
.

Finished in 0.54873 seconds
12 examples, 0 failures

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

Славена обнови решението на 31.10.2011 04:18 (преди около 13 години)

+class Song
+
+ attr_accessor :name, :artist, :genre, :subgenre, :tags
+
+ def initialize(song_as_string)
+ songParams = song_as_string.strip.split(%r{\.\s*})
+ @name, @artist = songParams
+ genre_subgenre = songParams[2].split(%r{,\s})
+ @genre, @subgenre = genre_subgenre
+ @tags = (songParams[3] || "").split(%r{,\s})
+ @tags += genre_subgenre.map(&:downcase)
+ end
+
+end
+
+class Collection
+
+ def initialize(songs_as_string, artist_tags)
+ @songs = songs_as_string.lines.map { |song| Song.new(song)}
+ @songs.each do |song|
+ song.tags += artist_tags.fetch(song.artist, [])
+ puts song
+ end
+ end
+
+ def find criteria
+ @songs.select do |song|
+ isMatch = true
+ criteria.each {| key, value | isMatch &&= match?(song, key, value)}
+ isMatch
+ end
+ end
+
+ def match? song, type, value
+ return case type
+ when :name
+ value == song.name
+ when :artist
+ value == song.artist
+ when :filter
+ value[song]
+ when :tags
+ matchTags?(song.tags, value)
+ end
+ end
+
+ def matchTags? tags, criteriaTags
+ Array(criteriaTags).all? do |tag|
+ if tag[-1] == '!'
+ not(tags.member? tag.chop)
+ else
+ tags.member? tag
+ end
+ end
+ end
+
+end
  • Променливите в Ruby са underscore_case, а не camelCase.
  • Аналогично за методите - match_tags?, а не matchTags?.
  • Няма нужда от return-а в имплементацията ти на match?.
  • Виж String#end_with?, вместо tag[-1] ==.
  • puts song в Collection#initialize няма място там.
  • Не оставяй празен ред след class дефиниции
  • Имплементацията ти на find ползва temp и #each. Искала си да напишеш #all? там. Кода става по-прост и по-разбираем.
  • Регулярни изрази се записват така /\.\s*/, /,\s/, /,\s/. %r{} се ползва, когато в регулярния ти израз има /.
  • Отделно, тези регуларни изрази са криви, понеже са чуствителни на whitespace. Няма да работят с тези тагове: jazz,cool, slow ,funny