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

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

Към профила на Ростислав Градинаров

Резултати

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

Код

#!/usr/bin/env ruby
class Song
def initialize(line)
arr = []
line.each_line('.') { |s| arr<<s }
@s_name = arr[0].delete(".").rstrip.lstrip
@s_artist = arr[1].delete(".").lstrip
@s_genre = arr[2].partition(",").first.delete(".").lstrip
@s_subgenre = arr[2]=="" ? nil:arr[2].partition(",").last.delete(".").lstrip
@s_tags=Array.new([@s_genre.to_s.downcase,@s_subgenre.to_s.downcase])
if !(arr[2].include?("\n"))
@s_tags<<arr[3].lstrip.tr!("\n", "")
@s_tags.flatten!
end
@s_tags.delete_if {|x| x=="" || x==nil}
end
def full_name
str = name+artist+genre+subgenre+tags.to_s
return str
end
def name
return @s_name
end
def artist
return @s_artist
end
def genre
return @s_genre
end
def subgenre
return @s_subgenre
end
def tags
return @s_tags
end
def add_tags(more_tags)
@s_tags<<more_tags
@s_tags.flatten!
@s_tags.each do |tag|
tag.delete!("\n")
end
end
def ret(what)
case what
when "name"
self.name
when "artist"
self.artist
when "genre"
self.genre
when "subgenre"
self.subgenre
when "tags"
self.tags
end
end
end
class Collection
def initialize(songs_as_string, artist_tags)
arr=[]
songs_as_string.each_line {|s| arr<<Song.new(s)}
artist_tags.each do |key, value|
arr.each do |song|
if key==song.artist
song.add_tags(value)
end
end
end
@songs = arr
end
def check(song, hsh)
arr=[]
hsh.each do |key, value|
arr<<value
end
arr.flatten!
arr.each do |str|
if str.include?("!") && song.include?(str.delete("!"))
return false
elsif !(song.include?(str)) && !(str.include?("!"))
return false
end
end
return true
end
def find(criteria)
arr=[]
@songs.each do |song|
if check(song.full_name, criteria)==true
arr<<song
end
end
return arr
end
end

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

.......F..FF

Failures:

  1) Collection can filter songs by a lambda
     Failure/Error: collection.find(options)
     NoMethodError:
       undefined method `include?' for #<Proc:0xaa77024@/tmp/d20111115-13548-e5iug3/spec.rb:65 (lambda)>
     # /tmp/d20111115-13548-e5iug3/solution.rb:91:in `block in check'
     # /tmp/d20111115-13548-e5iug3/solution.rb:90:in `each'
     # /tmp/d20111115-13548-e5iug3/solution.rb:90:in `check'
     # /tmp/d20111115-13548-e5iug3/solution.rb:103:in `block in find'
     # /tmp/d20111115-13548-e5iug3/solution.rb:102:in `each'
     # /tmp/d20111115-13548-e5iug3/solution.rb:102:in `find'
     # /tmp/d20111115-13548-e5iug3/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-e5iug3/spec.rb:65: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: collection.find(options)
     NoMethodError:
       undefined method `include?' for #<Proc:0xa9c5478@/tmp/d20111115-13548-e5iug3/spec.rb:81 (lambda)>
     # /tmp/d20111115-13548-e5iug3/solution.rb:91:in `block in check'
     # /tmp/d20111115-13548-e5iug3/solution.rb:90:in `each'
     # /tmp/d20111115-13548-e5iug3/solution.rb:90:in `check'
     # /tmp/d20111115-13548-e5iug3/solution.rb:103:in `block in find'
     # /tmp/d20111115-13548-e5iug3/solution.rb:102:in `each'
     # /tmp/d20111115-13548-e5iug3/solution.rb:102:in `find'
     # /tmp/d20111115-13548-e5iug3/spec.rb:96:in `songs'
     # /tmp/d20111115-13548-e5iug3/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)>'

  3) Collection constructs an object for each song
     Failure/Error: song.tags.should      include('weird', 'cool')
       expected ["jazz", "fusion", "weird, cool"] to include "weird" and "cool"
       Diff:
       @@ -1,2 +1,2 @@
       -weird
       +["jazz", "fusion", "weird, cool"]
     # /tmp/d20111115-13548-e5iug3/spec.rb:92: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.55229 seconds
12 examples, 3 failures

Failed examples:

rspec /tmp/d20111115-13548-e5iug3/spec.rb:64 # Collection can filter songs by a lambda
rspec /tmp/d20111115-13548-e5iug3/spec.rb:76 # Collection allows all criteria
rspec /tmp/d20111115-13548-e5iug3/spec.rb:85 # Collection constructs an object for each song

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

Ростислав обнови решението на 31.10.2011 15:12 (преди около 13 години)

+#!/usr/bin/env ruby
+
+class Song
+
+ def initialize(line)
+ arr = []
+ line.each_line('.') { |s| arr<<s }
+ @s_name = arr[0].delete(".").rstrip.lstrip
+ @s_artist = arr[1].delete(".").lstrip
+ @s_genre = arr[2].partition(",").first.delete(".").lstrip
+ @s_subgenre = arr[2]=="" ? nil:arr[2].partition(",").last.delete(".").lstrip
+ @s_tags=Array.new([@s_genre.to_s.downcase,@s_subgenre.to_s.downcase])
+ if !(arr[2].include?("\n"))
+ @s_tags<<arr[3].lstrip.tr!("\n", "")
+ @s_tags.flatten!
+ end
+ @s_tags.delete_if {|x| x=="" || x==nil}
+ end
+
+ def full_name
+ str = name+artist+genre+subgenre+tags.to_s
+ return str
+ end
+
+ def name
+ return @s_name
+ end
+
+ def artist
+ return @s_artist
+ end
+
+ def genre
+ return @s_genre
+ end
+
+ def subgenre
+ return @s_subgenre
+ end
+
+ def tags
+ return @s_tags
+ end
+
+ def add_tags(more_tags)
+ @s_tags<<more_tags
+ @s_tags.flatten!
+ @s_tags.each do |tag|
+ tag.delete!("\n")
+ end
+ end
+
+ def ret(what)
+ case what
+ when "name"
+ self.name
+ when "artist"
+ self.artist
+ when "genre"
+ self.genre
+ when "subgenre"
+ self.subgenre
+ when "tags"
+ self.tags
+ end
+ end
+end
+
+class Collection
+
+ def initialize(songs_as_string, artist_tags)
+ arr=[]
+ songs_as_string.each_line {|s| arr<<Song.new(s)}
+ artist_tags.each do |key, value|
+ arr.each do |song|
+ if key==song.artist
+ song.add_tags(value)
+ end
+ end
+ end
+ @songs = arr
+ end
+
+ def check(song, hsh)
+ arr=[]
+ hsh.each do |key, value|
+ arr<<value
+ end
+ arr.flatten!
+ arr.each do |str|
+ if str.include?("!") && song.include?(str.delete("!"))
+ return false
+ elsif !(song.include?(str)) && !(str.include?("!"))
+ return false
+ end
+ end
+ return true
+ end
+
+ def find(criteria)
+ arr=[]
+ @songs.each do |song|
+ if check(song.full_name, criteria)==true
+ arr<<song
+ end
+ end
+ return arr
+ end
+
+end