Решение на Втора задача от Неделчо Бозев

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

Към профила на Неделчо Бозев

Резултати

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

Код

text = "Fields of Gold
Sting
Ten Summoner\'s Tales
Mad About You
Sting
The Soul Cages
Fields of Gold
Eva Cassidy
Live at Blues Alley
Autumn Leaves
Eva Cassidy
Live at Blues Alley
Autumn Leaves
Bill Evans
Portrait in Jazz
Brain of J.F.K
Pearl Jam
Yield
Jeremy
Pearl Jam
Ten
Come Away With Me
Norah Johnes
One
Acknowledgment
John Coltrane
A Love Supreme
Ruby, My Dear
Thelonious Monk
Mysterioso"
class Song
def initialize(name , artist, album)
@name, @artist, @album = name, artist, album
end
def name
"#{@name}"
end
def artist
"#{@artist}"
end
def album
"#{@album}"
end
end
class Collection
include Enumerable
def initialize(songs)
@songs = songs
end
def Collection.parse(text)
Collection.new text.split("\n").each_slice(4).map{|r,s,a,e| Song.new(r,s,a)}
end
def names
nams = []
@songs.map{ |s| nams.push s.name}
nams.uniq
end
def artists
arts = []
@songs.map{ |s| arts.push s.artist }
arts.uniq
end
def albums
albs = []
@songs.map{ |s| albs.push s.album }
albs .uniq
end
def each
@songs.select{ |s| yield s }
end
def filter(criteria)
value = criteria.get_criteria
@songs.select{|s| s.name == value[1] and value[0] =="name"}
@songs.select{|s| s.artist == value[1] and value[0] =="artist"}
@songs.select{|s| s.album == value[1]and value[0] =="album"}
end
end
class Criteria
def initialize( field, value)
@field, @value = field, value
end
def Criteria.name(value)
Criteria.new "name", value
end
def Criteria.artist(value)
Criteria.new "artist", value
end
def Criteria.album(value)
Criteria.new "album", value
end
def get_criteria
[ @field, @value ]
end
end

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

...FF..FFFF

Failures:

  1) Collection can be filtered by song name
     Failure/Error: filtered.map(&:artist).should =~ ['Eva Cassidy', 'Sting']
       expected collection contained:  ["Eva Cassidy", "Sting"]
       actual collection contained:    []
       the missing elements were:      ["Eva Cassidy", "Sting"]
     # /tmp/d20130203-23049-7puyin/spec.rb:45: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 be filtered by song name
     Failure/Error: filtered.map(&:album).should =~ ['The Soul Cages', "Ten Summoner's Tales"]
       expected collection contained:  ["Ten Summoner's Tales", "The Soul Cages"]
       actual collection contained:    []
       the missing elements were:      ["Ten Summoner's Tales", "The Soul Cages"]
     # /tmp/d20130203-23049-7puyin/spec.rb:50: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 supports a conjuction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & Criteria.name('Fields of Gold')
     NoMethodError:
       undefined method `&' for #<Criteria:0x9180e44 @field="artist", @value="Sting">
     # /tmp/d20130203-23049-7puyin/spec.rb:64: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 supports a disjunction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') | Criteria.name('Fields of Gold')
     NoMethodError:
       undefined method `|' for #<Criteria:0x917eea0 @field="artist", @value="Sting">
     # /tmp/d20130203-23049-7puyin/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)>'

  5) Collection supports negation of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & !Criteria.name('Fields of Gold')
     NoMethodError:
       undefined method `&' for #<Criteria:0x9773284 @field="artist", @value="Sting">
     # /tmp/d20130203-23049-7puyin/spec.rb:78: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)>'

  6) Collection can be adjoined with another collection
     Failure/Error: adjoined = sting.adjoin(eva)
     NoMethodError:
       undefined method `adjoin' for []:Array
     # /tmp/d20130203-23049-7puyin/spec.rb:85: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.01054 seconds
11 examples, 6 failures

Failed examples:

rspec /tmp/d20130203-23049-7puyin/spec.rb:43 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-7puyin/spec.rb:48 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-7puyin/spec.rb:63 # Collection supports a conjuction of filters
rspec /tmp/d20130203-23049-7puyin/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-7puyin/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-7puyin/spec.rb:82 # Collection can be adjoined with another collection

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

Неделчо обнови решението на 31.10.2012 01:11 (преди над 11 години)

+text = "Fields of Gold
+Sting
+Ten Summoner\'s Tales
+
+Mad About You
+Sting
+The Soul Cages
+
+Fields of Gold
+Eva Cassidy
+Live at Blues Alley
+
+Autumn Leaves
+Eva Cassidy
+Live at Blues Alley
+
+Autumn Leaves
+Bill Evans
+Portrait in Jazz
+
+Brain of J.F.K
+Pearl Jam
+Yield
+
+Jeremy
+Pearl Jam
+Ten
+
+Come Away With Me
+Norah Johnes
+One
+
+Acknowledgment
+John Coltrane
+A Love Supreme
+
+Ruby, My Dear
+Thelonious Monk
+Mysterioso"
+
+class Song
+ def initialize(name , artist, album)
+ @name, @artist, @album = name, artist, album
+ end
+
+ def name
+ "#{@name}"
+ end
+
+ def artist
+ "#{@artist}"
+ end
+
+ def album
+ "#{@album}"
+ end
+end
+
+class Collection
+ include Enumerable
+
+ def initialize(songs)
+ @songs = songs
+ end
+
+ def Collection.parse(text)
+ Collection.new text.split("\n").each_slice(4).map{|r,s,a,e| Song.new(r,s,a)}
+ end
+
+ def names
+ nams = []
+ @songs.map{ |s| nams.push s.name}
+ nams.uniq
+ end
+
+ def artists
+ arts = []
+ @songs.map{ |s| arts.push s.artist }
+ arts.uniq
+ end
+
+ def albums
+ albs = []
+ @songs.map{ |s| albs.push s.album }
+ albs .uniq
+ end
+
+ def each
+ @songs.select{ |s| yield s }
+ end
+
+ def filter(criteria)
+ value = criteria.get_criteria
+ @songs.select{|s| s.name == value[1] and value[0] =="name"}
+ @songs.select{|s| s.artist == value[1] and value[0] =="artist"}
+ @songs.select{|s| s.album == value[1]and value[0] =="album"}
+ end
+end
+
+class Criteria
+ def initialize( field, value)
+ @field, @value = field, value
+ end
+
+ def Criteria.name(value)
+ Criteria.new "name", value
+ end
+
+ def Criteria.artist(value)
+ Criteria.new "artist", value
+ end
+
+ def Criteria.album(value)
+ Criteria.new "album", value
+ end
+
+ def get_criteria
+ [ @field, @value ]
+ end
+end