Решение на Втора задача от Даяна Беркова

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

Към профила на Даяна Беркова

Резултати

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

Код

class Criteria
attr_accessor :name, :artist, :album, :criteria
def initialize(input_string='',where_to_input='')
@hash = Hash[:name, nil, :artist, nil, :album, nil]
@criteria = []
@hash[where_to_input] = input_string.dup if input_string != ''
@criteria << @hash if input_string != ''
end
def Criteria.name(def_name)
Criteria.new(def_name,:name)
end
def Criteria.artist(def_artist)
Criteria.new(def_artist,:artist)
end
def Criteria.album(def_album)
Criteria.new(def_album,:album)
end
def |(other)
@criteria << other.criteria[0]
self
end
def !
self
end
def &(other)
self
end
end
class Song
attr_accessor :name, :artist, :album
def initialize(array_songs)
@name, @artist, @album = array_songs[0], array_songs[1], array_songs[2]
end
def matches?(criteria, songs_array)
criteria.criteria.map { |song| check_if_any(song, songs_array) }
songs_array.uniq
end
def check_if_any(song, songs_array)
song.any? { |type, value| check_by_type(type, value, songs_array) }
end
def check_by_type(type, value, songs_array)
case type
when :name then check_by_value(name, value, songs_array)
when :artist then check_by_value(artist, value, songs_array)
when :album then check_by_value(album, value, songs_array)
end
end
def check_by_value(type, value, songs_array)
songs_array << "#{name}, #{artist}, #{album}" if type == value
end
end
class Collection
include Enumerable
attr_accessor :songs, :songs_array
def initialize(text)
@songs = text.split("\n").select { |n| n.length != 0 }
@songs = @songs.each_slice(3).to_a
@songs.each_index { |index| @songs[index] = Song.new( @songs[index]) }
@songs_array = []
end
def Collection.parse(text)
self.new(text)
end
def artists
artists = []
@songs.map { |text| artists << text.artist }
artists.uniq
end
def albums
albums = []
@songs.map { |text| albums << text.album }
albums.uniq
end
def names
names = []
@songs.map { |text| names << text.name}
names.uniq
end
def filter(criteria)
@songs.map { |song| song.matches?(criteria, songs_array) }
p @songs_array
self
end
def adjoin(criteria)
@songs_array
end
def each
@songs.each { |result| yield result }
end
end

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

...["Fields of Gold, Sting, Ten Summoner's Tales", "Fields of Gold, Eva Cassidy, Live at Blues Alley"]
F["Fields of Gold, Sting, Ten Summoner's Tales", "Mad About You, Sting, The Soul Cages"]
F["Fields of Gold, Eva Cassidy, Live at Blues Alley", "Autumn Leaves, Eva Cassidy, Live at Blues Alley"]
F[]
F["Fields of Gold, Sting, Ten Summoner's Tales", "Mad About You, Sting, The Soul Cages"]
F["Fields of Gold, Sting, Ten Summoner's Tales", "Fields of Gold, Sting, Ten Summoner's Tales", "Mad About You, Sting, The Soul Cages", "Fields of Gold, Eva Cassidy, Live at Blues Alley"]
F["Fields of Gold, Sting, Ten Summoner's Tales", "Mad About You, Sting, The Soul Cages"]
F["Fields of Gold, Sting, Ten Summoner's Tales", "Mad About You, Sting, The Soul Cages"]
["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"]
F

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:    ["Bill Evans", "Eva Cassidy", "Eva Cassidy", "John Coltrane", "Norah Johnes", "Pearl Jam", "Pearl Jam", "Sting", "Sting", "Thelonious Monk"]
       the extra elements were:        ["Bill Evans", "Eva Cassidy", "John Coltrane", "Norah Johnes", "Pearl Jam", "Pearl Jam", "Sting", "Thelonious Monk"]
     # /tmp/d20130203-23049-1kz7j3n/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:    ["A Love Supreme", "Live at Blues Alley", "Live at Blues Alley", "Mysterioso", "One", "Portrait in Jazz", "Ten", "Ten Summoner's Tales", "The Soul Cages", "Yield"]
       the extra elements were:        ["A Love Supreme", "Live at Blues Alley", "Live at Blues Alley", "Mysterioso", "One", "Portrait in Jazz", "Ten", "Yield"]
     # /tmp/d20130203-23049-1kz7j3n/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 can be filtered by album
     Failure/Error: filtered.map(&:name).should =~ ['Fields of Gold', 'Autumn Leaves']
       expected collection contained:  ["Autumn Leaves", "Fields of Gold"]
       actual collection contained:    ["Acknowledgment", "Autumn Leaves", "Autumn Leaves", "Brain of J.F.K", "Come Away With Me", "Fields of Gold", "Fields of Gold", "Jeremy", "Mad About You", "Ruby, My Dear"]
       the extra elements were:        ["Acknowledgment", "Autumn Leaves", "Brain of J.F.K", "Come Away With Me", "Fields of Gold", "Jeremy", "Mad About You", "Ruby, My Dear"]
     # /tmp/d20130203-23049-1kz7j3n/spec.rb:55: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 can return an empty result
     Failure/Error: filtered.to_a.should eq []
       
       expected: []
            got: [#<Song:0xa0c1aac @name="Fields of Gold", @artist="Sting", @album="Ten Summoner's Tales">, #<Song:0xa0c1a5c @name="Mad About You", @artist="Sting", @album="The Soul Cages">, #<Song:0xa0c1a0c @name="Fields of Gold", @artist="Eva Cassidy", @album="Live at Blues Alley">, #<Song:0xa0c19a8 @name="Autumn Leaves", @artist="Eva Cassidy", @album="Live at Blues Alley">, #<Song:0xa0c1958 @name="Autumn Leaves", @artist="Bill Evans", @album="Portrait in Jazz">, #<Song:0xa0c1930 @name="Brain of J.F.K", @artist="Pearl Jam", @album="Yield">, #<Song:0xa0c1804 @name="Jeremy", @artist="Pearl Jam", @album="Ten">, #<Song:0xa0c17b4 @name="Come Away With Me", @artist="Norah Johnes", @album="One">, #<Song:0xa0c1778 @name="Acknowledgment", @artist="John Coltrane", @album="A Love Supreme">, #<Song:0xa0c164c @name="Ruby, My Dear", @artist="Thelonious Monk", @album="Mysterioso">]
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,35 @@
       -[]
       +[#<Song:0xa0c1aac
       +  @album="Ten Summoner's Tales",
       +  @artist="Sting",
       +  @name="Fields of Gold">,
       + #<Song:0xa0c1a5c
       +  @album="The Soul Cages",
       +  @artist="Sting",
       +  @name="Mad About You">,
       + #<Song:0xa0c1a0c
       +  @album="Live at Blues Alley",
       +  @artist="Eva Cassidy",
       +  @name="Fields of Gold">,
       + #<Song:0xa0c19a8
       +  @album="Live at Blues Alley",
       +  @artist="Eva Cassidy",
       +  @name="Autumn Leaves">,
       + #<Song:0xa0c1958
       +  @album="Portrait in Jazz",
       +  @artist="Bill Evans",
       +  @name="Autumn Leaves">,
       + #<Song:0xa0c1930 @album="Yield", @artist="Pearl Jam", @name="Brain of J.F.K">,
       + #<Song:0xa0c1804 @album="Ten", @artist="Pearl Jam", @name="Jeremy">,
       + #<Song:0xa0c17b4
       +  @album="One",
       +  @artist="Norah Johnes",
       +  @name="Come Away With Me">,
       + #<Song:0xa0c1778
       +  @album="A Love Supreme",
       +  @artist="John Coltrane",
       +  @name="Acknowledgment">,
       + #<Song:0xa0c164c
       +  @album="Mysterioso",
       +  @artist="Thelonious Monk",
       +  @name="Ruby, My Dear">]
     # /tmp/d20130203-23049-1kz7j3n/spec.rb:60: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 a conjuction of filters
     Failure/Error: filtered.map(&:album).should eq ["Ten Summoner's Tales"]
       
       expected: ["Ten Summoner's Tales"]
            got: ["Ten Summoner's Tales", "The Soul Cages", "Live at Blues Alley", "Live at Blues Alley", "Portrait in Jazz", "Yield", "Ten", "One", "A Love Supreme", "Mysterioso"]
       
       (compared using ==)
     # /tmp/d20130203-23049-1kz7j3n/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)>'

  6) Collection supports a disjunction of filters
     Failure/Error: ]
       expected collection contained:  ["Live at Blues Alley", "Ten Summoner's Tales", "The Soul Cages"]
       actual collection contained:    ["A Love Supreme", "Live at Blues Alley", "Live at Blues Alley", "Mysterioso", "One", "Portrait in Jazz", "Ten", "Ten Summoner's Tales", "The Soul Cages", "Yield"]
       the extra elements were:        ["A Love Supreme", "Live at Blues Alley", "Mysterioso", "One", "Portrait in Jazz", "Ten", "Yield"]
     # /tmp/d20130203-23049-1kz7j3n/spec.rb:74: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)>'

  7) Collection supports negation of filters
     Failure/Error: filtered.map(&:name).should eq ['Mad About You']
       
       expected: ["Mad About You"]
            got: ["Fields of Gold", "Mad About You", "Fields of Gold", "Autumn Leaves", "Autumn Leaves", "Brain of J.F.K", "Jeremy", "Come Away With Me", "Acknowledgment", "Ruby, My Dear"]
       
       (compared using ==)
     # /tmp/d20130203-23049-1kz7j3n/spec.rb:79: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)>'

  8) Collection can be adjoined with another collection
     Failure/Error: adjoined.names.should =~ [
     NoMethodError:
       undefined method `names' for #<Array:0xa479564>
     # /tmp/d20130203-23049-1kz7j3n/spec.rb:88: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.02383 seconds
11 examples, 8 failures

Failed examples:

rspec /tmp/d20130203-23049-1kz7j3n/spec.rb:43 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-1kz7j3n/spec.rb:48 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-1kz7j3n/spec.rb:53 # Collection can be filtered by album
rspec /tmp/d20130203-23049-1kz7j3n/spec.rb:58 # Collection can return an empty result
rspec /tmp/d20130203-23049-1kz7j3n/spec.rb:63 # Collection supports a conjuction of filters
rspec /tmp/d20130203-23049-1kz7j3n/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-1kz7j3n/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-1kz7j3n/spec.rb:82 # Collection can be adjoined with another collection

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

Даяна обнови решението на 31.10.2012 16:56 (преди над 11 години)

+class Criteria
+ attr_accessor :name, :artist, :album, :criteria
+ def initialize(input_string='',where_to_input='')
+ @hash = Hash[:name, nil, :artist, nil, :album, nil]
+ @criteria = []
+ @hash[where_to_input] = input_string.dup if input_string != ''
+ @criteria << @hash if input_string != ''
+ end
+
+ def Criteria.name(def_name)
+ Criteria.new(def_name,:name)
+ end
+
+ def Criteria.artist(def_artist)
+ Criteria.new(def_artist,:artist)
+ end
+
+ def Criteria.album(def_album)
+ Criteria.new(def_album,:album)
+ end
+
+ def |(other)
+ @criteria << other.criteria[0]
+ self
+ end
+
+ def !
+ self
+ end
+
+ def &(other)
+ self
+ end
+end
+
+class Song
+ attr_accessor :name, :artist, :album
+ def initialize(array_songs)
+ @name, @artist, @album = array_songs[0], array_songs[1], array_songs[2]
+ end
+
+ def matches?(criteria, songs_array)
+ criteria.criteria.map { |song| check_if_any(song, songs_array) }
+ songs_array.uniq
+ end
+
+ def check_if_any(song, songs_array)
+ song.any? { |type, value| check_by_type(type, value, songs_array) }
+ end
+
+ def check_by_type(type, value, songs_array)
+ case type
+ when :name then check_by_value(name, value, songs_array)
+ when :artist then check_by_value(artist, value, songs_array)
+ when :album then check_by_value(album, value, songs_array)
+ end
+ end
+
+ def check_by_value(type, value, songs_array)
+ songs_array << "#{name}, #{artist}, #{album}" if type == value
+ end
+end
+
+class Collection
+ include Enumerable
+ attr_accessor :songs, :songs_array
+ def initialize(text)
+ @songs = text.split("\n").select { |n| n.length != 0 }
+ @songs = @songs.each_slice(3).to_a
+ @songs.each_index { |index| @songs[index] = Song.new( @songs[index]) }
+ @songs_array = []
+ end
+
+ def Collection.parse(text)
+ self.new(text)
+ end
+
+ def artists
+ artists = []
+ @songs.map { |text| artists << text.artist }
+ artists.uniq
+ end
+
+ def albums
+ albums = []
+ @songs.map { |text| albums << text.album }
+ albums.uniq
+ end
+
+ def names
+ names = []
+ @songs.map { |text| names << text.name}
+ names.uniq
+ end
+
+ def filter(criteria)
+ @songs.map { |song| song.matches?(criteria, songs_array) }
+ p @songs_array
+ self
+ end
+
+ def adjoin(criteria)
+ @songs_array
+ end
+
+ def each
+ @songs.each { |result| yield result }
+ end
+end