Решение на Трета задача от Ясен Трифонов

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

Към профила на Ясен Трифонов

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def initialize(object = nil)
@data = object
end
def self.parse(string_data)
type = string_data.split(/:/).first
data = string_data[type.size+1..-1]
File.new Helper.create_object(type, data)
end
def data_type
case data
when NilClass then :nil
when String then :string
when Symbol then :symbol
when TrueClass, FalseClass then :boolean
else :number
end
end
def serialize
"#{data_type}:#{data}"
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize(files = {}, dirs = {})
@files = files
@directories = dirs
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = RBFS::Directory.new)
@directories[name] = directory
end
def serialize
"#{@files.size}:#{serialize_files}#{@directories.size}:#{serialize_directories}"
end
def self.parse(string_data)
num_files = Helper.get_string_to_colon(string_data).to_i
file_data = Helper.get_string_after_colon(string_data)
files, dir_data = Helper.make_file_hash(num_files, file_data)
num_dirs = Helper.get_string_to_colon(dir_data).to_i
p dir_data
dir_data = Helper.get_string_after_colon(dir_data)
directories = Helper.make_dir_hash(num_dirs, dir_data)
Directory.new(files, directories)
end
def [](name)
return @directories[name] if @directories.has_key? name
return @files[name] if @files.has_key? name
nil
end
private
def serialize_files
serialized = ""
@files.each do |name, file|
serialized << name << ':' << file.serialize.size.to_s << ':' << file.serialize
end
serialized
end
def serialize_directories
serialized = ""
@directories.each do |name, dir|
serialized << name << ':' << dir.serialize.size.to_s << ':' << dir.serialize
end
serialized
end
end
class Helper
def self.create_object(type, data)
case type
when "string" then data
when "number" then parse_number data
when "nil" then nil
when "symbol" then data.to_sym
when "boolean" then parse_boolean data
end
end
def self.get_string_to_colon(data)
data.split(/:/).first
end
def self.get_string_after_colon(data)
data[get_string_to_colon(data).size+1..-1]
end
def self.make_file_hash(num_files, file_data)
files = {}
num_files.times do
name, length, file_data = Helper.get_length_and_data(file_data)
files[name] = RBFS::File.parse(file_data[0..length-1])
file_data = file_data[length..-1]
end
[files, file_data]
end
def self.make_dir_hash(num_dirs, dir_data)
directories = {}
num_dirs.times do
name, length, dir_data = Helper.get_length_and_data(dir_data)
directories[name] = RBFS::Directory.parse(dir_data[0..length-1])
dir_data = dir_data[length..-1]
end
directories
end
def self.get_length_and_data(string_data)
name = get_string_to_colon(string_data)
string_data = get_string_after_colon(string_data)
length = get_string_to_colon(string_data).to_i
string_data = get_string_after_colon(string_data)
[name, length, string_data]
end
def parse_number(data)
data.include?('.') ? data.to_f : data.to_i
end
def parse_boolean(data)
data == "true" ? true : false
end
end
end

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

.........."0:"
."1:rbfs:4:0:0:"
"0:"
."2:dir1:15:0:1:dir2:4:0:0:dir3:4:0:0:"
"1:dir2:4:0:0:"
"0:"
"0:"
."2:rbfs:64:1:solution.rb:13:symbol:hidden1:spec:24:1:test:12:boolean:true0:sample:4:0:0:"
"1:spec:24:1:test:12:boolean:true0:"
F....................F..F..F..F

Failures:

  1) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: parsed_directory = RBFS::Directory.parse(recursive_serialized_string)
     NoMethodError:
       undefined method `parse_boolean' for RBFS::Helper:Class
     # /tmp/d20141111-26053-11oc76h/solution.rb:95:in `create_object'
     # /tmp/d20141111-26053-11oc76h/solution.rb:12:in `parse'
     # /tmp/d20141111-26053-11oc76h/solution.rb:111:in `block in make_file_hash'
     # /tmp/d20141111-26053-11oc76h/solution.rb:109:in `times'
     # /tmp/d20141111-26053-11oc76h/solution.rb:109:in `make_file_hash'
     # /tmp/d20141111-26053-11oc76h/solution.rb:54:in `parse'
     # /tmp/d20141111-26053-11oc76h/solution.rb:121:in `block in make_dir_hash'
     # /tmp/d20141111-26053-11oc76h/solution.rb:119:in `times'
     # /tmp/d20141111-26053-11oc76h/solution.rb:119:in `make_dir_hash'
     # /tmp/d20141111-26053-11oc76h/solution.rb:59:in `parse'
     # /tmp/d20141111-26053-11oc76h/solution.rb:121:in `block in make_dir_hash'
     # /tmp/d20141111-26053-11oc76h/solution.rb:119:in `times'
     # /tmp/d20141111-26053-11oc76h/solution.rb:119:in `make_dir_hash'
     # /tmp/d20141111-26053-11oc76h/solution.rb:59:in `parse'
     # /tmp/d20141111-26053-11oc76h/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)>'

  2) RBFS File data type number can be parsed
     Failure/Error: file = RBFS::File.parse('number:1234')
     NoMethodError:
       undefined method `parse_number' for RBFS::Helper:Class
     # /tmp/d20141111-26053-11oc76h/solution.rb:92:in `create_object'
     # /tmp/d20141111-26053-11oc76h/solution.rb:12:in `parse'
     # /tmp/d20141111-26053-11oc76h/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)>'

  3) RBFS File data type float number can be parsed
     Failure/Error: file = RBFS::File.parse('number:3.14')
     NoMethodError:
       undefined method `parse_number' for RBFS::Helper:Class
     # /tmp/d20141111-26053-11oc76h/solution.rb:92:in `create_object'
     # /tmp/d20141111-26053-11oc76h/solution.rb:12:in `parse'
     # /tmp/d20141111-26053-11oc76h/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)>'

  4) RBFS File data type boolean true can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:true')
     NoMethodError:
       undefined method `parse_boolean' for RBFS::Helper:Class
     # /tmp/d20141111-26053-11oc76h/solution.rb:95:in `create_object'
     # /tmp/d20141111-26053-11oc76h/solution.rb:12:in `parse'
     # /tmp/d20141111-26053-11oc76h/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)>'

  5) RBFS File data type boolean false can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:false')
     NoMethodError:
       undefined method `parse_boolean' for RBFS::Helper:Class
     # /tmp/d20141111-26053-11oc76h/solution.rb:95:in `create_object'
     # /tmp/d20141111-26053-11oc76h/solution.rb:12:in `parse'
     # /tmp/d20141111-26053-11oc76h/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.04494 seconds
44 examples, 5 failures

Failed examples:

rspec /tmp/d20141111-26053-11oc76h/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-11oc76h/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-11oc76h/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-11oc76h/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-11oc76h/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Ясен обнови решението на 10.11.2014 13:08 (преди около 10 години)

+module RBFS
+ class File
+ attr_accessor :data
+
+ def initialize(object = nil)
+ @data = object
+ end
+
+ def self.parse(string_data)
+ type = string_data.split(/:/).first
+ data = string_data[type.size+1..-1]
+ File.new Helper.create_object(type, data)
+ end
+
+ def data_type
+ case data
+ when NilClass then :nil
+ when String then :string
+ when Symbol then :symbol
+ when TrueClass, FalseClass then :boolean
+ else :number
+ end
+ end
+
+ def serialize
+ "#{data_type}:#{data}"
+ end
+ end
+
+ class Directory
+
+ end
+
+ class Helper
+ def self.create_object(type, data)
+ case type
+ when "string" then data
+ when "number" then parse_number data
+ when "nil" then nil
+ when "symbol" then data.to_sym
+ when "boolean" then parse_boolean data
+ end
+ end
+
+ private
+
+ def parse_number(data)
+ data.include?('.') ? data.to_f : data.to_i
+ end
+
+ def parse_boolean(data)
+ data == "true" ? true : false
+ end
+ end
+end

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

module RBFS
class File
attr_accessor :data
def initialize(object = nil)
@data = object
end
def self.parse(string_data)
type = string_data.split(/:/).first
data = string_data[type.size+1..-1]
File.new Helper.create_object(type, data)
end
def data_type
case data
when NilClass then :nil
when String then :string
when Symbol then :symbol
when TrueClass, FalseClass then :boolean
else :number
end
end
def serialize
"#{data_type}:#{data}"
end
end
class Directory
+ attr_reader :files
+ attr_reader :directories
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = RBFS::Directory.new)
+ @directories[name] = directory
+ end
+
+ def serialize
+ "#{@files.size}:#{file_serialization}#{@directories}:#{directory_serialization}"
+ end
+
+ def self.parse
+
+ end
+
+ def [](name)
+
+ end
+
+ private
+
+ def file_serialization
+ serialized = ""
+ @files.each do |name, file|
+ serialized << name << ':' << file.serialize.size.to_s << ':' << file.serialize
+ end
+ serialized
+ end
+
+ def directory_serialization
+ serialized = ""
+ @directories.each do |name, dir|
+ serialized << name << ':' << dir.serialize.size.to_s << ':' << dir.serialize
+ end
+ serialized
+ end
end
class Helper
def self.create_object(type, data)
case type
when "string" then data
when "number" then parse_number data
when "nil" then nil
when "symbol" then data.to_sym
when "boolean" then parse_boolean data
end
end
private
def parse_number(data)
data.include?('.') ? data.to_f : data.to_i
end
def parse_boolean(data)
data == "true" ? true : false
end
end
end

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

module RBFS
class File
attr_accessor :data
def initialize(object = nil)
@data = object
end
def self.parse(string_data)
type = string_data.split(/:/).first
data = string_data[type.size+1..-1]
File.new Helper.create_object(type, data)
end
def data_type
case data
when NilClass then :nil
when String then :string
when Symbol then :symbol
when TrueClass, FalseClass then :boolean
else :number
end
end
def serialize
"#{data_type}:#{data}"
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = RBFS::Directory.new)
@directories[name] = directory
end
def serialize
- "#{@files.size}:#{file_serialization}#{@directories}:#{directory_serialization}"
+ "#{@files.size}:#{serialize_files}#{@directories.size}:#{serialize_directories}"
end
def self.parse
end
def [](name)
end
private
- def file_serialization
+ def serialize_files
serialized = ""
@files.each do |name, file|
serialized << name << ':' << file.serialize.size.to_s << ':' << file.serialize
end
serialized
end
- def directory_serialization
+ def serialize_directories
serialized = ""
@directories.each do |name, dir|
serialized << name << ':' << dir.serialize.size.to_s << ':' << dir.serialize
end
serialized
end
end
class Helper
def self.create_object(type, data)
case type
when "string" then data
when "number" then parse_number data
when "nil" then nil
when "symbol" then data.to_sym
when "boolean" then parse_boolean data
end
end
private
def parse_number(data)
data.include?('.') ? data.to_f : data.to_i
end
def parse_boolean(data)
data == "true" ? true : false
end
end
end

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

module RBFS
class File
attr_accessor :data
def initialize(object = nil)
@data = object
end
def self.parse(string_data)
type = string_data.split(/:/).first
data = string_data[type.size+1..-1]
File.new Helper.create_object(type, data)
end
def data_type
case data
when NilClass then :nil
when String then :string
when Symbol then :symbol
when TrueClass, FalseClass then :boolean
else :number
end
end
def serialize
"#{data_type}:#{data}"
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = RBFS::Directory.new)
@directories[name] = directory
end
def serialize
"#{@files.size}:#{serialize_files}#{@directories.size}:#{serialize_directories}"
end
def self.parse
end
def [](name)
-
+ return @directories[name] if @directories.has_key? name
+ return @files[name] if @files.has_key? name
+ nil
end
private
def serialize_files
serialized = ""
@files.each do |name, file|
serialized << name << ':' << file.serialize.size.to_s << ':' << file.serialize
end
serialized
end
def serialize_directories
serialized = ""
@directories.each do |name, dir|
serialized << name << ':' << dir.serialize.size.to_s << ':' << dir.serialize
end
serialized
end
end
class Helper
def self.create_object(type, data)
case type
when "string" then data
when "number" then parse_number data
when "nil" then nil
when "symbol" then data.to_sym
when "boolean" then parse_boolean data
end
end
private
def parse_number(data)
data.include?('.') ? data.to_f : data.to_i
end
def parse_boolean(data)
data == "true" ? true : false
end
end
end

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

module RBFS
class File
attr_accessor :data
def initialize(object = nil)
@data = object
end
def self.parse(string_data)
type = string_data.split(/:/).first
data = string_data[type.size+1..-1]
File.new Helper.create_object(type, data)
end
def data_type
case data
when NilClass then :nil
when String then :string
when Symbol then :symbol
when TrueClass, FalseClass then :boolean
else :number
end
end
def serialize
"#{data_type}:#{data}"
end
end
class Directory
attr_reader :files
attr_reader :directories
- def initialize
- @files = {}
- @directories = {}
+ def initialize(files = {}, dirs = {})
+ @files = files
+ @directories = dirs
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = RBFS::Directory.new)
@directories[name] = directory
end
def serialize
"#{@files.size}:#{serialize_files}#{@directories.size}:#{serialize_directories}"
end
- def self.parse
-
+ def self.parse(string_data)
+ num_files = Helper.get_string_to_colon(string_data).to_i
+ file_data = Helper.get_string_after_colon(string_data)
+ files, dir_data = Helper.make_file_hash(num_files, file_data)
+ Directory.new(files)
end
def [](name)
return @directories[name] if @directories.has_key? name
return @files[name] if @files.has_key? name
nil
end
private
def serialize_files
serialized = ""
@files.each do |name, file|
serialized << name << ':' << file.serialize.size.to_s << ':' << file.serialize
end
serialized
end
def serialize_directories
serialized = ""
@directories.each do |name, dir|
serialized << name << ':' << dir.serialize.size.to_s << ':' << dir.serialize
end
serialized
end
end
class Helper
def self.create_object(type, data)
case type
when "string" then data
when "number" then parse_number data
when "nil" then nil
when "symbol" then data.to_sym
when "boolean" then parse_boolean data
end
+ end
+
+ def self.get_string_to_colon(data)
+ data.split(/:/).first
+ end
+
+ def self.get_string_after_colon(data)
+ data[get_string_to_colon(data).size+1..-1]
+ end
+
+ def self.make_file_hash(num_files, file_data)
+ files = {}
+ num_files.times do
+ name = get_string_to_colon(file_data)
+ file_data = get_string_after_colon(file_data)
+ length = get_string_to_colon(file_data).to_i
+ file_data = get_string_after_colon(file_data)
+ files[name] = RBFS::File.parse(file_data[0..length-1])
+ end
+ [files, file_data]
end
private
def parse_number(data)
data.include?('.') ? data.to_f : data.to_i
end
def parse_boolean(data)
data == "true" ? true : false
end
end
end

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

module RBFS
class File
attr_accessor :data
def initialize(object = nil)
@data = object
end
def self.parse(string_data)
type = string_data.split(/:/).first
data = string_data[type.size+1..-1]
File.new Helper.create_object(type, data)
end
def data_type
case data
when NilClass then :nil
when String then :string
when Symbol then :symbol
when TrueClass, FalseClass then :boolean
else :number
end
end
def serialize
"#{data_type}:#{data}"
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize(files = {}, dirs = {})
@files = files
@directories = dirs
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = RBFS::Directory.new)
@directories[name] = directory
end
def serialize
"#{@files.size}:#{serialize_files}#{@directories.size}:#{serialize_directories}"
end
def self.parse(string_data)
num_files = Helper.get_string_to_colon(string_data).to_i
file_data = Helper.get_string_after_colon(string_data)
files, dir_data = Helper.make_file_hash(num_files, file_data)
- Directory.new(files)
+ num_dirs = Helper.get_string_to_colon(dir_data).to_i
+
+ p dir_data
+ dir_data = Helper.get_string_after_colon(dir_data)
+ directories = Helper.make_dir_hash(num_dirs, dir_data)
+ Directory.new(files, directories)
end
def [](name)
return @directories[name] if @directories.has_key? name
return @files[name] if @files.has_key? name
nil
end
private
def serialize_files
serialized = ""
@files.each do |name, file|
serialized << name << ':' << file.serialize.size.to_s << ':' << file.serialize
end
serialized
end
def serialize_directories
serialized = ""
@directories.each do |name, dir|
serialized << name << ':' << dir.serialize.size.to_s << ':' << dir.serialize
end
serialized
end
end
class Helper
def self.create_object(type, data)
case type
when "string" then data
when "number" then parse_number data
when "nil" then nil
when "symbol" then data.to_sym
when "boolean" then parse_boolean data
end
end
def self.get_string_to_colon(data)
data.split(/:/).first
end
def self.get_string_after_colon(data)
data[get_string_to_colon(data).size+1..-1]
end
def self.make_file_hash(num_files, file_data)
files = {}
num_files.times do
- name = get_string_to_colon(file_data)
- file_data = get_string_after_colon(file_data)
- length = get_string_to_colon(file_data).to_i
- file_data = get_string_after_colon(file_data)
+ name, length, file_data = Helper.get_length_and_data(file_data)
files[name] = RBFS::File.parse(file_data[0..length-1])
+ file_data = file_data[length..-1]
end
[files, file_data]
end
- private
+ def self.make_dir_hash(num_dirs, dir_data)
+ directories = {}
+ num_dirs.times do
+ name, length, dir_data = Helper.get_length_and_data(dir_data)
+ directories[name] = RBFS::Directory.parse(dir_data[0..length-1])
+ dir_data = dir_data[length..-1]
+ end
+ directories
+ end
+
+ def self.get_length_and_data(string_data)
+ name = get_string_to_colon(string_data)
+ string_data = get_string_after_colon(string_data)
+ length = get_string_to_colon(string_data).to_i
+ string_data = get_string_after_colon(string_data)
+ [name, length, string_data]
+ end
def parse_number(data)
data.include?('.') ? data.to_f : data.to_i
end
def parse_boolean(data)
data == "true" ? true : false
end
end
end