Решение на Трета задача от Бисер Кръстев

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

Към профила на Бисер Кръстев

Резултати

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

Код

module RBFS
class File
attr_accessor :data
attr_accessor :data_type
def initialize(data = nil)
@data = data
if data.is_a? Fixnum then @data_type = :number
elsif data.is_a? Symbol then @data_type = :symbol
elsif data.is_a? NilClass then @data_type = :nil
elsif data.is_a? String then @data_type = :string
else @data_type = :boolean end
end
def serialize()
"#{@data_type}:#{@data}"
end
def self.parse(string_data)
index = string_data.index(':')
file = File.new nil
file.data_type = string_data[0...index].to_sym
file.data = string_data[(index+1)..-1]
file
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize()
@files = {}
@directories = {}
end
def add_file(name, file)
if !@files.has_key? name then
@files[name] = file
end
end
def add_directory(name, directory = Directory.new)
if !@directories.has_key? name then
@directories[name] = directory
end
end
def [](name)
return @directories[name] if @directories.has_key? name
@files[name]
end
def serialize()
result = "#{@files.count}:"
@files.each do |name, item|
result += "#{name}:#{item.serialize.length}:#{item.serialize}"
end
result += "#{@directories.count}:"
@directories.each do |name, item|
result += "#{name}:#{item.serialize.length}:#{item.serialize}"
end
result
end
def self.parse(data)
dir = Directory.new
#read all files
index, dir = read_data(data, 0, dir, RBFS::File) do |dir, name, type, data|
dir.add_file(name, type::parse(data))
dir
end
#read all subdirectories
index, dir = read_data(data, index, dir, RBFS::Directory) do |dir, name, type, data|
dir.add_directory(name, type::parse(data))
dir
end
#return the accumulated structure
dir
end
end
#read a chunk of data from the given string
#where current is the reading start position
#and fixed_length can be 0 to read until the next ':'
# or a positive value to read a fixed length of data
def Directory::read_chunk(data, current = 0, fixed_length = 0)
if fixed_length > 0 then
result = data[current ... current + fixed_length]
new = current + fixed_length
else
result = data[current ... data.index(':', current)]
new = data.index(':', current) + 1
end
return new, result
end
#parse a certain type of info from the data
#where current is the reading start position
#directory is the object to work on
#type is the class being added
#and method is the method used for adding the data
# the method needs 4 parameters:
# • directory to work on,
# • name of new object added,
# • class of the new object,
# • the data to be parsed
def Directory::read_data(data, current, directory, type, &method)
current, num_dirs = read_chunk(data, current)
num_dirs.to_i.times do
current, name = read_chunk(data, current)
current, len = read_chunk(data, current)
current, string_data = read_chunk(data, current, len.to_i)
directory = method.call(directory, name, type, string_data)
end
return current, directory
end
end

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

.............F..........FFF..FFFFFFFFFFFFFFF

Failures:

  1) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: expect(rbfs_directory['solution.rb'].data ).to eq :hidden
       
       expected: :hidden
            got: "hidden"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:hidden
       +"hidden"
     # /tmp/d20141111-26053-e14gnh/spec.rb:133:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) RBFS File data type nil can be parsed
     Failure/Error: expect(file.data     ).to eq nil
       
       expected: nil
            got: ""
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:231:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) RBFS File data type string can be detected
     Failure/Error: expect(file.data_type).to eq :string
       
       expected: :string
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:string
       +:nil
     # /tmp/d20141111-26053-e14gnh/spec.rb:240:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) RBFS File data type string can be serialized
     Failure/Error: expect(file.serialize).to eq 'string:Hi'
       
       expected: "string:Hi"
            got: "nil:Hi"
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:244:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) RBFS File data type symbol can be detected
     Failure/Error: expect(file.data_type).to eq :symbol
       
       expected: :symbol
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:symbol
       +:nil
     # /tmp/d20141111-26053-e14gnh/spec.rb:266:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) RBFS File data type symbol can be serialized
     Failure/Error: expect(file.serialize).to eq 'symbol:yo'
       
       expected: "symbol:yo"
            got: "nil:yo"
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:270:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) RBFS File data type symbol can be parsed
     Failure/Error: expect(file.data     ).to eq :hello
       
       expected: :hello
            got: "hello"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:hello
       +"hello"
     # /tmp/d20141111-26053-e14gnh/spec.rb:276:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) RBFS File data type number can be detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +:nil
     # /tmp/d20141111-26053-e14gnh/spec.rb:285:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) RBFS File data type number can be serialized
     Failure/Error: expect(file.serialize).to eq 'number:666'
       
       expected: "number:666"
            got: "nil:666"
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:289:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) RBFS File data type number can be parsed
     Failure/Error: expect(file.data     ).to eq 1234
       
       expected: 1234
            got: "1234"
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:295:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) RBFS File data type float number can be detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +:nil
     # /tmp/d20141111-26053-e14gnh/spec.rb:304:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) RBFS File data type float number can be serialized
     Failure/Error: expect(file.serialize).to eq 'number:666.6'
       
       expected: "number:666.6"
            got: "nil:666.6"
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:308:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) RBFS File data type float number can be parsed
     Failure/Error: expect(file.data     ).to eq 3.14
       
       expected: 3.14
            got: "3.14"
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:314:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) RBFS File data type boolean true can be detected
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +:nil
     # /tmp/d20141111-26053-e14gnh/spec.rb:323:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) RBFS File data type boolean true can be serialized
     Failure/Error: expect(file.serialize).to eq 'boolean:true'
       
       expected: "boolean:true"
            got: "nil:true"
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:328:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) RBFS File data type boolean true can be parsed
     Failure/Error: expect(file.data     ).to eq true
       
       expected: true
            got: "true"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -true
       +"true"
     # /tmp/d20141111-26053-e14gnh/spec.rb:334:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) RBFS File data type boolean false can be detected
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: :nil
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +:nil
     # /tmp/d20141111-26053-e14gnh/spec.rb:342:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) RBFS File data type boolean false can be serialized
     Failure/Error: expect(file.serialize).to eq 'boolean:false'
       
       expected: "boolean:false"
            got: "nil:false"
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:347:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) RBFS File data type boolean false can be parsed
     Failure/Error: expect(file.data     ).to eq false
       
       expected: false
            got: "false"
       
       (compared using ==)
     # /tmp/d20141111-26053-e14gnh/spec.rb:353:in `block (6 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.0483 seconds
44 examples, 19 failures

Failed examples:

rspec /tmp/d20141111-26053-e14gnh/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-e14gnh/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-e14gnh/spec.rb:239 # RBFS File data type string can be detected
rspec /tmp/d20141111-26053-e14gnh/spec.rb:243 # RBFS File data type string can be serialized
rspec /tmp/d20141111-26053-e14gnh/spec.rb:265 # RBFS File data type symbol can be detected
rspec /tmp/d20141111-26053-e14gnh/spec.rb:269 # RBFS File data type symbol can be serialized
rspec /tmp/d20141111-26053-e14gnh/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-e14gnh/spec.rb:284 # RBFS File data type number can be detected
rspec /tmp/d20141111-26053-e14gnh/spec.rb:288 # RBFS File data type number can be serialized
rspec /tmp/d20141111-26053-e14gnh/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-e14gnh/spec.rb:303 # RBFS File data type float number can be detected
rspec /tmp/d20141111-26053-e14gnh/spec.rb:307 # RBFS File data type float number can be serialized
rspec /tmp/d20141111-26053-e14gnh/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-e14gnh/spec.rb:321 # RBFS File data type boolean true can be detected
rspec /tmp/d20141111-26053-e14gnh/spec.rb:326 # RBFS File data type boolean true can be serialized
rspec /tmp/d20141111-26053-e14gnh/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-e14gnh/spec.rb:340 # RBFS File data type boolean false can be detected
rspec /tmp/d20141111-26053-e14gnh/spec.rb:345 # RBFS File data type boolean false can be serialized
rspec /tmp/d20141111-26053-e14gnh/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Бисер обнови решението на 08.11.2014 11:01 (преди около 10 години)

+module RBFS
+ class File
+ attr_accessor :data
+ attr_reader :data_type
+
+ def initialize(data = nil)
+ @data = data
+ if data.is_a? Fixnum then @data_type = :number
+ elsif data.is_a? Symbol then @data_type = :symbol
+ elsif data.is_a? NilClass then @data_type = :nil
+ elsif data.is_a? String then @data_type = :string
+ else @data_type = :boolean end
+ end
+
+ def serialize()
+ "#{@data_type}:#{@data}"
+ end
+
+ def self.parse(string_data)
+ string_data = string_data.split(':')
+ file = File.new nil
+ file.data_type = string_data[0].to_sym
+ file.data = string_data[1]
+ file
+ end
+ end
+
+ class Directory
+ attr_reader :files
+ attr_reader :directories
+
+ def initialize()
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ if @files.has_key? name then return nil end
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = Directory.new)
+ if @directories.has_key? name then return nil end
+ @directories[name] = directory
+ end
+
+ def [](name)
+ return @directories[name] if @directories.has_key? name
+ @files[name]
+ end
+
+ def serialize()
+ result = "#{@files.count}:"
+ @files.each do |name, item|
+ result += "#{name}:#{item.serialize.length}:#{item.serialize}"
+ end
+ result += "#{@directories.count}:"
+ @directories.each do |name, item|
+ result += "#{name}:#{item.serialize.length}:#{item.serialize}"
+ end
+ result
+ end
+
+ def self.parse(string_data)
+ end
+ end
+end

Бисер обнови решението на 08.11.2014 21:21 (преди около 10 години)

module RBFS
class File
attr_accessor :data
- attr_reader :data_type
+ attr_accessor :data_type
def initialize(data = nil)
@data = data
if data.is_a? Fixnum then @data_type = :number
elsif data.is_a? Symbol then @data_type = :symbol
elsif data.is_a? NilClass then @data_type = :nil
elsif data.is_a? String then @data_type = :string
else @data_type = :boolean end
end
def serialize()
"#{@data_type}:#{@data}"
end
def self.parse(string_data)
- string_data = string_data.split(':')
+ guts = string_data.split(':')
file = File.new nil
- file.data_type = string_data[0].to_sym
- file.data = string_data[1]
+ file.data_type = guts[0].to_sym
+ file.data = guts[1]
file
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize()
@files = {}
@directories = {}
end
def add_file(name, file)
- if @files.has_key? name then return nil end
- @files[name] = file
+ if !@files.has_key? name then
+ @files[name] = file
+ end
end
def add_directory(name, directory = Directory.new)
- if @directories.has_key? name then return nil end
- @directories[name] = directory
+ if !@directories.has_key? name then
+ @directories[name] = directory
+ end
end
def [](name)
return @directories[name] if @directories.has_key? name
@files[name]
end
def serialize()
result = "#{@files.count}:"
@files.each do |name, item|
result += "#{name}:#{item.serialize.length}:#{item.serialize}"
end
result += "#{@directories.count}:"
@directories.each do |name, item|
result += "#{name}:#{item.serialize.length}:#{item.serialize}"
end
result
end
- def self.parse(string_data)
+ def self.parse(data)
+ dir = Directory.new
+ #read all files
+ index, dir = read_data(data, 0, dir, RBFS::File) do |dir, name, type, data|
+ dir.add_file(name, type::parse(data))
+ dir
+ end
+ #read all subdirectories
+ index, dir = read_data(data, index, dir, RBFS::Directory) do |dir, name, type, data|
+ dir.add_directory(name, type::parse(data))
+ dir
+ end
+ #return the accumulated structure
+ dir
end
end
+
+ #read a chunk of data from the given string
+ #where current is the reading start position
+ #and fixed_length can be 0 to read until the next ':'
+ # or a positive value to read a fixed length of data
+ def Directory::read_chunk(data, current = 0, fixed_length = 0)
+ if fixed_length > 0 then
+ result = data[current ... current + fixed_length]
+ new = current + fixed_length
+ else
+ result = data[current ... data.index(':', current)]
+ new = data.index(':', current) + 1
+ end
+
+ return new, result
+ end
+
+ #parse a certain type of info from the data
+ #where current is the reading start position
+ #directory is the object to work on
+ #type is the class being added
+ #and method is the method used for adding the data
+ # the method needs 4 parameters:
+ # • directory to work on,
+ # • name of new object added,
+ # • class of the new object,
+ # • the data to be parsed
+ def Directory::read_data(data, current, directory, type, &method)
+ current, num_dirs = read_chunk(data, current)
+
+ num_dirs.to_i.times do
+ current, name = read_chunk(data, current)
+ current, len = read_chunk(data, current)
+ current, string_data = read_chunk(data, current, len.to_i)
+ directory = method.call(directory, name, type, string_data)
+ end
+
+ return current, directory
+ end
+
end

Бисер обнови решението на 10.11.2014 16:44 (преди около 10 години)

module RBFS
class File
attr_accessor :data
attr_accessor :data_type
def initialize(data = nil)
@data = data
if data.is_a? Fixnum then @data_type = :number
elsif data.is_a? Symbol then @data_type = :symbol
elsif data.is_a? NilClass then @data_type = :nil
elsif data.is_a? String then @data_type = :string
else @data_type = :boolean end
end
def serialize()
"#{@data_type}:#{@data}"
end
def self.parse(string_data)
- guts = string_data.split(':')
+ index = string_data.index(':')
file = File.new nil
- file.data_type = guts[0].to_sym
- file.data = guts[1]
+ file.data_type = string_data[0...index].to_sym
+ file.data = string_data[(index+1)..-1]
file
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize()
@files = {}
@directories = {}
end
def add_file(name, file)
if !@files.has_key? name then
@files[name] = file
end
end
def add_directory(name, directory = Directory.new)
if !@directories.has_key? name then
@directories[name] = directory
end
end
def [](name)
return @directories[name] if @directories.has_key? name
@files[name]
end
def serialize()
result = "#{@files.count}:"
@files.each do |name, item|
result += "#{name}:#{item.serialize.length}:#{item.serialize}"
end
result += "#{@directories.count}:"
@directories.each do |name, item|
result += "#{name}:#{item.serialize.length}:#{item.serialize}"
end
result
end
def self.parse(data)
dir = Directory.new
#read all files
index, dir = read_data(data, 0, dir, RBFS::File) do |dir, name, type, data|
dir.add_file(name, type::parse(data))
dir
end
#read all subdirectories
index, dir = read_data(data, index, dir, RBFS::Directory) do |dir, name, type, data|
dir.add_directory(name, type::parse(data))
dir
end
#return the accumulated structure
dir
end
end
#read a chunk of data from the given string
#where current is the reading start position
#and fixed_length can be 0 to read until the next ':'
# or a positive value to read a fixed length of data
def Directory::read_chunk(data, current = 0, fixed_length = 0)
if fixed_length > 0 then
result = data[current ... current + fixed_length]
new = current + fixed_length
else
result = data[current ... data.index(':', current)]
new = data.index(':', current) + 1
end
return new, result
end
#parse a certain type of info from the data
#where current is the reading start position
#directory is the object to work on
#type is the class being added
#and method is the method used for adding the data
# the method needs 4 parameters:
# • directory to work on,
# • name of new object added,
# • class of the new object,
# • the data to be parsed
def Directory::read_data(data, current, directory, type, &method)
current, num_dirs = read_chunk(data, current)
num_dirs.to_i.times do
current, name = read_chunk(data, current)
current, len = read_chunk(data, current)
current, string_data = read_chunk(data, current, len.to_i)
directory = method.call(directory, name, type, string_data)
end
return current, directory
end
-
end