Решение на Трета задача от Александър Стефанов

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

Към профила на Александър Стефанов

Резултати

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

Код

module RBFS
class StringToDir
@files
@file
@dirs
@dir
@dir_str
def initialize str
@files = Hash.new
@file = File.new
@dir = Directory.new
@dirs = Hash.new
@dir_str = str
string_to_files
string_to_dirs
end
def string_to_dirs
num,@dir_str = @dir_str.split(":",2)
while num.to_i > @dirs.size
name,size,rest = @dir.split(":",3)
one_dir_str = rest[0..size.to_i]
@dir_str = rest[size.to_i + 1..rest.length]
@dirs.join({name => @dir.sanitize})
end
end
def string_to_files
num,@dir_str = @dir_str.split(":",2)
while num.to_i > files.size
name,size,rest = @dir_str.split(":",3)
file_str = rest[0..size.to_i]
@dir_str = rest[size_to_i+1..rest.length]
@files.join({name => @file.sanitize(file_str)})
end
end
def get_hash
@dirs.join(@files)
end
end
class DirToString
def files_to_string(files)
files_text = String.new
files.each do |key,value|
value_str = value.serialize
char_num = value_str.length
files_text << key << ":" << char_num.to_s << ":" << value_str
end
files_text = nil if files.nil?
files_text
end
def dir_to_string(dir)
dir_text = ""
dir.each do |key,value|
value_str = value.serialize
char_num = value_str.length.to_s
dir_text << dir_text << key << ":" << char_num << ":" + value_str << ":"
end
dir_text = nil if dir.nil?
dir_text
end
end
class File
attr_accessor :data
attr_reader:data_type
def initialize object = nil
@data = object
change_data_type
end
def data_type
@data_type
end
def change_data_type
if(@data.is_a?(TrueClass) || @data.is_a?(FalseClass)) then @data_type = :boolean
elsif (@data.kind_of?(Integer) || @data.kind_of?(Float)) then @data_type = :number
elsif (@data.is_a? Symbol) then @data_type = :symbol
elsif (@data.is_a? String) then @data_type = :string
elsif (@data.nil?) then @data_type = :nil
end
end
def data=(object = nil)
@data = object if !object.nil?
change_data_type
end
def data
@data
end
def serialize
serialized_string = data_type.to_s + ":" + data.to_s
end
def parse string_data
array = string_data.split(":")
type,@data = array[0],array[1]
find_object_type(type,@data)
end
def find_object_type (type, string_obj)
if(type == "nil") then @data = nil
elsif (type == "boolean") then @data = (string_obj == "true")
elsif (type == "number" and string_obj.include? ".") then @data = string_obj.to_f
elsif (type == "number") then @data = string_obj.to_i
elsif (type == "symbol") then @data = string_obj.to_sym
end
change_data_type
end
end
class Directory
@hash
def initialize
@hash = Hash.new
end
def add_file(name,file)
if(name.include? ":")
nil
else
@hash.store(name,file)
end
end
def add_directory(name,directory=nil)
directory = Directory.new if directory.nil?
@hash.store(name,directory)
end
def [](name)
@hash[name]
end
def serialize
file_num = files.length.to_s
dir_num = directories.length.to_s
serializer=DirToString.new
files_str = serializer.files_to_string(files)
dir_str = serializer.dir_to_string(directories).chop
file_num + ":" + files_str + dir_num + ":" + dir_str
end
def parse string_data
parser = StringToDir.new string_data
@hash = parser.get_hash
end
def files
@hash.select {|key,item| item.instance_of? File}
end
def directories
@hash.select{|key,item| item.instance_of? Directory}
end
end
end

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

.........FFFFF..........F..FF..F..F..F..F..F

Failures:

  1) RBFS Directory serialization #serialize can serialize multiple directories recursively
     Failure/Error: expect(directory.serialize).to eq recursive_serialized_string
       
       expected: "2:README:19:string:Hello world!spec.rb:20:string:describe RBFS2:rbfs:64:1:solution.rb:13:symbol:hidden1:spec:24:1:test:12:boolean:true0:sample:4:0:0:"
            got: "2:README:19:string:Hello world!spec.rb:20:string:describe RBFS2:rbfs:64:1:solution.rb:13:symbol:hidden1:spec:24:1:test:12:boolean:true0::rbfs:64:1:solution.rb:13:symbol:hidden1:spec:24:1:test:12:boolean:true0::sample:4:0:0:"
       
       (compared using ==)
     # /tmp/d20141111-26053-l2m2fw/spec.rb:95: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 empty directories
     Failure/Error: parsed_directory = RBFS::Directory.parse('0:0:')
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-l2m2fw/spec.rb:101: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 Directory serialization ::parse can parse directories with files
     Failure/Error: parsed_directory = RBFS::Directory.parse(simple_serialized_string)
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-l2m2fw/spec.rb:108: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 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 `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-l2m2fw/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)>'

  5) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: parsed_directory = RBFS::Directory.parse(recursive_serialized_string)
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-l2m2fw/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)>'

  6) 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-l2m2fw/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)>'

  7) 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-l2m2fw/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)>'

  8) 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-l2m2fw/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)>'

  9) 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-l2m2fw/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)>'

  10) 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-l2m2fw/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)>'

  11) 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-l2m2fw/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)>'

  12) 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-l2m2fw/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)>'

  13) 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-l2m2fw/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.04193 seconds
44 examples, 13 failures

Failed examples:

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

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

Александър обнови решението на 10.11.2014 15:41 (преди около 10 години)

+module RBFS
+ class StringToDir
+ @files
+ @file
+ @dirs
+ @dir
+ @dir_str
+ def initialize str
+ @files = Hash.new
+ @file = File.new
+ @dir = Directory.new
+ @dirs = Hash.new
+ @dir_str = str
+ string_to_files
+ string_to_dirs
+ end
+ def string_to_dirs
+ num,@dir_str = @dir_str.split(":",2)
+ while num.to_i > @dirs.size
+ name,size,rest = @dir.split(":",3)
+ one_dir_str = rest[0..size.to_i]
+ @dir_str = rest[size.to_i + 1..rest.length]
+ @dirs.join({name => @dir.sanitize})
+ end
+ end
+ def string_to_files
+ num,@dir_str = @dir_str.split(":",2)
+ while num.to_i > files.size
+ name,size,rest = @dir_str.split(":",3)
+ file_str = rest[0..size.to_i]
+ @dir_str = rest[size_to_i+1..rest.length]
+ @files.join({name => @file.sanitize(file_str)})
+ end
+ end
+ def get_hash
+ @dirs.join(@files)
+ end
+ end
+ class DirToString
+ def files_to_string(files)
+ files_text = String.new
+ files.each do |key,value|
+ value_str = value.serialize
+ char_num = value_str.length
+ files_text << key << ":" << char_num.to_s << ":" << value_str
+ end
+ files_text = nil if files.nil?
+ files_text
+ end
+ def dir_to_string(dir)
+ dir_text = ""
+ dir.each do |key,value|
+ value_str = value.serialize
+ char_num = value_str.length.to_s
+ dir_text << dir_text << key << ":" << char_num << ":" + value_str << ":"
+ end
+ dir_text = nil if dir.nil?
+ dir_text
+ end
+ end
+ class File
+ attr_accessor :data
+ attr_reader:data_type
+ def initialize object = nil
+ @data = object
+ change_data_type
+ end
+ def data_type
+ @data_type
+ end
+ def change_data_type
+ if(@data.is_a?(TrueClass) || @data.is_a?(FalseClass)) then @data_type = :boolean
+ elsif (@data.kind_of?(Integer) || @data.kind_of?(Float)) then @data_type = :number
+ elsif (@data.is_a? Symbol) then @data_type = :symbol
+ elsif (@data.is_a? String) then @data_type = :string
+ elsif (@data.nil?) then @data_type = :nil
+ end
+ end
+ def data=(object = nil)
+ @data = object if !object.nil?
+ change_data_type
+ end
+ def data
+ @data
+ end
+ def serialize
+ serialized_string = data_type.to_s + ":" + data.to_s
+ end
+ def parse string_data
+ array = string_data.split(":")
+ type,@data = array[0],array[1]
+ find_object_type(type,@data)
+ end
+ def find_object_type (type, string_obj)
+ if(type == "nil") then @data = nil
+ elsif (type == "boolean") then @data = (string_obj == "true")
+ elsif (type == "number" and string_obj.include? ".") then @data = string_obj.to_f
+ elsif (type == "number") then @data = string_obj.to_i
+ elsif (type == "symbol") then @data = string_obj.to_sym
+ end
+ change_data_type
+ end
+ end
+ class Directory
+ @hash
+ def initialize
+ @hash = Hash.new
+ end
+ def add_file(name,file)
+ if(name.include? ":")
+ nil
+ else
+ @hash.store(name,file)
+ end
+ end
+ def add_directory(name,directory=nil)
+ directory = Directory.new if directory.nil?
+ @hash.store(name,directory)
+ end
+ def [](name)
+ @hash[name]
+ end
+ def serialize
+ file_num = files.length.to_s
+ dir_num = directories.length.to_s
+ serializer=DirToString.new
+ files_str = serializer.files_to_string(files)
+ dir_str = serializer.dir_to_string(directories).chop
+ file_num + ":" + files_str + dir_num + ":" + dir_str
+ end
+ def parse string_data
+ parser = StringToDir.new string_data
+ @hash = parser.get_hash
+ end
+ def files
+ @hash.select {|key,item| item.instance_of? File}
+ end
+ def directories
+ @hash.select{|key,item| item.instance_of? Directory}
+ end
+ end
+end