Решение на Втора задача от Никола Генешки

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

Към профила на Никола Генешки

Резултати

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

Код

require 'enumerator'
class Collection
include Enumerable
def each(&block)
@songs.each block
end
def Collection.parse(text)
songs = []
text.split("\n").each_slice(4) { |song| songs << Song.new(song.first(3)) }
songs_collection = Collection.new
songs_collection.songs = songs
end
attr_accessor :songs
def artists
@songs.uniq { |song| song.artist }
end
def albums
@songs.uniq { |song| song.album }
end
def names
@songs.uniq { |song| song.name }
end
def filter(criteria)
filtered_songs = select { |song| song.match criteria }
Collection.new filtered_songs
end
def adjoin(collection)
songs += collection.songs
end
end
class Song
def initialize(song_info)
@name = song_info[0]
@artist = song_info[1]
@album = song_info[2]
end
def match(criteria)
if match_conjunction(criteria) and match_disjunction(criteria)
return !match_disallowed(criteria)
end
false
end
attr_reader :name, :artist, :album
private
def match_conjunction(criteria)
matches = check_all criteria.conjugated
matches.size == 0 || !matches.include?(false)
end
def match_disjunction(criteria)
matches = check_all criteria.disjugated
matches.size == 0 || matches.include?(true)
end
def match_disallowed(criteria)
matches = check_all criteria.disallowed
matches.size == 0 || matches.include?(true)
end
def check_all(rules)
answers = []
rules.each { |key, value| answers << value.include?(send(key)) }
answers
end
end
class Criteria
def initialize()
@conjugated = Hash.new { |hash, key| hash[key] = [] }
@disjugated = Hash.new { |hash, key| hash[key] = [] }
@disallowed = Hash.new { |hash, key| hash[key] = [] }
end
def Criteria.name(song_name)
name_criteria = Criteria.new
name_criteria.disjugated.store :name, song_name
name_criteria
end
def Criteria.artist(artist_name)
artist_criteria = Criteria.new
artist_criteria.disjugated.store :artist, artist_name
artist_criteria
end
def Criteria.album(album_name)
album_criteria = Criteria.new
album_criteria.disjugated.store :album, album_name
album_criteria
end
attr_accessor :conjugated, :disjugated, :disallowed
def !
reversed_criteria = Criteria.new
reversed_criteria.disallowed = @disjugated
reversed_criteria
end
def |(other)
other.disjugated.each do |key, value|
@disjugated.fetch(key).merge! value
end
self
end
def &(other)
other.disjugated.each { |key, value| @conjugated.fetch(key).merge! value }
other.disallowed.each { |key, value| @disallowed.fetch(key).merge! value }
self
end
end

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

FFFFFFFFFFF

Failures:

  1) Collection can find all the artists in the collection
     Failure/Error: collection.artists.should =~ [
     NoMethodError:
       undefined method `artists' for #<Array:0x9db1b78>
     # /tmp/d20130203-23049-hzr3w7/spec.rb:5: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 all the names of all songs in the collection
     Failure/Error: collection.names.should =~ [
     NoMethodError:
       undefined method `names' for #<Array:0x9dc860c>
     # /tmp/d20130203-23049-hzr3w7/spec.rb:17: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 find all the albums in the collection
     Failure/Error: collection.albums.should =~ [
     NoMethodError:
       undefined method `albums' for #<Array:0x9dcf600>
     # /tmp/d20130203-23049-hzr3w7/spec.rb:30: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 be filtered by song name
     Failure/Error: filtered = collection.filter Criteria.name('Fields of Gold')
     NoMethodError:
       undefined method `filter' for #<Array:0x9dd92cc>
     # /tmp/d20130203-23049-hzr3w7/spec.rb:44: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 can be filtered by song name
     Failure/Error: filtered = collection.filter Criteria.artist('Sting')
     NoMethodError:
       undefined method `filter' for #<Array:0x9ddaab4>
     # /tmp/d20130203-23049-hzr3w7/spec.rb:49: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 filtered by album
     Failure/Error: filtered = collection.filter Criteria.album('Live at Blues Alley')
     NoMethodError:
       undefined method `filter' for #<Array:0x9de7250>
     # /tmp/d20130203-23049-hzr3w7/spec.rb:54: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 can return an empty result
     Failure/Error: filtered = collection.filter Criteria.album('The Dark Side of the Moon')
     NoMethodError:
       undefined method `filter' for #<Array:0x9dff710>
     # /tmp/d20130203-23049-hzr3w7/spec.rb:59: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 supports a conjuction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & Criteria.name('Fields of Gold')
     KeyError:
       key not found: :name
     # /tmp/d20130203-23049-hzr3w7/solution.rb:125:in `fetch'
     # /tmp/d20130203-23049-hzr3w7/solution.rb:125:in `block in &'
     # /tmp/d20130203-23049-hzr3w7/solution.rb:125:in `each'
     # /tmp/d20130203-23049-hzr3w7/solution.rb:125:in `&'
     # /tmp/d20130203-23049-hzr3w7/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)>'

  9) Collection supports a disjunction of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') | Criteria.name('Fields of Gold')
     KeyError:
       key not found: :name
     # /tmp/d20130203-23049-hzr3w7/solution.rb:119:in `fetch'
     # /tmp/d20130203-23049-hzr3w7/solution.rb:119:in `block in |'
     # /tmp/d20130203-23049-hzr3w7/solution.rb:118:in `each'
     # /tmp/d20130203-23049-hzr3w7/solution.rb:118:in `|'
     # /tmp/d20130203-23049-hzr3w7/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)>'

  10) Collection supports negation of filters
     Failure/Error: filtered = collection.filter Criteria.artist('Sting') & !Criteria.name('Fields of Gold')
     KeyError:
       key not found: :name
     # /tmp/d20130203-23049-hzr3w7/solution.rb:126:in `fetch'
     # /tmp/d20130203-23049-hzr3w7/solution.rb:126:in `block in &'
     # /tmp/d20130203-23049-hzr3w7/solution.rb:126:in `each'
     # /tmp/d20130203-23049-hzr3w7/solution.rb:126:in `&'
     # /tmp/d20130203-23049-hzr3w7/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)>'

  11) Collection can be adjoined with another collection
     Failure/Error: sting    = collection.filter Criteria.artist('Sting')
     NoMethodError:
       undefined method `filter' for #<Array:0x9e1b3d4>
     # /tmp/d20130203-23049-hzr3w7/spec.rb:83: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.00869 seconds
11 examples, 11 failures

Failed examples:

rspec /tmp/d20130203-23049-hzr3w7/spec.rb:4 # Collection can find all the artists in the collection
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:16 # Collection can find all the names of all songs in the collection
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:29 # Collection can find all the albums in the collection
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:43 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:48 # Collection can be filtered by song name
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:53 # Collection can be filtered by album
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:58 # Collection can return an empty result
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:63 # Collection supports a conjuction of filters
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:68 # Collection supports a disjunction of filters
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:77 # Collection supports negation of filters
rspec /tmp/d20130203-23049-hzr3w7/spec.rb:82 # Collection can be adjoined with another collection

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

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

+require 'enumerator'
+
+class Collection
+
+ include Enumerable
+
+ def each(&block)
+ @songs.each block
+ end
+
+ def Collection.parse(text)
+ songs = []
+ text.split("\n").each_slice(4) { |song| songs << Song.new(song.first(3)) }
+ songs_collection = Collection.new
+ songs_collection.songs = songs
+ end
+
+ attr_accessor :songs
+
+ def artists
+ @songs.uniq { |song| song.artist }
+ end
+
+ def albums
+ @songs.uniq { |song| song.album }
+ end
+
+ def names
+ @songs.uniq { |song| song.name }
+ end
+
+ def filter(criteria)
+ filtered_songs = select { |song| song.match criteria }
+ Collection.new filtered_songs
+ end
+
+ def adjoin(collection)
+ songs += collection.songs
+ end
+
+end
+
+class Song
+ def initialize(song_info)
+ @name = song_info[0]
+ @artist = song_info[1]
+ @album = song_info[2]
+ end
+
+ def match(criteria)
+ if match_conjunction(criteria) and match_disjunction(criteria)
+ return !match_disallowed(criteria)
+ end
+ false
+ end
+
+ attr_reader :name, :artist, :album
+
+ private
+
+ def match_conjunction(criteria)
+ matches = check_all criteria.conjugated
+ matches.size == 0 || !matches.include?(false)
+ end
+
+ def match_disjunction(criteria)
+ matches = check_all criteria.disjugated
+ matches.size == 0 || matches.include?(true)
+ end
+
+ def match_disallowed(criteria)
+ matches = check_all criteria.disallowed
+ matches.size == 0 || matches.include?(true)
+ end
+
+ def check_all(rules)
+ answers = []
+ rules.each { |key, value| answers << value.include?(send(key)) }
+ answers
+ end
+end
+
+class Criteria
+
+ def initialize()
+ @conjugated = Hash.new { |hash, key| hash[key] = [] }
+ @disjugated = Hash.new { |hash, key| hash[key] = [] }
+ @disallowed = Hash.new { |hash, key| hash[key] = [] }
+ end
+
+ def Criteria.name(song_name)
+ name_criteria = Criteria.new
+ name_criteria.disjugated.store :name, song_name
+ name_criteria
+ end
+
+ def Criteria.artist(artist_name)
+ artist_criteria = Criteria.new
+ artist_criteria.disjugated.store :artist, artist_name
+ artist_criteria
+ end
+
+ def Criteria.album(album_name)
+ album_criteria = Criteria.new
+ album_criteria.disjugated.store :album, album_name
+ album_criteria
+ end
+
+ attr_accessor :conjugated, :disjugated, :disallowed
+
+ def !
+ reversed_criteria = Criteria.new
+ reversed_criteria.disallowed = @disjugated
+ reversed_criteria
+ end
+
+ def |(other)
+ other.disjugated.each do |key, value|
+ @disjugated.fetch(key).merge! value
+ end
+ self
+ end
+
+ def &(other)
+ other.disjugated.each { |key, value| @conjugated.fetch(key).merge! value }
+ other.disallowed.each { |key, value| @disallowed.fetch(key).merge! value }
+ self
+ end
+
+end