Решение на Трета задача от Диана Генева

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

Към профила на Диана Генева

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def initialize(data = nil)
@data = data
end
def data_type
case(@data)
when String then :string
when Symbol then :symbol
when Numeric then :number
when TrueClass, FalseClass then :boolean
when nil then :nil
end
end
def serialize
data_type.to_s + ":" + @data.to_s
end
def File.parse_number(number_string)
if number_string.include? "."
File.new(number_string.to_f)
else
File.new(number_string.to_i)
end
end
def File.parse(string_data)
deserialize = string_data.split(":",2)
case(deserialize.first)
when "nil" then File.new
when "number" then parse_number(deserialize.last)
when "string" then File.new(deserialize.last)
when "symbol" then File.new(deserialize.last.gsub(":", "").to_sym)
when "boolean" then File.new(deserialize.last == "true")
end
end
end
class Directory
attr_reader :files, :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory=Directory.new)
@directories[name] = directory
end
def [](name)
if @directories.include?(name)
@directories.fetch(name, nil)
else
@files.fetch(name, nil)
end
end
def serialize
serialized = @files.size.to_s + ":"
@files.each do |key,value|
serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
end
serialized += @directories.size.to_s + ":"
@directories.each do |key,value|
serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
end
serialized
end
def Directory.parse_file(string_data, directory, parse)
number = string_data.split(":", 2).first.to_i
parse = string_data.split(":", 2).last
number.times do
parse = parse.split(":", 3)
directory.add_file(parse[0], File.parse(parse[2].slice!(0...(parse[1].to_i))))
parse = parse[2]
end
parse
end
def Directory.parse_directory(parse, directory)
number = parse.split(":", 2).first.to_i
parse = parse.split(":", 2).last
number.times do
parse = parse.split(":", 3)
directory.add_directory(parse[0],
Directory.parse(parse[2].slice!(0...(parse[1].to_i))))
parse = parse[2]
end
end
def Directory.parse(string_data)
directory = Directory.new
parse = ""
parse = parse_file(string_data, directory,parse)
parse_directory(parse, directory)
directory
end
end
end

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

............................................

Finished in 0.04234 seconds
44 examples, 0 failures

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

Диана обнови решението на 06.11.2014 22:04 (преди около 10 години)

+module RBFS
+ class File
+ attr_accessor :data
+ def initialize(data = nil)
+ @data = data
+ end
+
+ def data_type
+ case(@data)
+ when String then :string
+ when Symbol then :symbol
+ when Numeric then :number
+ when TrueClass, FalseClass then :boolean
+ when nil then :nil
+ end
+ end
+
+ def serialize
+ data_type.to_s + ":" + @data.to_s
+ end
+
+ def parse_number(number_string)
+ if number_string.include? "."
+ File.new(deserialize.last.to_f)
+ else
+ File.new(deserialize.last.to_i)
+ end
+ end
+
+ def File.parse(string_data)
+ deserialize = string_data.split(":",2)
+ case(deserialize.first)
+ when "nil" then File.new
+ when "number" then parse_number(deserialize.last)
+ when "string" then File.new(deserialize.last)
+ when "symbol" then File.new(deserialize.last.gsub(":", "").to_sym)
+ when "boolean" then File.new(deserialize.last == "true")
+ end
+ end
+
+ end
+
+ class Directory
+ attr_reader :files, :directories
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory=Directory.new)
+ @directories[name] = directory
+ end
+
+ def [](name)
+ if @files.include?(name)
+ @files.fetch(name)
+ else
+ @directories.fetch(name, nil)
+ end
+
+ end
+
+ def serialize
+ serialized = @files.size.to_s + ":"
+ @files.each do |key,value|
+ serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
+ end
+ serialized += @directories.size.to_s + ":"
+ @directories.each do |key,value|
+ serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
+ end
+ serialized
+ end
+
+ def Directory.parse(string_data)
+ end
+
+
+ end
+end

Диана обнови решението на 06.11.2014 23:51 (преди около 10 години)

module RBFS
class File
attr_accessor :data
def initialize(data = nil)
@data = data
end
def data_type
case(@data)
when String then :string
when Symbol then :symbol
when Numeric then :number
when TrueClass, FalseClass then :boolean
when nil then :nil
end
end
def serialize
data_type.to_s + ":" + @data.to_s
end
- def parse_number(number_string)
+ def File.parse_number(number_string)
if number_string.include? "."
- File.new(deserialize.last.to_f)
+ File.new(number_string.to_f)
else
- File.new(deserialize.last.to_i)
+ File.new(number_string.to_i)
end
end
def File.parse(string_data)
deserialize = string_data.split(":",2)
case(deserialize.first)
when "nil" then File.new
when "number" then parse_number(deserialize.last)
when "string" then File.new(deserialize.last)
when "symbol" then File.new(deserialize.last.gsub(":", "").to_sym)
when "boolean" then File.new(deserialize.last == "true")
end
end
end
class Directory
attr_reader :files, :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory=Directory.new)
@directories[name] = directory
end
def [](name)
if @files.include?(name)
@files.fetch(name)
else
@directories.fetch(name, nil)
end
end
def serialize
serialized = @files.size.to_s + ":"
@files.each do |key,value|
serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
end
serialized += @directories.size.to_s + ":"
@directories.each do |key,value|
serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
end
serialized
end
- def Directory.parse(string_data)
- end
+ def Directory.parse_file(string_data, directory, parse)
+ number = string_data.split(":", 2).first.to_i
+ parse = string_data.split(":", 2).last
+ number.times do
+ parse = parse.split(":", 3)
+ directory.add_file(parse[0], File.parse(parse[2].slice!(0...(parse[1].to_i))))
+ parse = parse[2]
+ end
+ parse
+ end
- end
+ def Directory.parse_directory(parse, directory)
+ number = parse.split(":", 2).first.to_i
+ parse = parse.split(":", 2).last
+
+ number.times do
+ parse = parse.split(":", 3)
+ directory.add_directory(parse[0],
+ Directory.parse(parse[2].slice!(0...(parse[1].to_i))))
+ parse = parse[2]
+ end
+ end
+
+ def Directory.parse(string_data)
+ directory = Directory.new
+ parse = ""
+ parse = parse_file(string_data, directory,parse)
+ parse_directory(parse, directory)
+ directory
+ end
+ end
end

Диана обнови решението на 06.11.2014 23:52 (преди около 10 години)

module RBFS
- class File
- attr_accessor :data
- def initialize(data = nil)
- @data = data
- end
+ class File
+ attr_accessor :data
+ def initialize(data = nil)
+ @data = data
+ end
- def data_type
- case(@data)
- when String then :string
- when Symbol then :symbol
- when Numeric then :number
- when TrueClass, FalseClass then :boolean
- when nil then :nil
- end
- end
+ def data_type
+ case(@data)
+ when String then :string
+ when Symbol then :symbol
+ when Numeric then :number
+ when TrueClass, FalseClass then :boolean
+ when nil then :nil
+ end
+ end
- def serialize
- data_type.to_s + ":" + @data.to_s
- end
+ def serialize
+ data_type.to_s + ":" + @data.to_s
+ end
- def File.parse_number(number_string)
- if number_string.include? "."
- File.new(number_string.to_f)
- else
- File.new(number_string.to_i)
- end
- end
+ def File.parse_number(number_string)
+ if number_string.include? "."
+ File.new(number_string.to_f)
+ else
+ File.new(number_string.to_i)
+ end
+ end
- def File.parse(string_data)
- deserialize = string_data.split(":",2)
- case(deserialize.first)
- when "nil" then File.new
- when "number" then parse_number(deserialize.last)
- when "string" then File.new(deserialize.last)
- when "symbol" then File.new(deserialize.last.gsub(":", "").to_sym)
- when "boolean" then File.new(deserialize.last == "true")
- end
- end
+ def File.parse(string_data)
+ deserialize = string_data.split(":",2)
+ case(deserialize.first)
+ when "nil" then File.new
+ when "number" then parse_number(deserialize.last)
+ when "string" then File.new(deserialize.last)
+ when "symbol" then File.new(deserialize.last.gsub(":", "").to_sym)
+ when "boolean" then File.new(deserialize.last == "true")
+ end
+ end
- end
+ end
- class Directory
- attr_reader :files, :directories
+ class Directory
+ attr_reader :files, :directories
- def initialize
- @files = {}
- @directories = {}
- end
+ def initialize
+ @files = {}
+ @directories = {}
+ end
- def add_file(name, file)
- @files[name] = file
- end
+ def add_file(name, file)
+ @files[name] = file
+ end
- def add_directory(name, directory=Directory.new)
- @directories[name] = directory
- end
+ def add_directory(name, directory=Directory.new)
+ @directories[name] = directory
+ end
- def [](name)
- if @files.include?(name)
- @files.fetch(name)
- else
- @directories.fetch(name, nil)
- end
+ def [](name)
+ if @files.include?(name)
+ @files.fetch(name)
+ else
+ @directories.fetch(name, nil)
+ end
- end
+ end
- def serialize
- serialized = @files.size.to_s + ":"
- @files.each do |key,value|
- serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
- end
- serialized += @directories.size.to_s + ":"
- @directories.each do |key,value|
- serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
- end
- serialized
- end
+ def serialize
+ serialized = @files.size.to_s + ":"
+ @files.each do |key,value|
+ serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
+ end
+ serialized += @directories.size.to_s + ":"
+ @directories.each do |key,value|
+ serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
+ end
+ serialized
+ end
def Directory.parse_file(string_data, directory, parse)
number = string_data.split(":", 2).first.to_i
parse = string_data.split(":", 2).last
number.times do
parse = parse.split(":", 3)
directory.add_file(parse[0], File.parse(parse[2].slice!(0...(parse[1].to_i))))
parse = parse[2]
end
parse
end
def Directory.parse_directory(parse, directory)
number = parse.split(":", 2).first.to_i
parse = parse.split(":", 2).last
number.times do
parse = parse.split(":", 3)
directory.add_directory(parse[0],
Directory.parse(parse[2].slice!(0...(parse[1].to_i))))
parse = parse[2]
end
end
def Directory.parse(string_data)
directory = Directory.new
parse = ""
parse = parse_file(string_data, directory,parse)
parse_directory(parse, directory)
directory
end
end
end

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

module RBFS
class File
attr_accessor :data
def initialize(data = nil)
@data = data
end
def data_type
case(@data)
when String then :string
when Symbol then :symbol
when Numeric then :number
when TrueClass, FalseClass then :boolean
when nil then :nil
end
end
def serialize
data_type.to_s + ":" + @data.to_s
end
def File.parse_number(number_string)
if number_string.include? "."
File.new(number_string.to_f)
else
File.new(number_string.to_i)
end
end
def File.parse(string_data)
deserialize = string_data.split(":",2)
case(deserialize.first)
when "nil" then File.new
when "number" then parse_number(deserialize.last)
when "string" then File.new(deserialize.last)
when "symbol" then File.new(deserialize.last.gsub(":", "").to_sym)
when "boolean" then File.new(deserialize.last == "true")
end
end
end
class Directory
attr_reader :files, :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory=Directory.new)
@directories[name] = directory
end
def [](name)
- if @files.include?(name)
- @files.fetch(name)
- else
+ if @directories.include?(name)
@directories.fetch(name, nil)
+ else
+ @files.fetch(name, nil)
end
-
end
def serialize
serialized = @files.size.to_s + ":"
@files.each do |key,value|
serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
end
serialized += @directories.size.to_s + ":"
@directories.each do |key,value|
serialized<<key<<":"<<value.serialize.length.to_s<<":"<<value.serialize
end
serialized
end
def Directory.parse_file(string_data, directory, parse)
number = string_data.split(":", 2).first.to_i
parse = string_data.split(":", 2).last
number.times do
parse = parse.split(":", 3)
directory.add_file(parse[0], File.parse(parse[2].slice!(0...(parse[1].to_i))))
parse = parse[2]
end
parse
end
def Directory.parse_directory(parse, directory)
number = parse.split(":", 2).first.to_i
parse = parse.split(":", 2).last
number.times do
parse = parse.split(":", 3)
directory.add_directory(parse[0],
Directory.parse(parse[2].slice!(0...(parse[1].to_i))))
parse = parse[2]
end
end
def Directory.parse(string_data)
directory = Directory.new
parse = ""
parse = parse_file(string_data, directory,parse)
parse_directory(parse, directory)
directory
end
end
end