Решение на Трета задача от Любомир Папазов

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

Към профила на Любомир Папазов

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 42 успешни тест(а)
  • 2 неуспешни тест(а)

Код

class String
def to_rbfs_type
if self == "true" then true
elsif self == "false" then false
elsif self.include? "." then self.to_f
else
self.to_i
end
end
def parse_files(dir)
substrings = self.split(':', 4)
if substrings[0].to_i != 0
current_first_file = substrings[3].slice!(0..substrings[2].to_i-1)
dir.add_file(substrings[1], RBFS::File.parse(current_first_file))
((substrings[0].to_i-1).to_s + ":" + substrings[3]).parse_files(dir)
else
self.slice!(0..1)
self
end
end
def parse_directories(dir)
substrings = self.split(':', 4)
if substrings[0].to_i != 0
length = substrings[2]
first_directory = substrings[3][0..substrings[2].to_i-2]
other_directories = substrings[3][substrings[2].to_i-1..substrings[3].length.to_i-1]
tempDir = RBFS::Directory.parse(first_directory)
dir.add_directory(substrings[1], tempDir)
((substrings[0].to_i-1).to_s + ":" + other_directories).parse_directories(dir)
end
end
end
module RBFS
class File
attr_reader :data_type
attr_reader :data
def serialize
"#{data_type}:#{data.to_s}"
end
def self.parse(string_data)
type_and_data=string_data.split(':', 2)
case type_and_data[0]
when "string" then File.new(type_and_data[1])
when "boolean" then File.new(type_and_data[1].to_rbfs_type)
when "number" then File.new(type_and_data[1].to_rbfs_type)
when "symbol" then File.new(type_and_data[1].to_sym)
when "nil" then File.new()
end
end
def data=(value)
@data = value
update_data_type
end
def update_data_type
case @data.class.to_s
when "String" then @data_type = :string
when "Symbol" then @data_type = :symbol
when "Fixnum" then @data_type = :number
when "Float" then @data_type = :number
when "NilClass" then @data_type = :nil
else @data_type = :boolean
end
end
def initialize (*args)
@data = args[0]
update_data_type
end
end
class Directory
attr_reader :files
attr_reader :directories
def self.parse(string_data)
dirr = Directory.new
tempRes = string_data.parse_files(dirr)
tempRes.parse_directories(dirr)
dirr
end
def file_serialize
res = @files.count.to_s + ":"
@files.each_pair do |name, value|
number_of_symbols = value.data.to_s.length + value.data_type.to_s.length + 1
res += "#{name}:#{number_of_symbols}:#{value.serialize}"
end
res
end
def serialize
temp1= ""
res = "" + file_serialize
directories.each_pair do |name, folder|
temp2 = ""
temp2 += folder.serialize
temp1 += name.to_s + ":" + temp2.length.to_s + ":" + temp2
end
res = res + directories.count.to_s + ":" + temp1
end
def initialize
@files = {}
@directories = {}
end
def add_directory(name, *directory)
if directory[0]
@directories[name] = directory[0]
else
@directories[name] = Directory.new
end
end
def add_file(name, file)
@files[name] = file unless name.to_s.include?(":")
end
def [](name)
if @directories[name] then @directories[name]
elsif @files[name] then @files[name]
else nil
end
end
end
end

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

............FF..............................

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 `slice!' for nil:NilClass
     # /tmp/d20141111-26053-1xugw8f/solution.rb:14:in `parse_files'
     # /tmp/d20141111-26053-1xugw8f/solution.rb:87:in `parse'
     # /tmp/d20141111-26053-1xugw8f/solution.rb:29:in `parse_directories'
     # /tmp/d20141111-26053-1xugw8f/solution.rb:31:in `parse_directories'
     # /tmp/d20141111-26053-1xugw8f/solution.rb:88:in `parse'
     # /tmp/d20141111-26053-1xugw8f/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 `slice!' for nil:NilClass
     # /tmp/d20141111-26053-1xugw8f/solution.rb:14:in `parse_files'
     # /tmp/d20141111-26053-1xugw8f/solution.rb:87:in `parse'
     # /tmp/d20141111-26053-1xugw8f/solution.rb:29:in `parse_directories'
     # /tmp/d20141111-26053-1xugw8f/solution.rb:31:in `parse_directories'
     # /tmp/d20141111-26053-1xugw8f/solution.rb:88:in `parse'
     # /tmp/d20141111-26053-1xugw8f/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)>'

Finished in 0.04354 seconds
44 examples, 2 failures

Failed examples:

rspec /tmp/d20141111-26053-1xugw8f/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-1xugw8f/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively

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

Любомир обнови решението на 08.11.2014 18:08 (преди над 9 години)

+class String
+ def to_rbfs_type
+ if self == "true" then true
+ elsif self == "false" then false
+ elsif self.include? "." then self.to_f
+ else
+ self.to_i
+ end
+ end
+end
+
+
+module RBFS
+ class File
+ attr_reader :data_type
+
+ attr_reader :data
+
+ def serialize
+ "#{data_type}:#{data.to_s}"
+ end
+
+ def self.parse(string_data)
+ type_and_data=string_data.split(':', 2)
+ case type_and_data[0]
+ when "string" then File.new(type_and_data[1])
+ when "boolean" then File.new(type_and_data[1].to_rbfs_type)
+ when "number" then File.new(type_and_data[1].to_rbfs_type)
+ when "symbol" then File.new(type_and_data[1].to_sym)
+ when "nil" then File.new()
+ end
+ end
+
+ def data=(value)
+ @data = value
+ update_data_type
+ end
+
+ def update_data_type
+ case @data.class.to_s
+ when "String" then @data_type = :string
+ when "Symbol" then @data_type = :symbol
+ when "Fixnum" then @data_type = :number
+ when "Float" then @data_type = :number
+ when "NilClass" then @data_type = :nil
+ else @data_type = :boolean
+ end
+ end
+
+ def initialize (*args)
+ @data = args[0]
+ update_data_type
+ end
+ end
+
+ class Directory
+ attr_reader :files
+
+ attr_reader :directories
+
+ def file_serialize
+ res = @files.count.to_s + ":"
+ @files.each_pair do |name, value|
+ number_of_symbols = value.data.to_s.length + value.data_type.to_s.length + 1
+ res += "#{name}:#{number_of_symbols}:#{value.serialize}"
+ end
+ res
+ end
+
+ def serialize
+ temp1= ""
+ res = "" + file_serialize
+ directories.each_pair do |name, folder|
+ temp2 = ""
+ temp2 += folder.serialize
+ temp1 += name.to_s + ":" + temp2.length.to_s + ":" + temp2
+ end
+ res = res + directories.count.to_s + ":" + temp1
+ end
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_directory(name, *directory)
+ @directories[name] = directory[0] unless name.to_s.include?(":")
+ end
+
+
+ def add_file(name, file)
+ @files[name] = file unless name.to_s.include?(":")
+ end
+
+ def [](name)
+ if @directories[name] then @directories[name]
+ elsif @files[name] then @files[name]
+ else nil
+ end
+ end
+ end
+end

Любомир обнови решението на 09.11.2014 13:13 (преди над 9 години)

class String
def to_rbfs_type
if self == "true" then true
elsif self == "false" then false
elsif self.include? "." then self.to_f
else
self.to_i
end
end
+
+ def parse_files(dir)
+ substrings = self.split(':', 4)
+ if substrings[0].to_i != 0
+ current_first_file = substrings[3].slice!(0..substrings[2].to_i-1)
+ dir.add_file(substrings[1], RBFS::File.parse(current_first_file))
+ ((substrings[0].to_i-1).to_s + ":" + substrings[3]).parse_files(dir)
+ else
+ self.slice!(0..1)
+ self
+ end
+ end
+
+ def parse_directories(dir)
+ substrings = self.split(':', 4)
+ if substrings[0].to_i != 0
+ length = substrings[2]
+ first_directory = substrings[3][0..substrings[2].to_i-2]
+ other_directories = substrings[3][substrings[2].to_i-1..substrings[3].length.to_i-1]
+ tempDir = RBFS::Directory.parse(first_directory)
+ dir.add_directory(substrings[1], tempDir)
+ ((substrings[0].to_i-1).to_s + ":" + other_directories).parse_directories(dir)
+ end
+ end
end
module RBFS
class File
attr_reader :data_type
attr_reader :data
def serialize
"#{data_type}:#{data.to_s}"
end
def self.parse(string_data)
type_and_data=string_data.split(':', 2)
case type_and_data[0]
when "string" then File.new(type_and_data[1])
when "boolean" then File.new(type_and_data[1].to_rbfs_type)
when "number" then File.new(type_and_data[1].to_rbfs_type)
when "symbol" then File.new(type_and_data[1].to_sym)
when "nil" then File.new()
end
end
def data=(value)
@data = value
update_data_type
end
def update_data_type
case @data.class.to_s
when "String" then @data_type = :string
when "Symbol" then @data_type = :symbol
when "Fixnum" then @data_type = :number
when "Float" then @data_type = :number
when "NilClass" then @data_type = :nil
else @data_type = :boolean
end
end
def initialize (*args)
@data = args[0]
update_data_type
end
end
class Directory
attr_reader :files
attr_reader :directories
def file_serialize
res = @files.count.to_s + ":"
@files.each_pair do |name, value|
number_of_symbols = value.data.to_s.length + value.data_type.to_s.length + 1
res += "#{name}:#{number_of_symbols}:#{value.serialize}"
end
res
end
+
+ def self.parse(string_data)
+ dirr = Directory.new
+ tempRes = string_data.parse_files(dirr)
+ tempRes.parse_directories(dirr)
+ dirr
+ end
+
def serialize
temp1= ""
res = "" + file_serialize
directories.each_pair do |name, folder|
temp2 = ""
temp2 += folder.serialize
temp1 += name.to_s + ":" + temp2.length.to_s + ":" + temp2
end
res = res + directories.count.to_s + ":" + temp1
end
def initialize
@files = {}
@directories = {}
end
def add_directory(name, *directory)
@directories[name] = directory[0] unless name.to_s.include?(":")
end
def add_file(name, file)
@files[name] = file unless name.to_s.include?(":")
end
def [](name)
if @directories[name] then @directories[name]
elsif @files[name] then @files[name]
else nil
end
end
end
end

Любомир обнови решението на 09.11.2014 13:48 (преди над 9 години)

class String
def to_rbfs_type
if self == "true" then true
elsif self == "false" then false
elsif self.include? "." then self.to_f
else
self.to_i
end
end
def parse_files(dir)
substrings = self.split(':', 4)
if substrings[0].to_i != 0
current_first_file = substrings[3].slice!(0..substrings[2].to_i-1)
dir.add_file(substrings[1], RBFS::File.parse(current_first_file))
((substrings[0].to_i-1).to_s + ":" + substrings[3]).parse_files(dir)
else
self.slice!(0..1)
self
end
end
def parse_directories(dir)
substrings = self.split(':', 4)
if substrings[0].to_i != 0
length = substrings[2]
first_directory = substrings[3][0..substrings[2].to_i-2]
other_directories = substrings[3][substrings[2].to_i-1..substrings[3].length.to_i-1]
tempDir = RBFS::Directory.parse(first_directory)
dir.add_directory(substrings[1], tempDir)
((substrings[0].to_i-1).to_s + ":" + other_directories).parse_directories(dir)
end
end
end
module RBFS
class File
attr_reader :data_type
attr_reader :data
def serialize
"#{data_type}:#{data.to_s}"
end
def self.parse(string_data)
type_and_data=string_data.split(':', 2)
case type_and_data[0]
when "string" then File.new(type_and_data[1])
when "boolean" then File.new(type_and_data[1].to_rbfs_type)
when "number" then File.new(type_and_data[1].to_rbfs_type)
when "symbol" then File.new(type_and_data[1].to_sym)
when "nil" then File.new()
end
end
def data=(value)
@data = value
update_data_type
end
def update_data_type
case @data.class.to_s
when "String" then @data_type = :string
when "Symbol" then @data_type = :symbol
when "Fixnum" then @data_type = :number
when "Float" then @data_type = :number
when "NilClass" then @data_type = :nil
else @data_type = :boolean
end
end
def initialize (*args)
@data = args[0]
update_data_type
end
end
class Directory
attr_reader :files
attr_reader :directories
+ def self.parse(string_data)
+ dirr = Directory.new
+ tempRes = string_data.parse_files(dirr)
+ tempRes.parse_directories(dirr)
+ dirr
+ end
+
def file_serialize
res = @files.count.to_s + ":"
@files.each_pair do |name, value|
number_of_symbols = value.data.to_s.length + value.data_type.to_s.length + 1
res += "#{name}:#{number_of_symbols}:#{value.serialize}"
end
res
end
- def self.parse(string_data)
- dirr = Directory.new
- tempRes = string_data.parse_files(dirr)
- tempRes.parse_directories(dirr)
- dirr
- end
-
-
def serialize
temp1= ""
res = "" + file_serialize
directories.each_pair do |name, folder|
temp2 = ""
temp2 += folder.serialize
temp1 += name.to_s + ":" + temp2.length.to_s + ":" + temp2
end
res = res + directories.count.to_s + ":" + temp1
end
def initialize
@files = {}
@directories = {}
end
def add_directory(name, *directory)
- @directories[name] = directory[0] unless name.to_s.include?(":")
+ if directory[0]
+ @directories[name] = directory[0]
+ else
+ @directories[name] = Directory.new
+ end
end
-
def add_file(name, file)
@files[name] = file unless name.to_s.include?(":")
end
def [](name)
if @directories[name] then @directories[name]
elsif @files[name] then @files[name]
else nil
end
end
end
end