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

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

Към профила на Дамяна Иванова

Резултати

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

Код

class Song
attr_accessor :name, :artist, :album
def initialize(arr = [])
@name = arr[0] || ""
@artist = arr[1] || ""
@album = arr[2] || ""
end
def part_eq(other)
((name == other.name and name != "") or
(artist == other.artist and artist != "") or
(album == other.album and album != ""))
end
def part_compare(other)
if not part_eq(other) then
return false
else
return true
end
end
def in_a(arr)
return true if arr == []
result = arr
res = result.map { |x| part_compare(x) }.select { |x| x == true}
arr.length == res.length
end
def eq_one_compare(other)
if other.is_a? Array then
return in_a(other)
else
return part_eq(other)
end
end
def eq_one(arr)
return true if arr == []
result = arr
res = result.map { |x| eq_one_compare(x) }.select { |x| x == true}
res.length == 1
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(arr = [])
self.songs = arr
end
def each
i = 0
while i < songs.length
yield songs[i]
i += 1
end
end
def Collection.parse(text)
songs = []
text.each_line("\n\n") { |x| songs.push Song.new(x.split("\n")) }
Collection.new(songs)
end
def names
names = []
each { |x| names.push x.name }
names.uniq
end
def artists
artists = []
each { |x| artists.push x.artist }
artists.uniq
end
def albums
albums = []
each { |x| albums.push x.album }
albums.uniq
end
def filter (criteria)
result = select do |x|
x.in_a criteria.all and x.eq_one criteria.one and
(criteria.none == [] or (not x.in_a(criteria.none)))
end
Collection.new(result)
end
def adjoin(other)
new_songs = other.songs
songs.each do |x|
new_songs << x
end
Collection.new(new_songs)
end
end
class Criteria
attr_accessor :one, :all, :none
def initialize(all = [], one = [], none = [])
@one = one
@all = all
@none = none
end
def Criteria.name(name)
Criteria.new([Song.new([name])])
end
def Criteria.artist (artist)
Criteria.new([Song.new(["", artist])])
end
def Criteria.album (album)
Criteria.new([Song.new(["", "", album])])
end
def &(other)
other.all.each { |x| all << x }
other.none.each { |x| none << x }
Criteria.new(all, [], none)
end
def |(other)
other.all.each { |x| one << x }
one << all if all != []
Criteria.new([], one, [])
end
def !
all.each { |x| none << x }
Criteria.new([], [], none)
end
end

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

........F..

Failures:

  1) 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:    ["Live at Blues Alley", "The Soul Cages"]
       the missing elements were:      ["Ten Summoner's Tales"]
     # /tmp/d20130203-23049-158yhix/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)>'

Finished in 0.01094 seconds
11 examples, 1 failure

Failed examples:

rspec /tmp/d20130203-23049-158yhix/spec.rb:68 # Collection supports a disjunction of filters

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

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

+class Song
+ attr_accessor :name, :artist, :album
+
+ def initialize(arr = [])
+ @name = arr[0] || ""
+ @artist = arr[1] || ""
+ @album = arr[2] || ""
+ end
+
+ def part_eq(other)
+ ((name == other.name and name != "") or
+ (artist == other.artist and artist != "") or
+ (album == other.album and album != ""))
+ end
+
+ def part_compare(other)
+ if not part_eq(other) then
+ return false
+ else
+ return true
+ end
+ end
+
+ def in_a(arr)
+ return true if arr == []
+ result = arr
+ res = result.map { |x| part_compare(x) }.take_while { |x| x == true}
+ arr.length == res.length
+ end
+
+ def eq_one_compare(other)
+ if other.is_a? Array then
+ return in_a(x)
+ else
+ return part_eq(x)
+ end
+ end
+
+ def eq_one(arr)
+ return true if arr == []
+ result = arr
+ res = result.map { |x| eq_one_compare(x) }.take_while { |x| x == false}
+ res == 1
+ end
+end
+
+class Collection
+ include Enumerable
+ attr_accessor :songs
+
+ def initialize(arr = [])
+ self.songs = arr
+ end
+
+ def each
+ i = 0
+ while i < songs.length
+ yield songs[i]
+ i += 1
+ end
+ end
+
+ def Collection.parse(text)
+ songs = []
+ text.each_line("\n\n") { |x| songs.push Song.new(x.split("\n")) }
+ Collection.new(songs)
+ end
+
+ def names
+ names = []
+ each { |x| names.push x.name }
+ names.uniq
+ end
+
+ def artists
+ artists = []
+ each { |x| artists.push x.artist }
+ artists.uniq
+ end
+
+ def albums
+ albums = []
+ each { |x| albums.push x.album }
+ albums.uniq
+ end
+
+ def filter (criteria)
+ result = select do |x|
+ x.in_a criteria.all and x.eq_one criteria.one and
+ (criteria.none == [] or (not x.in_a(criteria.none)))
+ end
+ Collection.new(result)
+ end
+
+ def adjoin(other)
+ new_songs = other.songs
+ songs.each do |x|
+ new_songs << x
+ end
+ Collection.new(new_songs)
+ end
+end
+
+class Criteria
+ attr_accessor :one, :all, :none
+ def initialize(all = [], one = [], none = [])
+ @one = one
+ @all = all
+ @none = none
+ end
+
+ def Criteria.name(name)
+ Criteria.new([Song.new([name])])
+ end
+
+ def Criteria.artist (artist)
+ Criteria.new([Song.new(["", artist])])
+ end
+
+ def Criteria.album (album)
+ Criteria.new([Song.new(["", "", album])])
+ end
+
+ def &(other)
+ other.all.each { |x| all << x }
+ other.none.each { |x| none << x }
+ Criteria.new(all, [], none)
+ end
+
+ def |(other)
+ other.all.each { |x| one << x }
+ one << all if all != []
+ Criteria.new([], one, [])
+ end
+
+ def !
+ all.each { |x| none << x }
+ Criteria.new([], [], none)
+ end
+end

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

class Song
attr_accessor :name, :artist, :album
def initialize(arr = [])
@name = arr[0] || ""
@artist = arr[1] || ""
@album = arr[2] || ""
end
def part_eq(other)
((name == other.name and name != "") or
(artist == other.artist and artist != "") or
(album == other.album and album != ""))
end
def part_compare(other)
if not part_eq(other) then
return false
else
return true
end
end
def in_a(arr)
return true if arr == []
result = arr
res = result.map { |x| part_compare(x) }.take_while { |x| x == true}
arr.length == res.length
end
def eq_one_compare(other)
if other.is_a? Array then
- return in_a(x)
+ return in_a(other)
else
- return part_eq(x)
+ return part_eq(other)
end
end
def eq_one(arr)
return true if arr == []
result = arr
res = result.map { |x| eq_one_compare(x) }.take_while { |x| x == false}
res == 1
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(arr = [])
self.songs = arr
end
def each
i = 0
while i < songs.length
yield songs[i]
i += 1
end
end
def Collection.parse(text)
songs = []
text.each_line("\n\n") { |x| songs.push Song.new(x.split("\n")) }
Collection.new(songs)
end
def names
names = []
each { |x| names.push x.name }
names.uniq
end
def artists
artists = []
each { |x| artists.push x.artist }
artists.uniq
end
def albums
albums = []
each { |x| albums.push x.album }
albums.uniq
end
def filter (criteria)
result = select do |x|
x.in_a criteria.all and x.eq_one criteria.one and
(criteria.none == [] or (not x.in_a(criteria.none)))
end
Collection.new(result)
end
def adjoin(other)
new_songs = other.songs
songs.each do |x|
new_songs << x
end
Collection.new(new_songs)
end
end
class Criteria
attr_accessor :one, :all, :none
def initialize(all = [], one = [], none = [])
@one = one
@all = all
@none = none
end
def Criteria.name(name)
Criteria.new([Song.new([name])])
end
def Criteria.artist (artist)
Criteria.new([Song.new(["", artist])])
end
def Criteria.album (album)
Criteria.new([Song.new(["", "", album])])
end
def &(other)
other.all.each { |x| all << x }
other.none.each { |x| none << x }
Criteria.new(all, [], none)
end
def |(other)
other.all.each { |x| one << x }
one << all if all != []
Criteria.new([], one, [])
end
def !
all.each { |x| none << x }
Criteria.new([], [], none)
end
end

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

class Song
attr_accessor :name, :artist, :album
def initialize(arr = [])
@name = arr[0] || ""
@artist = arr[1] || ""
@album = arr[2] || ""
end
def part_eq(other)
((name == other.name and name != "") or
(artist == other.artist and artist != "") or
(album == other.album and album != ""))
end
def part_compare(other)
if not part_eq(other) then
return false
else
return true
end
end
def in_a(arr)
return true if arr == []
result = arr
res = result.map { |x| part_compare(x) }.take_while { |x| x == true}
arr.length == res.length
end
def eq_one_compare(other)
if other.is_a? Array then
return in_a(other)
else
return part_eq(other)
end
end
def eq_one(arr)
return true if arr == []
result = arr
res = result.map { |x| eq_one_compare(x) }.take_while { |x| x == false}
- res == 1
+ res.length == 1
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(arr = [])
self.songs = arr
end
def each
i = 0
while i < songs.length
yield songs[i]
i += 1
end
end
def Collection.parse(text)
songs = []
text.each_line("\n\n") { |x| songs.push Song.new(x.split("\n")) }
Collection.new(songs)
end
def names
names = []
each { |x| names.push x.name }
names.uniq
end
def artists
artists = []
each { |x| artists.push x.artist }
artists.uniq
end
def albums
albums = []
each { |x| albums.push x.album }
albums.uniq
end
def filter (criteria)
result = select do |x|
x.in_a criteria.all and x.eq_one criteria.one and
(criteria.none == [] or (not x.in_a(criteria.none)))
end
Collection.new(result)
end
def adjoin(other)
new_songs = other.songs
songs.each do |x|
new_songs << x
end
Collection.new(new_songs)
end
end
class Criteria
attr_accessor :one, :all, :none
def initialize(all = [], one = [], none = [])
@one = one
@all = all
@none = none
end
def Criteria.name(name)
Criteria.new([Song.new([name])])
end
def Criteria.artist (artist)
Criteria.new([Song.new(["", artist])])
end
def Criteria.album (album)
Criteria.new([Song.new(["", "", album])])
end
def &(other)
other.all.each { |x| all << x }
other.none.each { |x| none << x }
Criteria.new(all, [], none)
end
def |(other)
other.all.each { |x| one << x }
one << all if all != []
Criteria.new([], one, [])
end
def !
all.each { |x| none << x }
Criteria.new([], [], none)
end
end

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

class Song
attr_accessor :name, :artist, :album
def initialize(arr = [])
@name = arr[0] || ""
@artist = arr[1] || ""
@album = arr[2] || ""
end
def part_eq(other)
((name == other.name and name != "") or
(artist == other.artist and artist != "") or
(album == other.album and album != ""))
end
def part_compare(other)
if not part_eq(other) then
return false
else
return true
end
end
def in_a(arr)
return true if arr == []
result = arr
res = result.map { |x| part_compare(x) }.take_while { |x| x == true}
arr.length == res.length
end
def eq_one_compare(other)
if other.is_a? Array then
return in_a(other)
else
return part_eq(other)
end
end
def eq_one(arr)
return true if arr == []
result = arr
- res = result.map { |x| eq_one_compare(x) }.take_while { |x| x == false}
+ res = result.map { |x| eq_one_compare(x) }.select { |x| x == true}
res.length == 1
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(arr = [])
self.songs = arr
end
def each
i = 0
while i < songs.length
yield songs[i]
i += 1
end
end
def Collection.parse(text)
songs = []
text.each_line("\n\n") { |x| songs.push Song.new(x.split("\n")) }
Collection.new(songs)
end
def names
names = []
each { |x| names.push x.name }
names.uniq
end
def artists
artists = []
each { |x| artists.push x.artist }
artists.uniq
end
def albums
albums = []
each { |x| albums.push x.album }
albums.uniq
end
def filter (criteria)
result = select do |x|
x.in_a criteria.all and x.eq_one criteria.one and
(criteria.none == [] or (not x.in_a(criteria.none)))
end
Collection.new(result)
end
def adjoin(other)
new_songs = other.songs
songs.each do |x|
new_songs << x
end
Collection.new(new_songs)
end
end
class Criteria
attr_accessor :one, :all, :none
def initialize(all = [], one = [], none = [])
@one = one
@all = all
@none = none
end
def Criteria.name(name)
Criteria.new([Song.new([name])])
end
def Criteria.artist (artist)
Criteria.new([Song.new(["", artist])])
end
def Criteria.album (album)
Criteria.new([Song.new(["", "", album])])
end
def &(other)
other.all.each { |x| all << x }
other.none.each { |x| none << x }
Criteria.new(all, [], none)
end
def |(other)
other.all.each { |x| one << x }
one << all if all != []
Criteria.new([], one, [])
end
def !
all.each { |x| none << x }
Criteria.new([], [], none)
end
end

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

class Song
attr_accessor :name, :artist, :album
def initialize(arr = [])
@name = arr[0] || ""
@artist = arr[1] || ""
@album = arr[2] || ""
end
def part_eq(other)
((name == other.name and name != "") or
(artist == other.artist and artist != "") or
(album == other.album and album != ""))
end
def part_compare(other)
if not part_eq(other) then
return false
else
return true
end
end
def in_a(arr)
return true if arr == []
result = arr
- res = result.map { |x| part_compare(x) }.take_while { |x| x == true}
+ res = result.map { |x| part_compare(x) }.select { |x| x == true}
arr.length == res.length
end
def eq_one_compare(other)
if other.is_a? Array then
return in_a(other)
else
return part_eq(other)
end
end
def eq_one(arr)
return true if arr == []
result = arr
res = result.map { |x| eq_one_compare(x) }.select { |x| x == true}
res.length == 1
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(arr = [])
self.songs = arr
end
def each
i = 0
while i < songs.length
yield songs[i]
i += 1
end
end
def Collection.parse(text)
songs = []
text.each_line("\n\n") { |x| songs.push Song.new(x.split("\n")) }
Collection.new(songs)
end
def names
names = []
each { |x| names.push x.name }
names.uniq
end
def artists
artists = []
each { |x| artists.push x.artist }
artists.uniq
end
def albums
albums = []
each { |x| albums.push x.album }
albums.uniq
end
def filter (criteria)
result = select do |x|
x.in_a criteria.all and x.eq_one criteria.one and
(criteria.none == [] or (not x.in_a(criteria.none)))
end
Collection.new(result)
end
def adjoin(other)
new_songs = other.songs
songs.each do |x|
new_songs << x
end
Collection.new(new_songs)
end
end
class Criteria
attr_accessor :one, :all, :none
def initialize(all = [], one = [], none = [])
@one = one
@all = all
@none = none
end
def Criteria.name(name)
Criteria.new([Song.new([name])])
end
def Criteria.artist (artist)
Criteria.new([Song.new(["", artist])])
end
def Criteria.album (album)
Criteria.new([Song.new(["", "", album])])
end
def &(other)
other.all.each { |x| all << x }
other.none.each { |x| none << x }
Criteria.new(all, [], none)
end
def |(other)
other.all.each { |x| one << x }
one << all if all != []
Criteria.new([], one, [])
end
def !
all.each { |x| none << x }
Criteria.new([], [], none)
end
end