Решение на Трета задача от Стилиян Стоянов

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

Към профила на Стилиян Стоянов

Резултати

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

Код

module RBFS
def to_boolean(str)
str == 'true'
end
class RBFS::File
attr_accessor :data
def File.parse(string_data)
type_and_data = string_data.partition(":")
file_type = File.get_type(type_and_data[0])
file_content = File.convert_data( type_and_data[2], file_type)
parsed_file = RBFS::File.new(file_content)
end
def File.get_type(string_data)
case string_data
when "numbers" then :numbers
when "string" then :string
when "boolean" then :boolean
when "symbol" then :symbol
when "nil" then :nil
end
end
def File.convert_data(string_data, type)
case type
when :numbers then string_data.to_f
when :string then string_data
when :boolean then to_boolean(string_data)
when :symbol then string_data.to_sym
else
nil
end
end
def initialize( data_to_store_in_file = '')
@data = data_to_store_in_file
end
def data_type
case
when @data.class == Fixnum, @data.class == Float then :number
when @data.class == String then :string
when @data.class == Symbol then :symbol
when @data.class == TrueClass, @data.class == FalseClass then :boolean
else
:nil
end
end
def serialize
self.data_type.to_s + ":" + @data.to_s
end
end
class RBFS::Directory
attr_reader :files
attr_reader :directories
def initialize
@directories = {}
@files = {}
end
def add_file(name, file)
files[name] = file
end
def add_directory(name, directory = RBFS::Directory.new)
directories[name] = directory
end
def [](name)
if directories[name] != nil
directories[name]
else
files[name]
end
end
def serialize()
serialized_files = serialized_hash(@files) + serialized_hash(@directories)
end
def serialized_hash(hash)
serialized_files = hash.size.to_s + ":"
hash.each do |key, value|
srlzd_elem = value.serialize
file_size = srlzd_elem.size.to_s
serialized_files = serialized_files + key + ":" + file_size + ":" + srlzd_elem
end
serialized_files
end
def Directory.parse(string_data)
dir = Directory.new
partitioned = string_data.partition(":")
count_of_files, counter, serialized_data = partitioned[0].to_i, 0, partitioned[2]
serialized_data = dir.parse_all_files(serialized_data, count_of_files, counter)
partitioned = serialized_data.partition(":")
count_of_dirs, counter, serialized_data = partitioned[0].to_i, 0, partitioned[2]
dir.parse_all_directories(serialized_data, count_of_dirs, counter)
end
def parse_all_files(serialized_data, count_of_files, counter)
while(counter < count_of_files )
file_name = serialized_data.partition(":")
serialized_data = file_name[2].partition(":")
subdir = serialized_data[2].slice!(0, serialized_data[0].to_i)
@files[file_name[0]] = RBFS::File.parse(subdir)
counter, serialized_data = counter + 1, serialized_data[2]
end
serialized_data
end
def parse_all_directories(serialized_data, count_of_dirs, counter)
while(counter < count_of_dirs)
file_name = serialized_data.partition(":")
serialized_data = file_name[2].partition(":")
subdir = serialized_data[2].slice!(0, serialized_data[0].to_i)
@directories[file_name[0]] = RBFS::Directory.parse(subdir)
counter = counter + 1
end
self
end
end
end

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

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

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:')
     ArgumentError:
       wrong number of arguments (1 for 0)
     # /tmp/d20141111-26053-1kieuak/solution.rb:114:in `partition'
     # /tmp/d20141111-26053-1kieuak/solution.rb:114:in `parse_all_directories'
     # /tmp/d20141111-26053-1kieuak/solution.rb:99:in `parse'
     # /tmp/d20141111-26053-1kieuak/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 `to_boolean' for RBFS::File:Class
     # /tmp/d20141111-26053-1kieuak/solution.rb:29:in `convert_data'
     # /tmp/d20141111-26053-1kieuak/solution.rb:12:in `parse'
     # /tmp/d20141111-26053-1kieuak/solution.rb:107:in `parse_all_files'
     # /tmp/d20141111-26053-1kieuak/solution.rb:96:in `parse'
     # /tmp/d20141111-26053-1kieuak/solution.rb:117:in `parse_all_directories'
     # /tmp/d20141111-26053-1kieuak/solution.rb:99:in `parse'
     # /tmp/d20141111-26053-1kieuak/solution.rb:117:in `parse_all_directories'
     # /tmp/d20141111-26053-1kieuak/solution.rb:99:in `parse'
     # /tmp/d20141111-26053-1kieuak/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 has nil as initial data
     Failure/Error: expect(file.data).to eq nil
       
       expected: nil
            got: ""
       
       (compared using ==)
     # /tmp/d20141111-26053-1kieuak/spec.rb:203:in `block (3 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 number can be parsed
     Failure/Error: expect(file.data     ).to eq 1234
       
       expected: 1234
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-1kieuak/spec.rb:295: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 float number can be parsed
     Failure/Error: expect(file.data     ).to eq 3.14
       
       expected: 3.14
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-1kieuak/spec.rb:314: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)>'

  6) RBFS File data type boolean true can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:true')
     NoMethodError:
       undefined method `to_boolean' for RBFS::File:Class
     # /tmp/d20141111-26053-1kieuak/solution.rb:29:in `convert_data'
     # /tmp/d20141111-26053-1kieuak/solution.rb:12:in `parse'
     # /tmp/d20141111-26053-1kieuak/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)>'

  7) RBFS File data type boolean false can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:false')
     NoMethodError:
       undefined method `to_boolean' for RBFS::File:Class
     # /tmp/d20141111-26053-1kieuak/solution.rb:29:in `convert_data'
     # /tmp/d20141111-26053-1kieuak/solution.rb:12:in `parse'
     # /tmp/d20141111-26053-1kieuak/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.04216 seconds
44 examples, 7 failures

Failed examples:

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

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

Стилиян обнови решението на 10.11.2014 01:44 (преди около 10 години)

+module RBFS
+ def to_boolean(str)
+ str == 'true'
+ end
+
+ class RBFS::File
+ attr_accessor :data
+ attr_accessor :data_type
+
+ def File.parse(string_data)
+ type_and_data = string_data.partition(":")
+ file_type = File.get_type(type_and_data[0])
+ file_content = File.convert_data( type_and_data[2], file_type)
+ parsed_file = RBFS::File.new(file_content)
+ end
+ def File.get_type(string_data)
+ case string_data
+ when "numbers" then :numbers
+ when "string" then :string
+ when "boolean" then :boolean
+ when "symbol" then :symbol
+ when "nil" then :nil
+ end
+ end
+
+ def File.convert_data(string_data, type)
+ case type
+ when :numbers then string_data.to_f
+ when :string then string_data
+ when :boolean then to_boolean(string_data)
+ when :symbol then string_data.to_sym
+ else
+ nil
+ end
+ end
+
+ def initialize( data_to_store_in_file )
+ @data = data_to_store_in_file
+ @data_type = self.data_type
+ end
+
+ def data_type
+ case
+ when @data.class == Fixnum, @data.class == Float then :number
+ when @data.class == String then :string
+ when @data.class == Symbol then :symbol
+ when @data.class == TrueClass, @data.class == FalseClass then :boolean
+ else
+ :nil
+ end
+ end
+
+ def serialize
+ @data_type.to_s + ":" + @data.to_s
+ end
+ end
+
+ class RBFS::Directory
+ attr_reader :files
+ attr_reader :directories
+ def initialize
+ @directories = {}
+ @files = {}
+ end
+
+ def add_file(name, file)
+ files[name] = file
+ end
+
+ def add_directory(name, directory)
+ directories[name] = directory
+ end
+
+ def [](name)
+ if directories[name] != nil
+ directories[name]
+ else
+ files[name]
+ end
+ end
+ def serialize()
+ serialized_files = serialized_hash(@files) + serialized_hash(@directories)
+ end
+ def serialized_hash(hash)
+ serialized_files = @directories.size.to_s + ":"
+ hash.each do |key, value|
+ srld_elem = value.serialize
+ file_size = srld_elem.size.to_s
+ serialized_files = serialized_files + key + ":" + file_size + ":" + srld_elem
+ end
+ serialized_files
+ end
+ end
+end

Стилиян обнови решението на 10.11.2014 12:19 (преди около 10 години)

module RBFS
def to_boolean(str)
str == 'true'
end
class RBFS::File
attr_accessor :data
- attr_accessor :data_type
def File.parse(string_data)
type_and_data = string_data.partition(":")
file_type = File.get_type(type_and_data[0])
file_content = File.convert_data( type_and_data[2], file_type)
parsed_file = RBFS::File.new(file_content)
end
def File.get_type(string_data)
case string_data
when "numbers" then :numbers
when "string" then :string
when "boolean" then :boolean
when "symbol" then :symbol
when "nil" then :nil
end
end
def File.convert_data(string_data, type)
case type
when :numbers then string_data.to_f
when :string then string_data
when :boolean then to_boolean(string_data)
when :symbol then string_data.to_sym
else
nil
end
end
- def initialize( data_to_store_in_file )
+ def initialize( data_to_store_in_file = '')
@data = data_to_store_in_file
- @data_type = self.data_type
end
def data_type
case
when @data.class == Fixnum, @data.class == Float then :number
when @data.class == String then :string
when @data.class == Symbol then :symbol
when @data.class == TrueClass, @data.class == FalseClass then :boolean
else
:nil
end
end
def serialize
- @data_type.to_s + ":" + @data.to_s
+ self.data_type.to_s + ":" + @data.to_s
end
end
class RBFS::Directory
attr_reader :files
attr_reader :directories
def initialize
@directories = {}
@files = {}
end
def add_file(name, file)
files[name] = file
end
- def add_directory(name, directory)
+ def add_directory(name, directory = RBFS::Directory.new)
directories[name] = directory
end
def [](name)
if directories[name] != nil
directories[name]
else
files[name]
end
end
def serialize()
serialized_files = serialized_hash(@files) + serialized_hash(@directories)
end
def serialized_hash(hash)
- serialized_files = @directories.size.to_s + ":"
+ serialized_files = hash.size.to_s + ":"
hash.each do |key, value|
- srld_elem = value.serialize
- file_size = srld_elem.size.to_s
- serialized_files = serialized_files + key + ":" + file_size + ":" + srld_elem
+ srlzd_elem = value.serialize
+ file_size = srlzd_elem.size.to_s
+ serialized_files = serialized_files + key + ":" + file_size + ":" + srlzd_elem
end
serialized_files
end
+
+ def Directory.parse(string_data)
+ dir = Directory.new
+ partitioned = string_data.partition(":")
+ count_of_files, counter, serialized_data = partitioned[0].to_i, 0, partitioned[2]
+ serialized_data = dir.parse_all_files(serialized_data, count_of_files, counter)
+ partitioned = serialized_data.partition(":")
+ count_of_dirs, counter, serialized_data = partitioned[0].to_i, 0, partitioned[2]
+ dir.parse_all_directories(serialized_data, count_of_dirs, counter)
+ end
+
+ def parse_all_files(serialized_data, count_of_files, counter)
+ while(counter < count_of_files )
+ file_name = serialized_data.partition(":")
+ serialized_data = file_name[2].partition(":")
+ subdir = serialized_data[2].slice!(0, serialized_data[0].to_i)
+ @files[file_name[0]] = RBFS::File.parse(subdir)
+ counter, serialized_data = counter + 1, serialized_data[2]
+ end
+ serialized_data
+ end
+ def parse_all_directories(serialized_data, count_of_dirs, counter)
+ while(counter < count_of_dirs)
+ file_name = serialized_data.partition(":")
+ serialized_data = file_name[2].partition(":")
+ subdir = serialized_data[2].slice!(0, serialized_data[0].to_i)
+ @directories[file_name[0]] = RBFS::Directory.parse(subdir)
+ counter = counter + 1
+ end
+ self
+ end
+
end
end