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

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

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

Резултати

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

Код

module RBFS
class File
def initialize(data = nil)
@data = data
set_type
end
attr_reader :data_type
attr_reader :data
def set_type
case @data
when NilClass then @data_type = :nil
when Fixnum then @data_type = :number
when Float then @data_type = :number
when Symbol then @data_type = :symbol
when String then @data_type = :string
else @data_type = :boolean
end
end
def data=(new_data)
@data = new_data
set_type
end
def serialize
data_type.to_s + ':' + data.to_s
end
def self.parse(string_data)
word_array = string_data.split(":")
case word_array[0]
when "boolean" then File.new Utils.to_bool(word_array[1])
when "number" then File.new Utils.convert_to_number(word_array[1])
when "symbol" then File.new word_array[1].to_sym
when "string" then File.new word_array[1]
else File.new
end
end
end
class Directory
def initialize
@files = {}
@directories = {}
end
attr_reader :files, :directories
def add_file(name, file)
unless name.include? ":"
@files[name] = file
end
end
def add_directory(name, directory = RBFS::Directory.new)
unless name.include? "."
@directories[name] = directory
end
end
def [](name)
if(@directories[name] && @files[name])
@directories[name]
elsif @directories[name]
@directories[name]
elsif @files[name]
@files[name]
else
nil
end
end
def serialize
serialized = files.size.to_s + ":"
files.each do |name, file|
serialized += name + ":" + file.serialize.length.to_s + ':' + file.serialize
end
serialized += directories.length.to_s + ":"
directories.each do |name, directory|
s = directory.serialize
serialized += name + ":" + s.length.to_s + ":" + s
end
serialized
end
def self.parse(string)
word_array = string.split(':', 2)
file_count, iter, temp = word_array[0].to_i, 0, word_array[1]
dir = RBFS::Directory.new
arr = self.file_helper temp, iter, file_count, dir
dir, temp = arr[0], arr[1]
dir_count, iter = temp.split(":", 2)[0].to_i, 0
temp = temp.split(":", 2)[1]
dir = self.dir_helper temp, iter, dir_count, dir
end
def self.file_helper (string, iter, file_count, dir)
while iter < file_count
file_name, iter = string.split(':', 2)[0], iter + 1
file_len = string.split(':', 2)[1].split(':', 2)[0].to_i
file = string.split(':', 2)[1].split(':', 2)[1].slice!(0...file_len)
len = string.split(':', 2)[1].split(':', 2)[1].length
dir.add_file(file_name, RBFS::File.parse(file))
string = string.split(':', 2)[1].split(':', 2)[1].slice(file_len...len)
end
[dir, string]
end
def self.dir_helper(string, iter, dir_count, dir)
while iter < dir_count
dir_name, iter = string.split(":", 2)[0] , iter + 1
arr = string.split(":", 2)[1].split(":", 2)
dir_len, len, new_dir = arr[0].to_i, arr[1].length
new_dir, string= arr[1].slice(0...dir_len), arr[1].slice(dir_len..len)
dir2 = RBFS::Directory.parse(new_dir)
dir.add_directory(dir_name, dir2)
end
dir
end
end
class Utils
def self.is_float?(string)
string.include? "."
end
def self.to_bool(string)
return true if string == "true"
false if string == "false"
end
def self.convert_to_number(string)
(self.is_float? string )? string.to_f : string.to_i
end
end
end

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

............................F...............

Failures:

  1) RBFS File data type string can parse a string with colons
     Failure/Error: expect(file.data     ).to eq 'Hay :)'
       
       expected: "Hay :)"
            got: "Hay "
       
       (compared using ==)
     # /tmp/d20141111-26053-1vs06cv/spec.rb:257: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.04361 seconds
44 examples, 1 failure

Failed examples:

rspec /tmp/d20141111-26053-1vs06cv/spec.rb:254 # RBFS File data type string can parse a string with colons

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

Любомир обнови решението на 09.11.2014 21:44 (преди около 10 години)

+module RBFS
+ class File
+ def initialize(data = nil)
+ @data = data
+ set_type
+ end
+
+ attr_reader :data_type
+
+ attr_reader :data
+
+ def set_type
+ case @data
+ when NilClass then @data_type = :nil
+ when Fixnum then @data_type = :number
+ when Float then @data_type = :number
+ when Symbol then @data_type = :symbol
+ when String then @data_type = :string
+ else @data_type = :boolean
+ end
+ end
+
+ def data=(new_data)
+ @data = new_data
+ set_type
+ end
+
+ def serialize
+ data_type.to_s + ':' + data.to_s
+ end
+
+ def self.parse(string_data)
+ word_array = string_data.split(":")
+ case word_array[0]
+ when "boolean" then File.new Utils.to_bool(word_array[1])
+ when "number" then File.new Utils.convert_to_number(word_array[1])
+ when "symbol" then File.new word_array[1].to_sym
+ when "string" then File.new word_array[1]
+ else File.new
+ end
+ end
+ end
+
+ class Directory
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ attr_reader :files, :directories
+
+ def add_file(name, file)
+ unless name.include? ":"
+ @files[name] = file
+ end
+ end
+
+ def add_directory(name, directory = RBFS::Directory.new)
+ unless name.include? "."
+ @directories[name] = directory
+ end
+ end
+
+ def [](name)
+ if(@directories[name] && @files[name])
+ @directories[name]
+ elsif @directories[name]
+ @directories[name]
+ elsif @files[name]
+ @files[name]
+ else
+ nil
+ end
+ end
+
+ def serialize
+ serialized = files.size.to_s + ":"
+ files.each do |name, file|
+ serialized += name + ":" + file.serialize.length.to_s + ':' + file.serialize
+ end
+ serialized += directories.length.to_s + ":"
+ directories.each do |name, directory|
+ s = directory.serialize
+ serialized += name + ":" + s.length.to_s + ":" + s
+ end
+ serialized
+ end
+
+ def self.parse(string)
+ word_array = string.split(':', 2)
+ file_count, iter, temp = word_array[0].to_i, 0, word_array[1]
+ dir = RBFS::Directory.new
+ arr = self.file_helper temp, iter, file_count, dir
+ dir, temp = arr[0], arr[1]
+ dir_count, iter = temp.split(":", 2)[0].to_i, 0
+ temp = temp.split(":", 2)[1]
+ dir = self.dir_helper temp, iter, dir_count, dir
+ end
+
+ def self.file_helper (string, iter, file_count, dir)
+ while iter < file_count
+ file_name, iter = string.split(':', 2)[0], iter + 1
+ file_len = string.split(':', 2)[1].split(':', 2)[0].to_i
+ file = string.split(':', 2)[1].split(':', 2)[1].slice!(0...file_len)
+ len = string.split(':', 2)[1].split(':', 2)[1].length
+ dir.add_file(file_name, RBFS::File.parse(file))
+ string = string.split(':', 2)[1].split(':', 2)[1].slice(file_len...len)
+ end
+ [dir, string]
+ end
+
+ def self.dir_helper(string, iter, dir_count, dir)
+ while iter < dir_count
+ dir_name, iter = string.split(":", 2)[0] , iter + 1
+ arr = string.split(":", 2)[1].split(":", 2)
+ dir_len, len, new_dir = arr[0].to_i, arr[1].length
+ new_dir, string= arr[1].slice(0...dir_len), arr[1].slice(dir_len..len)
+ dir2 = RBFS::Directory.parse(new_dir)
+ dir.add_directory(dir_name, dir2)
+ end
+ dir
+ end
+ end
+
+ class Utils
+
+ def self.is_float?(string)
+ string.include? "."
+ end
+
+ def self.to_bool(string)
+ return true if string == "true"
+ false if string == "false"
+ end
+
+ def self.convert_to_number(string)
+ (self.is_float? string )? string.to_f : string.to_i
+ end
+ end
+end

Любомир обнови решението на 09.11.2014 22:14 (преди около 10 години)

module RBFS
class File
def initialize(data = nil)
@data = data
set_type
end
attr_reader :data_type
attr_reader :data
def set_type
case @data
when NilClass then @data_type = :nil
when Fixnum then @data_type = :number
when Float then @data_type = :number
when Symbol then @data_type = :symbol
when String then @data_type = :string
else @data_type = :boolean
end
end
def data=(new_data)
@data = new_data
set_type
end
def serialize
data_type.to_s + ':' + data.to_s
end
def self.parse(string_data)
word_array = string_data.split(":")
case word_array[0]
when "boolean" then File.new Utils.to_bool(word_array[1])
when "number" then File.new Utils.convert_to_number(word_array[1])
when "symbol" then File.new word_array[1].to_sym
when "string" then File.new word_array[1]
else File.new
end
end
end
class Directory
def initialize
@files = {}
@directories = {}
end
attr_reader :files, :directories
def add_file(name, file)
unless name.include? ":"
@files[name] = file
end
end
def add_directory(name, directory = RBFS::Directory.new)
unless name.include? "."
@directories[name] = directory
end
end
def [](name)
if(@directories[name] && @files[name])
@directories[name]
elsif @directories[name]
@directories[name]
elsif @files[name]
@files[name]
else
nil
end
end
def serialize
serialized = files.size.to_s + ":"
files.each do |name, file|
serialized += name + ":" + file.serialize.length.to_s + ':' + file.serialize
end
serialized += directories.length.to_s + ":"
directories.each do |name, directory|
s = directory.serialize
serialized += name + ":" + s.length.to_s + ":" + s
end
serialized
end
def self.parse(string)
word_array = string.split(':', 2)
file_count, iter, temp = word_array[0].to_i, 0, word_array[1]
dir = RBFS::Directory.new
arr = self.file_helper temp, iter, file_count, dir
dir, temp = arr[0], arr[1]
dir_count, iter = temp.split(":", 2)[0].to_i, 0
temp = temp.split(":", 2)[1]
dir = self.dir_helper temp, iter, dir_count, dir
end
def self.file_helper (string, iter, file_count, dir)
while iter < file_count
file_name, iter = string.split(':', 2)[0], iter + 1
file_len = string.split(':', 2)[1].split(':', 2)[0].to_i
file = string.split(':', 2)[1].split(':', 2)[1].slice!(0...file_len)
len = string.split(':', 2)[1].split(':', 2)[1].length
dir.add_file(file_name, RBFS::File.parse(file))
string = string.split(':', 2)[1].split(':', 2)[1].slice(file_len...len)
end
[dir, string]
end
def self.dir_helper(string, iter, dir_count, dir)
while iter < dir_count
dir_name, iter = string.split(":", 2)[0] , iter + 1
arr = string.split(":", 2)[1].split(":", 2)
dir_len, len, new_dir = arr[0].to_i, arr[1].length
new_dir, string= arr[1].slice(0...dir_len), arr[1].slice(dir_len..len)
dir2 = RBFS::Directory.parse(new_dir)
dir.add_directory(dir_name, dir2)
end
dir
end
end
class Utils
-
def self.is_float?(string)
string.include? "."
end
def self.to_bool(string)
return true if string == "true"
false if string == "false"
end
def self.convert_to_number(string)
(self.is_float? string )? string.to_f : string.to_i
end
end
end