Решение на Трета задача от Людмил Делчев

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

Към профила на Людмил Делчев

Резултати

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

Код

module RBFS
module Serialize
def file_serialize(name, file)
"#{name}:#{file.serialize.size}:#{file.serialize}"
end
def files_serialize
return nil if files.empty?
serialized = ""
files.each{|name, file| serialized << file_serialize(name, file)}
serialized
end
def directory_serialize(name, directory)
"#{name}:#{directory.serialize.size}:#{directory.serialize}"
end
def directories_serialize
return nil if directories.empty?
serialized = ""
directories.each{|name, directory| serialized<<directory_serialize(name, directory)}
serialized
end
end
class Parse
attr_accessor :serialized_data
def initialize(serialized_data)
@serialized_data = serialized_data
@directory = Directory.new
end
def parse_files(number_of_files, serialized_data)
number_of_files.times do
file_name, file_size, @serialized_data = serialized_data.split(':', 3)
serialized_data = @serialized_data
file_size = file_size.to_i
serialized_file = @serialized_data.slice!(0, file_size)
@directory.add_file(file_name, File.new.parse(serialized_file))
end
end
def parse_directories(number_of_directories, serialized_data)
number_of_directories.times do
directory_name, directory_size, @serialized_data = serialized_data.split(':', 3)
serialized_data = @serialized_dat
directory_size = directory_size.to_i
serialized_directory = @serialized_data.slice!(0, directory_size)
@directory.add_directory(directory_name, Directory.parse(serialized_directory))
end
end
def parse
number_of_files, @serialized_data = @serialized_data.split(':', 2)
number_of_files = number_of_files.to_i
parse_files(number_of_files, @serialized_data)
number_of_directories, @serialized_data = @serialized_data.split(':', 2)
number_of_directories = number_of_directories.to_i
parse_directories(number_of_directories, @serialized_data)
@directory
end
end
class File
attr_reader :data_type, :data
def initialize(data = nil)
@data = data
@data_type = type_of_data(@data)
end
def data=(other_data)
@data = other_data
@data_type = type_of_data(other_data)
end
def type_of_data(data)
return :nil if @data.is_a?(NilClass)
return :string if @data.is_a?(String)
return :symbol if @data.is_a?(Symbol)
return :number if @data.is_a?(Integer) || @data.is_a?(Float)
return :boolean if @data.is_a?(TrueClass) || @data.is_a?(FalseClass)
end
def serialize
"#{@data_type}:#{@data}"
end
def create_file(type, data)
if type == "nil" then return File.new end
if type == "string" then return File.new(data.to_s) end
if type == "symbol" then return File.new(data.to_sym) end
if type == "boolean" && data =="true" then return File.new(true) end
if type == "boolean" && data =="false" then return File.new(false) end
if type == "number" && data.include?('.') then return File.new(data.to_f) end
if type == "number" then return File.new(data.to_i) end
end
def parse(string_data)
type, data = string_data.split(':')
create_file(type, data)
end
end
class Directory
include Serialize
attr_reader :files, :directories
def initialize
@directories = Hash.new
@files = Hash.new
end
def add_file(name, file = File.new)
@files[name] = file
end
def add_directory(name, directory = Directory.new)
@directories[name] = directory
end
def [](name)
return directories[name] if directories.has_key?(name)
files[name]
end
def serialize
"#{files.size}:#{files_serialize}#{directories.size}:#{directories_serialize}"
end
def Directory.parse(string_data)
Parse.new(string_data).parse
end
end
end

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

............FF..........F..FF..F..F..F..F..F

Failures:

  1) RBFS Directory serialization ::parse can parse directory trees without files
     Failure/Error: parsed_directory = RBFS::Directory.parse('0:2:dir1:15:0:1:dir2:4:0:0:dir3:4:0:0:')
     NoMethodError:
       undefined method `split' for nil:NilClass
     # /tmp/d20141111-26053-arstxi/solution.rb:48:in `block in parse_directories'
     # /tmp/d20141111-26053-arstxi/solution.rb:47:in `times'
     # /tmp/d20141111-26053-arstxi/solution.rb:47:in `parse_directories'
     # /tmp/d20141111-26053-arstxi/solution.rb:62:in `parse'
     # /tmp/d20141111-26053-arstxi/solution.rb:136:in `parse'
     # /tmp/d20141111-26053-arstxi/spec.rb:117: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 Directory serialization ::parse can parse directories recursively
     Failure/Error: parsed_directory = RBFS::Directory.parse(recursive_serialized_string)
     NoMethodError:
       undefined method `split' for nil:NilClass
     # /tmp/d20141111-26053-arstxi/solution.rb:48:in `block in parse_directories'
     # /tmp/d20141111-26053-arstxi/solution.rb:47:in `times'
     # /tmp/d20141111-26053-arstxi/solution.rb:47:in `parse_directories'
     # /tmp/d20141111-26053-arstxi/solution.rb:62:in `parse'
     # /tmp/d20141111-26053-arstxi/solution.rb:136:in `parse'
     # /tmp/d20141111-26053-arstxi/spec.rb:125: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 nil can be parsed
     Failure/Error: file = RBFS::File.parse('nil:')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-arstxi/spec.rb:230: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 parsed
     Failure/Error: file = RBFS::File.parse('string:Hey there')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-arstxi/spec.rb:248: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 string can parse a string with colons
     Failure/Error: file = RBFS::File.parse('string:Hay :)')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-arstxi/spec.rb:255: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 parsed
     Failure/Error: file = RBFS::File.parse('symbol:hello')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-arstxi/spec.rb:274: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 number can be parsed
     Failure/Error: file = RBFS::File.parse('number:1234')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-arstxi/spec.rb:293: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 float number can be parsed
     Failure/Error: file = RBFS::File.parse('number:3.14')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-arstxi/spec.rb:312: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 boolean true can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:true')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-arstxi/spec.rb:332: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)>'

  10) RBFS File data type boolean false can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:false')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-arstxi/spec.rb:351: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.04237 seconds
44 examples, 10 failures

Failed examples:

rspec /tmp/d20141111-26053-arstxi/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-arstxi/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-arstxi/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-arstxi/spec.rb:247 # RBFS File data type string can be parsed
rspec /tmp/d20141111-26053-arstxi/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-arstxi/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-arstxi/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-arstxi/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-arstxi/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-arstxi/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Людмил обнови решението на 08.11.2014 00:11 (преди около 10 години)

+module RBFS
+
+ module Serialize
+ def file_serialize(name, file)
+ "#{name}:#{file.serialize.size}:#{file.serialize}"
+ end
+
+ def files_serialize
+ return nil if files.empty?
+
+ serialized = ""
+ files.each{|name, file| serialized<<file_serialize(name, file)}
+ serialized
+ end
+
+ def directory_serialize(name, directory)
+ "#{name}:#{directory.serialize.size}:#{directory.serialize}"
+ end
+
+ def directories_serialize
+ return nil if directories.empty?
+
+ serialized = ""
+ directories.each{|name, directory| serialized<<directory_serialize(name, directory)}
+ serialized
+ end
+ end
+
+ class File
+ attr_reader :data_type, :data
+
+ def initialize(data = nil)
+ @data = data
+ @data_type = type_of_data(data)
+ end
+
+ def data=(other_data)
+ @data = other_data
+ @data_type = type_of_data(other_data)
+ end
+
+ def type_of_data(data)
+ return :nil if @data.is_a?(NilClass)
+ return :string if @data.is_a?(String)
+ return :symbol if @data.is_a?(Symbol)
+ return :number if @data.is_a?(Integer) || @data.is_a?(Float)
+ return :boolean if @data.is_a?(TrueClass) || @data.is_a?(FalseClass)
+ end
+
+ def serialize
+ "#{@data_type}:#{@data}"
+ end
+
+ def parse(string_data)
+ end
+ end
+
+ class Directory
+ include Serialize
+
+ def initialize
+ @directory = Hash.new
+ end
+
+ def add_file(name, file)
+ @directory[name] = file
+ end
+
+ def add_directory(name, directory = Directory.new)
+ @directory[name] = directory
+ end
+
+ def [](name)
+ @directory[name]
+ end
+
+ def serialize
+ "#{files.size}:#{files_serialize}#{directories.size}:#{directories_serialize}"
+ end
+
+ def parse(string_data)
+ end
+
+ def files
+ @directory.select{|name, object| object.is_a?(File)}
+ end
+
+ def directories
+ @directory.select{|name, object| object.is_a?(Directory)}
+ end
+ end
+end

Людмил обнови решението на 10.11.2014 12:41 (преди около 10 години)

module RBFS
-
module Serialize
def file_serialize(name, file)
"#{name}:#{file.serialize.size}:#{file.serialize}"
end
def files_serialize
return nil if files.empty?
serialized = ""
- files.each{|name, file| serialized<<file_serialize(name, file)}
+ files.each{|name, file| serialized << file_serialize(name, file)}
serialized
end
def directory_serialize(name, directory)
"#{name}:#{directory.serialize.size}:#{directory.serialize}"
end
def directories_serialize
return nil if directories.empty?
serialized = ""
directories.each{|name, directory| serialized<<directory_serialize(name, directory)}
serialized
end
end
+ class Parse
+ attr_accessor :serialized_data
+
+ def initialize(serialized_data)
+ @serialized_data = serialized_data
+ @directory = Directory.new
+ end
+
+ def parse_files(number_of_files, serialized_data)
+ number_of_files.times do
+ file_name, file_size, @serialized_data = serialized_data.split(':', 3)
+ serialized_data = @serialized_data
+ file_size = file_size.to_i
+ serialized_file = @serialized_data.slice!(0, file_size)
+ @directory.add_file(file_name, File.new.parse(serialized_file))
+ end
+ end
+
+ def parse_directories(number_of_directories, serialized_data)
+ number_of_directories.times do
+ directory_name, directory_size, @serialized_data = serialized_data.split(':', 3)
+ serialized_data = @serialized_dat
+ directory_size = directory_size.to_i
+ serialized_directory = @serialized_data.slice!(0, directory_size)
+ @directory.add_directory(directory_name, Directory.parse(serialized_directory))
+ end
+ end
+
+ def parse
+ number_of_files, @serialized_data = @serialized_data.split(':', 2)
+ number_of_files = number_of_files.to_i
+ parse_files(number_of_files, @serialized_data)
+ number_of_directories, @serialized_data = @serialized_data.split(':', 2)
+ number_of_directories = number_of_directories.to_i
+ parse_directories(number_of_directories, @serialized_data)
+ @directory
+ end
+ end
+
class File
attr_reader :data_type, :data
def initialize(data = nil)
@data = data
- @data_type = type_of_data(data)
+ @data_type = type_of_data(@data)
end
def data=(other_data)
@data = other_data
@data_type = type_of_data(other_data)
end
def type_of_data(data)
return :nil if @data.is_a?(NilClass)
return :string if @data.is_a?(String)
return :symbol if @data.is_a?(Symbol)
return :number if @data.is_a?(Integer) || @data.is_a?(Float)
return :boolean if @data.is_a?(TrueClass) || @data.is_a?(FalseClass)
end
def serialize
"#{@data_type}:#{@data}"
end
+ def create_file(type, data)
+ if type == "nil" then return File.new end
+ if type == "string" then return File.new(data.to_s) end
+ if type == "symbol" then return File.new(data.to_sym) end
+ if type == "boolean" && data =="true" then return File.new(true) end
+ if type == "boolean" && data =="false" then return File.new(false) end
+ if type == "number" && data.include?('.') then return File.new(data.to_f) end
+ if type == "number" then return File.new(data.to_i) end
+ end
+
def parse(string_data)
+ type, data = string_data.split(':')
+ create_file(type, data)
end
end
class Directory
include Serialize
+ attr_reader :files, :directories
+
def initialize
- @directory = Hash.new
+ @directories = Hash.new
+ @files = Hash.new
end
- def add_file(name, file)
- @directory[name] = file
+ def add_file(name, file = File.new)
+ @files[name] = file
end
def add_directory(name, directory = Directory.new)
- @directory[name] = directory
+ @directories[name] = directory
end
def [](name)
- @directory[name]
+ return directories[name] if directories.has_key?(name)
+ files[name]
end
def serialize
"#{files.size}:#{files_serialize}#{directories.size}:#{directories_serialize}"
end
- def parse(string_data)
- end
-
- def files
- @directory.select{|name, object| object.is_a?(File)}
- end
-
- def directories
- @directory.select{|name, object| object.is_a?(Directory)}
+ def Directory.parse(string_data)
+ Parse.new(string_data).parse
end
end
end