Решение на Трета задача от Бетина Иванова

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

Към профила на Бетина Иванова

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def initialize(object = nil)
@data = object
end
def data_type
case @data.class.to_s
when 'String' then :string
when 'NilClass' then :nil
when 'Symbol' then :symbol
when 'Fixnum' then :number
when 'Float' then :number
else :boolean
end
end
def serialize
format("%s:%s", data_type.to_s, @data.to_s)
end
def self.parse(string_data)
data = string_data.partition(":").last
case string_data.partition(":").first
when 'nil' then File.new nil
when 'string' then File.new data
when 'symbol' then File.new data.to_sym
when 'number' then parse_helper(data)
else
parse_helper1(data)
end
end
def parse_helper(data)
if data.include? '.'
File.new data.to_f
else
File.new data.to_i end
end
def parse_helper1(data)
if data == 'true'
File.new true
else
File.new false
end
end
end
class Directory
def initialize
@directory = []
end
def add_file(name, file)
@directory << [name, file]
end
def add_directory(name, directory = Directory.new)
@directory << [name, directory]
end
def [](name)
help = []
help = @directory.select { | (first, second) | first == name }
case help.size
when 1 then help.flatten.last
when 2 then Helper.getter(help)
else
nil
end
end
def files
hash = {}
data = @directory.select { | (first, last) | last.instance_of? File }
data.each do | (first, last) |
hash[first] = last
end
hash
end
def directories
hash = {}
data = @directory.select { | (first, last) | (last.is_a? Directory or last == []) }
data.each do | (first, last) |
hash[first] = last
end
hash
end
def serialize
result = ""
if files.empty? & directories.empty?
result += "0:0:"
else
result = Helper.serializing(result, files, directories)
end
result
end
def self.parse(string_data)
directory = Directory.new
if string_data != '0:0:'
array = string_data.partition(':')
string_data = Helper.parsing(array, string_data, directory)
Helper.parsing_function(array, string_data, directory)
end
directory
end
end
class Helper
class << self
def parsing(array, string_data, directory)
string_data = array.last
array.first.to_i.times do
string_data = self.function(array, string_data, directory)
end
string_data
end
def function(array, string_data, directory)
array = string_data.partition(':')
file_name = array.first
array = array.last.partition(":")
count = array.first.to_i
directory.add_file(file_name, File.parse(array.last.slice(0, count)))
array = array.last.partition(":")
string_data = array.last.slice(count - array.first.size - 1, array.last.size)
string_data
end
def parsing_function(array, string_data, directory)
array = string_data.partition(":")
array.first.to_i.times do
array = array.last.partition(":")
name = array.first
array = array.last.partition(":")
limit = array.first.to_i
directory.add_directory(name, Directory.parse(array.last.slice(0, limit)))
end
end
def serializing(result, keys, data)
result = result + (keys.size.to_s + ':')
result, count = self.helper(result, keys.size, keys) + (data.size.to_s + ':'), 0
while(count < data.size)
data.values[count] = self.checker(data.values[count])
first, last = data.keys[count], data.values[count]
result += (first.to_s + ':' + last.serialize.length.to_s + ':' + last.serialize)
count += 1
end
result
end
def helper(result, size, hash)
count = 0
while (count < size)
key, value = hash.keys[count], hash.values[count]
result += (key.to_s + ':' + value.serialize.length.to_s + ':' + value.serialize)
count += 1
end
result
end
def checker(directory)
if (directory.files.empty? and directory.files.empty?)
directory = Directory.new
end
directory
end
def getter(help)
help.select{ | (first, second) | second.instance_of? Directory }.flatten.last
end
end
end
end

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

............FF....................F..F..F..F

Failures:

  1) RBFS Directory serialization ::parse can parse directory trees without files
     Failure/Error: expect(parsed_directory['dir3']        ).to be_an RBFS::Directory
       expected nil to be a kind of RBFS::Directory
     # /tmp/d20141111-26053-95c6dj/spec.rb:121: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 `parse_helper1' for RBFS::File:Class
     # /tmp/d20141111-26053-95c6dj/solution.rb:32:in `parse'
     # /tmp/d20141111-26053-95c6dj/solution.rb:131:in `function'
     # /tmp/d20141111-26053-95c6dj/solution.rb:121:in `block in parsing'
     # /tmp/d20141111-26053-95c6dj/solution.rb:120:in `times'
     # /tmp/d20141111-26053-95c6dj/solution.rb:120:in `parsing'
     # /tmp/d20141111-26053-95c6dj/solution.rb:109:in `parse'
     # /tmp/d20141111-26053-95c6dj/solution.rb:144:in `block in parsing_function'
     # /tmp/d20141111-26053-95c6dj/solution.rb:139:in `times'
     # /tmp/d20141111-26053-95c6dj/solution.rb:139:in `parsing_function'
     # /tmp/d20141111-26053-95c6dj/solution.rb:110:in `parse'
     # /tmp/d20141111-26053-95c6dj/solution.rb:144:in `block in parsing_function'
     # /tmp/d20141111-26053-95c6dj/solution.rb:139:in `times'
     # /tmp/d20141111-26053-95c6dj/solution.rb:139:in `parsing_function'
     # /tmp/d20141111-26053-95c6dj/solution.rb:110:in `parse'
     # /tmp/d20141111-26053-95c6dj/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)>'

  3) RBFS File data type number can be parsed
     Failure/Error: file = RBFS::File.parse('number:1234')
     NoMethodError:
       undefined method `parse_helper' for RBFS::File:Class
     # /tmp/d20141111-26053-95c6dj/solution.rb:30:in `parse'
     # /tmp/d20141111-26053-95c6dj/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)>'

  4) RBFS File data type float number can be parsed
     Failure/Error: file = RBFS::File.parse('number:3.14')
     NoMethodError:
       undefined method `parse_helper' for RBFS::File:Class
     # /tmp/d20141111-26053-95c6dj/solution.rb:30:in `parse'
     # /tmp/d20141111-26053-95c6dj/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)>'

  5) RBFS File data type boolean true can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:true')
     NoMethodError:
       undefined method `parse_helper1' for RBFS::File:Class
     # /tmp/d20141111-26053-95c6dj/solution.rb:32:in `parse'
     # /tmp/d20141111-26053-95c6dj/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)>'

  6) RBFS File data type boolean false can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:false')
     NoMethodError:
       undefined method `parse_helper1' for RBFS::File:Class
     # /tmp/d20141111-26053-95c6dj/solution.rb:32:in `parse'
     # /tmp/d20141111-26053-95c6dj/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.04315 seconds
44 examples, 6 failures

Failed examples:

rspec /tmp/d20141111-26053-95c6dj/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-95c6dj/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-95c6dj/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-95c6dj/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-95c6dj/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-95c6dj/spec.rb:350 # RBFS File data type boolean false can be parsed

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

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

+module RBFS
+ class File
+ attr_accessor :data
+
+ def initialize(object = nil)
+ @data = object
+ end
+
+ def data_type
+ case @data.class.to_s
+ when 'String' then :string
+ when 'NilClass' then :nil
+ when 'Symbol' then :symbol
+ when 'Fixnum' then :number
+ when 'Float' then :number
+ else :boolean
+ end
+ end
+
+ def serialize
+ format("%s:%s", data_type.to_s, @data.to_s)
+ end
+
+ def self.parse(string_data)
+ data = string_data.partition(":").last
+ case string_data.partition(":").first
+ when 'nil' then File.new nil
+ when 'string' then File.new data
+ when 'symbol' then File.new data.to_sym
+ when 'number' then parse_helper(data)
+ else
+ parse_helper1(data)
+ end
+ end
+
+ def parse_helper(data)
+ if data.include? '.'
+ File.new data.to_f
+ else
+ File.new data.to_i end
+ end
+
+ def parse_helper1(data)
+ if data == 'true'
+ File.new true
+ else
+ File.new false
+ end
+ end
+ end
+
+
+ class Directory
+ def initialize
+ @directory = []
+ end
+
+ def add_file(name, file)
+ @directory << [name, file]
+ end
+
+ def add_directory(name, directory = Directory.new)
+ @directory << [name, directory]
+ end
+
+ def [](name)
+ help = []
+ help = @directory.select { | (first, second) | first == name }
+ case help.size
+ when 1 then help.flatten.last
+ when 2 then Helper.getter(help)
+ else
+ nil
+ end
+ end
+
+ def files
+ hash = {}
+ data = @directory.select { | (first, last) | last.instance_of? File }
+ data.each do | (first, last) |
+ hash[first] = last
+ end
+ hash
+ end
+
+ def directories
+ hash = {}
+ data = @directory.select { | (first, last) | (last.is_a? Directory or last == []) }
+ data.each do | (first, last) |
+ hash[first] = last
+ end
+ hash
+ end
+
+ def serialize
+ result = ""
+ if files.empty? & directories.empty?
+ result += "0:0:"
+ else
+ result = Helper.serializing(result, files, directories)
+ end
+ result
+ end
+
+ def self.parse(string_data)
+ directory = Directory.new
+ if string_data != '0:0:'
+ array = string_data.partition(':')
+ string_data = Helper.parsing(array, string_data, directory)
+ Helper.parsing_function(array, string_data, directory)
+ end
+ directory
+ end
+ end
+
+ class Helper
+ class << self
+ def parsing(array, string_data, directory)
+ string_data = array.last
+ array.first.to_i.times do
+ string_data = self.function(array, string_data, directory)
+ end
+ string_data
+ end
+
+ def function(array, string_data, directory)
+ array = string_data.partition(':')
+ file_name = array.first
+ array = array.last.partition(":")
+ count = array.first.to_i
+ directory.add_file(file_name, File.parse(array.last.slice(0, count)))
+ array = array.last.partition(":")
+ string_data = array.last.slice(count - array.first.size - 1, array.last.size)
+ string_data
+ end
+
+ def parsing_function(array, string_data, directory)
+ array = string_data.partition(":")
+ array.first.to_i.times do
+ array = array.last.partition(":")
+ name = array.first
+ array = array.last.partition(":")
+ limit = array.first.to_i
+ directory.add_directory(name, Directory.parse(array.last.slice(0, limit)))
+ end
+ end
+
+ def serializing(result, keys, data)
+ result = result + (keys.size.to_s + ':')
+ result, count = self.helper(result, keys.size, keys) + (data.size.to_s + ':'), 0
+ while(count < data.size)
+ data.values[count] = self.checker(data.values[count])
+ first, last = data.keys[count], data.values[count]
+ result += (first.to_s + ':' + last.serialize.length.to_s + ':' + last.serialize)
+ count += 1
+ end
+ result
+ end
+
+ def helper(result, size, hash)
+ count = 0
+ while (count < size)
+ key, value = hash.keys[count], hash.values[count]
+ result += (key.to_s + ':' + value.serialize.length.to_s + ':' + value.serialize)
+ count += 1
+ end
+ result
+ end
+
+ def checker(directory)
+ if (directory.files.empty? and directory.files.empty?)
+ directory = Directory.new
+ end
+ directory
+ end
+
+ def getter(help)
+ help.select{ | (first, second) | second.instance_of? Directory }.flatten.last
+ end
+ end
+ end
+end