Решение на Трета задача от Ангел Ангелов

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

Към профила на Ангел Ангелов

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def initialize(data = nil)
@data = data
end
def data_type
case data
when nil then :nil
when String then :string
when Symbol then :symbol
when Numeric then :number
when true, false then :boolean
end
end
def serialize
data_type.to_s + ':' + data.to_s
end
def self.parse(serial)
type, data = serial.split(':', 2)
self.new case type
when 'nil' then nil
when 'string' then data
when 'boolean' then boolean(data)
when 'number' then number(data)
when 'symbol' then data.to_sym
end
end
private
def self.number(serial)
if serial.include? '.'
serial.to_f
else
serial.to_i
end
end
def self.boolean(serial)
(serial == 'true') ? true : false
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)
@directories[name] = directory
end
def [](name)
if @directories.include? name
@directories[name]
elsif @files.include? name
@files[name]
else
nil
end
end
def serialize
serialize_hash(@files) + serialize_hash(@directories)
end
private
def serialize_hash(hash)
serial = hash.length.to_s + ':'
hash.each do |name, file|
serial += name + ':' + file.serialize.length.to_s + ':' + file.serialize
end
serial
end
end
end

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

.F......FFFFFF..............................

Failures:

  1) RBFS Directory can create empty directory
     Failure/Error: directory.add_directory 'home'
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20141111-26053-17cu48g/solution.rb:59:in `add_directory'
     # /tmp/d20141111-26053-17cu48g/spec.rb:150: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)>'

  2) RBFS Directory serialization #serialize can serialize
     Failure/Error: directory.add_directory 'rbfs'
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20141111-26053-17cu48g/solution.rb:59:in `add_directory'
     # /tmp/d20141111-26053-17cu48g/spec.rb:79: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 Directory serialization #serialize can serialize multiple directories recursively
     Failure/Error: directory.add_directory 'rbfs'
     ArgumentError:
       wrong number of arguments (1 for 2)
     # /tmp/d20141111-26053-17cu48g/solution.rb:59:in `add_directory'
     # /tmp/d20141111-26053-17cu48g/spec.rb:88: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 Directory serialization ::parse can parse empty directories
     Failure/Error: parsed_directory = RBFS::Directory.parse('0:0:')
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-17cu48g/spec.rb:101: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 Directory serialization ::parse can parse directories with files
     Failure/Error: parsed_directory = RBFS::Directory.parse(simple_serialized_string)
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-17cu48g/spec.rb:108: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 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:')
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-17cu48g/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)>'

  7) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: parsed_directory = RBFS::Directory.parse(recursive_serialized_string)
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-17cu48g/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)>'

Finished in 0.04064 seconds
44 examples, 7 failures

Failed examples:

rspec /tmp/d20141111-26053-17cu48g/spec.rb:149 # RBFS Directory can create empty directory
rspec /tmp/d20141111-26053-17cu48g/spec.rb:76 # RBFS Directory serialization #serialize can serialize
rspec /tmp/d20141111-26053-17cu48g/spec.rb:84 # RBFS Directory serialization #serialize can serialize multiple directories recursively
rspec /tmp/d20141111-26053-17cu48g/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-17cu48g/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-17cu48g/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-17cu48g/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively

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

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

+module RBFS
+ class File
+ attr_accessor :data
+ def initialize(data = nil)
+ @data = data
+ end
+
+ def data_type
+ case data
+ when nil then :nil
+ when String then :string
+ when Symbol then :symbol
+ when Numeric then :number
+ when true, false then :boolean
+ end
+ end
+
+ def serialize
+ data_type.to_s + ':' + data.to_s
+ 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)
+ @directories[name] = directory
+ end
+
+ def [](name)
+ if @directories.include? name
+ @directories[name]
+ elsif @files.include? name
+ @files[name]
+ else
+ nil
+ end
+ end
+ end
+end

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

module RBFS
class File
attr_accessor :data
def initialize(data = nil)
@data = data
end
def data_type
case data
when nil then :nil
when String then :string
when Symbol then :symbol
when Numeric then :number
when true, false then :boolean
end
end
def serialize
data_type.to_s + ':' + data.to_s
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)
@directories[name] = directory
end
def [](name)
if @directories.include? name
@directories[name]
elsif @files.include? name
@files[name]
else
nil
end
end
+
+ def serialize_hash(hash)
+ serial = hash.length.to_s + ':'
+ hash.each do |name, file|
+ serial += name + ':' + file.serialize.length.to_s + ':' + file.serialize
+ end
+ serial
+ end
+
+ def serialize
+ serialize_hash(@files) + serialize_hash(@directories)
+ end
end
end

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

module RBFS
class File
attr_accessor :data
def initialize(data = nil)
@data = data
end
def data_type
case data
when nil then :nil
when String then :string
when Symbol then :symbol
when Numeric then :number
when true, false then :boolean
end
end
def serialize
data_type.to_s + ':' + data.to_s
end
+
+ def self.parse(serial)
+ type, data = serial.split(':', 2)
+ self.new case type
+ when 'nil' then nil
+ when 'string' then data
+ when 'boolean' then boolean(data)
+ when 'number' then number(data)
+ when 'symbol' then data.to_sym
+ end
+ end
+
+ private
+ def self.number(serial)
+ if serial.include? '.'
+ serial.to_f
+ else
+ serial.to_i
+ end
+ end
+
+ def self.boolean(serial)
+ (serial == 'true') ? true : false
+ 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)
@directories[name] = directory
end
def [](name)
if @directories.include? name
@directories[name]
elsif @files.include? name
@files[name]
else
nil
end
end
+ def serialize
+ serialize_hash(@files) + serialize_hash(@directories)
+ end
+
+ private
def serialize_hash(hash)
serial = hash.length.to_s + ':'
hash.each do |name, file|
serial += name + ':' + file.serialize.length.to_s + ':' + file.serialize
end
serial
- end
-
- def serialize
- serialize_hash(@files) + serialize_hash(@directories)
end
end
end