Решение на Втора задача от Чанита Иванова

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

Към профила на Чанита Иванова

Резултати

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

Код

class Song
attr_accessor :song
def initialize(array = [])
@song = array
end
def album
song[2]
end
def name
song[0]
end
def artist
song[1]
end
def +(other)
if(song == [] || other.song == []) then Song.new()
else Song.new([name + other.name, artist + other.artist,
album + other.album])
end
end
def copy_collection(songs,result)
songs.map{|x| result << x }
end
def add_sub(songs,result,mode)
copy_collection(songs,result) if (result == [])
result |= [] if(mode == 1 and (result.delete_if{|x| (x != self)}))
result |= [] if(mode == 2 and (result.delete_if{|x| (x == self)}))
end
def ==(other)
if(song[0] == other.name or song[0] == '' or other.name == '') and
(song[1] == other.artist or song[1] == '' or other.artist == '') and
(song[2] == other.album or song[2] == '' or other.album == '')
then true else false end
end
end
class Collection
include Enumerable
attr_accessor :songs
def initialize(text)
@songs = text.split("\n\n").map{ |x| Song.new(x.split("\n")) }
end
def Collection.parse(text)
Collection.new(text)
end
def each
current = 0
while current < songs.length
yield songs[current]
current += 1
end
end
def names
map{|x| x.name} | []
end
def artists
map{|x| x.artist} | []
end
def albums
map{|x| x.album} | []
end
def adjoin(other_collection)
result = Collection.new("")
result.songs = songs + (other_collection.songs)
result.songs |= []
result
end
def filter(criteria)
result=Collection.new("")
criteria.array.select{|x| x[1]}.map{|x| x[0].add_sub(songs,result.songs,1)}
criteria.array.reject{|x| x[1]}.map{|x| x[0].add_sub(songs,result.songs,2)}
result
end
end
class Criteria
attr_accessor :array
def initialize(array=[],tag = false)
@array =[[ Song.new(array),tag]]
end
def Criteria.name(name)
Criteria.new([name ,'',''],true)
end
def Criteria.album(album)
Criteria.new(['','',album],true)
end
def Criteria.artist(artist)
Criteria.new(['',artist,''],true)
end
def |(other)
result = Criteria.new()
result.array = array + other.array
result
end
def check_tags(song_1,song_2)
if(song_1[1] == song_2[1]) then [[song_1[0] + song_2[0],song_1[1]],
song_1,song_2]
elsif(!song_1[1]) then [[song_1[0] + song_2[0],false],song_2]
else [[song_1[0] + song_2[0],false],song_1]
end
end
def &(other)
result = Criteria.new()
array_product = array.product(other.array)
array_product.map{|x| result.array += check_tags(x[0],x[1])}
result
end
def !@
result = Criteria.new()
array.map{|x| result.array << [x[0],(!x[1])]}
result
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:    ["Ten Summoner's Tales"]
       the missing elements were:      ["Live at Blues Alley", "The Soul Cages"]
     # /tmp/d20130203-23049-17moerz/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.01052 seconds
11 examples, 1 failure

Failed examples:

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

История (3 версии и 2 коментара)

Чанита обнови решението на 27.10.2012 22:18 (преди около 12 години)

+module Perambulate
+ def is_element(element,array)
+ element if(!array.include?(element))
+ end
+ def simple_check(value_1,value_2)
+ (value_1 == []) ? (value_1 + value_2) : value_1
+ end
+ def reverse_criteria(result,criteria)
+ i=5
+ 0.upto(5).each do |x| result[x][1] = criteria.to_a[i][1]
+ i -=1
+ end
+ result
+ end
+ def get_needed(needed,collection)
+ result=[]
+ collection.map{ |x| x[needed] }.map{ |y| result << (is_element(y,result))}
+ result
+ end
+end
+module Filter_Help
+ def check_include(name,indexes,collection,i)
+ indexes.select{ |y| name == collection[y][i] }
+ end
+ def check_remove(name,indexes,collection,i)
+ indexes.select{ |y| name != collection[y][i] }
+ end
+ def add_songs(indexes,collection,names,i)
+ return indexes if(names == [])
+ result=[]
+ names.map{ |x| result = result + check_include(x,indexes,collection,i) }
+ result
+ end
+ def remove(indexes,collection,names,i)
+ return indexes if(names == [])
+ result=[]
+ names.map{ |x| result = result + check_remove(x,indexes,collection,i) }
+ result
+ end
+ def initialize_indexes(collection)
+ indexes=[]
+ 0.upto(collection.length-1).each do |x| indexes << x end
+ indexes
+ end
+ def switch_between(i,value,indexes,collection)
+ indexes = initialize_indexes(collection) if(indexes == [])
+ if(i<3)
+ add_songs(indexes,collection,value,i)
+ else
+ remove(indexes,collection,value,(i-5).abs)
+ end
+ end
+ def indexes(collection,criteria)
+ indexes, i = [] , 0
+ criteria.criteria.map{|key,value|
+ indexes , i = switch_between(i,value,indexes,collection) , (i +=1) }
+ indexes
+ end
+end
+class Song
+ attr_accessor :song
+ def initialize(array)
+ @song = []
+ array.map{|x| song << x}
+ end
+ def album
+ @song[2]
+ end
+ def name
+ @song[0]
+ end
+ def artist
+ @song[1]
+ end
+ def [](index)
+ @song[index]
+ end
+end
+class Collection
+ include Enumerable
+ include Perambulate
+ include Filter_Help
+ attr_accessor :collection
+ def initialize(text)
+ @collection = text.split("\n\n").map{ |x| Song.new(x.split("\n"))}
+ end
+ def Collection.parse(text)
+ Collection.new(text)
+ end
+ def names
+ get_needed(0,@collection).select{|y| (y)}
+ end
+ def artists
+ get_needed(1,@collection).select{|y| (y)}
+ end
+ def albums
+ get_needed(2,@collection).select{|y| (y)}
+ end
+ def each
+ current = 0
+ while current<@collection.length
+ yield @collection[current]
+ current +=1
+ end
+ end
+ def adjoin(other_collection)
+ result = Collection.new("")
+ result.collection = @collection + (other_collection.collection)
+ result
+ end
+ def filter(criteria)
+ result , indexes = Collection.new("") , indexes(@collection,criteria)
+ result.collection = @collection.select{ |x|
+ (indexes.include?(@collection.index(x)))}
+ result
+ end
+end
+class Criteria
+ include Perambulate
+ attr_accessor :criteria
+ def initialize()
+ @criteria = {name: [], artist: [], album: [],
+ not_album: [], not_artist: [], not_name: []}
+ end
+ def Criteria.name(name)
+ result = Criteria.new
+ result.criteria[:name] << name
+ result
+ end
+ def Criteria.album(album)
+ result = Criteria.new
+ result.criteria[:album] << album
+ result
+ end
+ def Criteria.artist(artist)
+ result = Criteria.new
+ result.criteria[:artist] << artist
+ result
+ end
+ def |(other)
+ result = Criteria.new()
+ @criteria.map{|key,value|
+ result.criteria[key] = @criteria[key] + (other.criteria[key])}
+ result
+ end
+ def &(other)
+ result = Criteria.new()
+ @criteria.map{|key,value|
+ result.criteria[key] = simple_check(value,other.criteria[key])}
+ result
+ end
+ def !@
+ result = Criteria.new
+ result.criteria = Hash[reverse_criteria(result.criteria.to_a,@criteria)]
+ result
+ end
+end

Чанита обнови решението на 27.10.2012 22:22 (преди около 12 години)

module Perambulate
def is_element(element,array)
element if(!array.include?(element))
end
def simple_check(value_1,value_2)
(value_1 == []) ? (value_1 + value_2) : value_1
end
def reverse_criteria(result,criteria)
i=5
0.upto(5).each do |x| result[x][1] = criteria.to_a[i][1]
i -=1
end
result
end
def get_needed(needed,collection)
result=[]
collection.map{ |x| x[needed] }.map{ |y| result << (is_element(y,result))}
result
end
end
-module Filter_Help
+module FilterHelp
def check_include(name,indexes,collection,i)
indexes.select{ |y| name == collection[y][i] }
end
def check_remove(name,indexes,collection,i)
indexes.select{ |y| name != collection[y][i] }
end
def add_songs(indexes,collection,names,i)
return indexes if(names == [])
result=[]
names.map{ |x| result = result + check_include(x,indexes,collection,i) }
result
end
def remove(indexes,collection,names,i)
return indexes if(names == [])
result=[]
names.map{ |x| result = result + check_remove(x,indexes,collection,i) }
result
end
def initialize_indexes(collection)
indexes=[]
0.upto(collection.length-1).each do |x| indexes << x end
indexes
end
def switch_between(i,value,indexes,collection)
indexes = initialize_indexes(collection) if(indexes == [])
if(i<3)
add_songs(indexes,collection,value,i)
else
remove(indexes,collection,value,(i-5).abs)
end
end
def indexes(collection,criteria)
indexes, i = [] , 0
criteria.criteria.map{|key,value|
indexes , i = switch_between(i,value,indexes,collection) , (i +=1) }
indexes
end
end
class Song
attr_accessor :song
def initialize(array)
@song = []
array.map{|x| song << x}
end
def album
@song[2]
end
def name
@song[0]
end
def artist
@song[1]
end
def [](index)
@song[index]
end
end
class Collection
include Enumerable
include Perambulate
include Filter_Help
attr_accessor :collection
def initialize(text)
@collection = text.split("\n\n").map{ |x| Song.new(x.split("\n"))}
end
def Collection.parse(text)
Collection.new(text)
end
def names
get_needed(0,@collection).select{|y| (y)}
end
def artists
get_needed(1,@collection).select{|y| (y)}
end
def albums
get_needed(2,@collection).select{|y| (y)}
end
def each
current = 0
while current<@collection.length
yield @collection[current]
current +=1
end
end
def adjoin(other_collection)
result = Collection.new("")
result.collection = @collection + (other_collection.collection)
result
end
def filter(criteria)
result , indexes = Collection.new("") , indexes(@collection,criteria)
result.collection = @collection.select{ |x|
(indexes.include?(@collection.index(x)))}
result
end
end
class Criteria
include Perambulate
attr_accessor :criteria
def initialize()
@criteria = {name: [], artist: [], album: [],
not_album: [], not_artist: [], not_name: []}
end
def Criteria.name(name)
result = Criteria.new
result.criteria[:name] << name
result
end
def Criteria.album(album)
result = Criteria.new
result.criteria[:album] << album
result
end
def Criteria.artist(artist)
result = Criteria.new
result.criteria[:artist] << artist
result
end
def |(other)
result = Criteria.new()
@criteria.map{|key,value|
result.criteria[key] = @criteria[key] + (other.criteria[key])}
result
end
def &(other)
result = Criteria.new()
@criteria.map{|key,value|
result.criteria[key] = simple_check(value,other.criteria[key])}
result
end
def !@
result = Criteria.new
result.criteria = Hash[reverse_criteria(result.criteria.to_a,@criteria)]
result
end
end
  • Не спазваш конвенции на места. Намери ги сама.
  • Peramutable не е дума.
  • Метода check_simple е лош. Защо се казва така? Защо параметрите му са value_1 и value_2? Не ми става ясно какво прави докато го чета.
  • 10 ред не спазва конвенции.
  • Двата модула добавени в Collection са лош дизайн. Можеш да минеш без тях. Трябват ти три класа и всеки от тях има по-малко от 10 методи. Всичко друго и излишно и има начини да го премахнеш. Потърси ги.

Чанита обнови решението на 31.10.2012 02:52 (преди около 12 години)

-module Perambulate
- def is_element(element,array)
- element if(!array.include?(element))
- end
- def simple_check(value_1,value_2)
- (value_1 == []) ? (value_1 + value_2) : value_1
- end
- def reverse_criteria(result,criteria)
- i=5
- 0.upto(5).each do |x| result[x][1] = criteria.to_a[i][1]
- i -=1
- end
- result
- end
- def get_needed(needed,collection)
- result=[]
- collection.map{ |x| x[needed] }.map{ |y| result << (is_element(y,result))}
- result
- end
-end
-module FilterHelp
- def check_include(name,indexes,collection,i)
- indexes.select{ |y| name == collection[y][i] }
- end
- def check_remove(name,indexes,collection,i)
- indexes.select{ |y| name != collection[y][i] }
- end
- def add_songs(indexes,collection,names,i)
- return indexes if(names == [])
- result=[]
- names.map{ |x| result = result + check_include(x,indexes,collection,i) }
- result
- end
- def remove(indexes,collection,names,i)
- return indexes if(names == [])
- result=[]
- names.map{ |x| result = result + check_remove(x,indexes,collection,i) }
- result
- end
- def initialize_indexes(collection)
- indexes=[]
- 0.upto(collection.length-1).each do |x| indexes << x end
- indexes
- end
- def switch_between(i,value,indexes,collection)
- indexes = initialize_indexes(collection) if(indexes == [])
- if(i<3)
- add_songs(indexes,collection,value,i)
- else
- remove(indexes,collection,value,(i-5).abs)
- end
- end
- def indexes(collection,criteria)
- indexes, i = [] , 0
- criteria.criteria.map{|key,value|
- indexes , i = switch_between(i,value,indexes,collection) , (i +=1) }
- indexes
- end
-end
class Song
attr_accessor :song
- def initialize(array)
- @song = []
- array.map{|x| song << x}
+
+ def initialize(array = [])
+ @song = array
end
+
def album
- @song[2]
+ song[2]
end
+
def name
- @song[0]
+ song[0]
end
+
def artist
- @song[1]
+ song[1]
end
- def [](index)
- @song[index]
+
+ def +(other)
+ if(song == [] || other.song == []) then Song.new()
+ else Song.new([name + other.name, artist + other.artist,
+ album + other.album])
+ end
end
+
+ def copy_collection(songs,result)
+ songs.map{|x| result << x }
+ end
+
+ def add_sub(songs,result,mode)
+ copy_collection(songs,result) if (result == [])
+ result |= [] if(mode == 1 and (result.delete_if{|x| (x != self)}))
+ result |= [] if(mode == 2 and (result.delete_if{|x| (x == self)}))
+ end
+
+ def ==(other)
+ if(song[0] == other.name or song[0] == '' or other.name == '') and
+ (song[1] == other.artist or song[1] == '' or other.artist == '') and
+ (song[2] == other.album or song[2] == '' or other.album == '')
+ then true else false end
+ end
end
+
class Collection
include Enumerable
- include Perambulate
- include Filter_Help
- attr_accessor :collection
+
+ attr_accessor :songs
+
def initialize(text)
- @collection = text.split("\n\n").map{ |x| Song.new(x.split("\n"))}
+ @songs = text.split("\n\n").map{ |x| Song.new(x.split("\n")) }
end
+
def Collection.parse(text)
Collection.new(text)
end
+
+ def each
+ current = 0
+ while current < songs.length
+ yield songs[current]
+ current += 1
+ end
+ end
+
def names
- get_needed(0,@collection).select{|y| (y)}
+ map{|x| x.name} | []
end
+
def artists
- get_needed(1,@collection).select{|y| (y)}
+ map{|x| x.artist} | []
end
+
def albums
- get_needed(2,@collection).select{|y| (y)}
+ map{|x| x.album} | []
end
- def each
- current = 0
- while current<@collection.length
- yield @collection[current]
- current +=1
- end
- end
+
def adjoin(other_collection)
result = Collection.new("")
- result.collection = @collection + (other_collection.collection)
+ result.songs = songs + (other_collection.songs)
+ result.songs |= []
result
end
+
def filter(criteria)
- result , indexes = Collection.new("") , indexes(@collection,criteria)
- result.collection = @collection.select{ |x|
- (indexes.include?(@collection.index(x)))}
+ result=Collection.new("")
+ criteria.array.select{|x| x[1]}.map{|x| x[0].add_sub(songs,result.songs,1)}
+ criteria.array.reject{|x| x[1]}.map{|x| x[0].add_sub(songs,result.songs,2)}
result
end
+
end
+
class Criteria
- include Perambulate
- attr_accessor :criteria
- def initialize()
- @criteria = {name: [], artist: [], album: [],
- not_album: [], not_artist: [], not_name: []}
+ attr_accessor :array
+
+ def initialize(array=[],tag = false)
+ @array =[[ Song.new(array),tag]]
end
+
def Criteria.name(name)
- result = Criteria.new
- result.criteria[:name] << name
- result
+ Criteria.new([name ,'',''],true)
end
+
def Criteria.album(album)
- result = Criteria.new
- result.criteria[:album] << album
- result
+ Criteria.new(['','',album],true)
end
+
def Criteria.artist(artist)
- result = Criteria.new
- result.criteria[:artist] << artist
- result
+ Criteria.new(['',artist,''],true)
end
+
def |(other)
result = Criteria.new()
- @criteria.map{|key,value|
- result.criteria[key] = @criteria[key] + (other.criteria[key])}
+ result.array = array + other.array
result
end
+
+ def check_tags(song_1,song_2)
+ if(song_1[1] == song_2[1]) then [[song_1[0] + song_2[0],song_1[1]],
+ song_1,song_2]
+ elsif(!song_1[1]) then [[song_1[0] + song_2[0],false],song_2]
+ else [[song_1[0] + song_2[0],false],song_1]
+ end
+ end
+
def &(other)
result = Criteria.new()
- @criteria.map{|key,value|
- result.criteria[key] = simple_check(value,other.criteria[key])}
+ array_product = array.product(other.array)
+ array_product.map{|x| result.array += check_tags(x[0],x[1])}
result
end
+
def !@
- result = Criteria.new
- result.criteria = Hash[reverse_criteria(result.criteria.to_a,@criteria)]
+ result = Criteria.new()
+ array.map{|x| result.array << [x[0],(!x[1])]}
result
end
+
end