Решение на Трета задача от Димитър Мутаров

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

Към профила на Димитър Мутаров

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 25 успешни тест(а)
  • 19 неуспешни тест(а)

Код

module RBFS
class File
attr_reader :data_type
attr_accessor :data
def initialize(data=nil, data_type=nil)
@data = data
if @data.nil?
@data_type = nil
elsif data_type.nil? && !@data.nil?
@data_type = getType(data).to_sym
else
@data_type = data_type.to_sym
end
end
def getType(value)
case
when value.is_a?(String) then 'string'
when value.is_a?(Symbol) then 'symbol'
when value.is_a?(Numeric) then 'number'
when value.is_a?(TrueClass) || value.is_a?(FalseClass) then 'boolean'
end
end
def data=(data)
@data = data
@data_type = getType(data)
end
def data_type=(data_type)
@data_type = data_type
end
def serialize
"#{@data_type}:#{@data}"
end
def self.parse(string_data)
result = string_data.split(':', 2)
type, data = result[0], result[1]
if type == 'symbol'
data = data.to_sym
elsif type == 'nil'
data = nil
end
File.new(data, type)
end
end
class Directory
attr_reader :files,:directories
def initialize(files=Hash.new, directories=Hash.new)
@files = files
@directories = directories
end
def add_file(name, file=nil)
if name.include? ":"
print "The file name cannot contain: ':' character "
end
@files[name.to_s] = file
end
def add_directory(name, directories=nil)
if name.include? ":"
print "The directory name cannot contain: ':' character "
end
directories ||= Directory.new
@directories[name.to_s] = directories
end
def serialize_files(files=nil)
if files.nil? || files.empty? then '0:'
end
result = files.length.to_s + ':'
files.each do |key, value|
result << key.to_s + ':'
temp = value.serialize
result << temp.length.to_s + ':' + temp
end
result
end
def serialize_directories(directory=nil)
result = serialize_files(directory.files)
if directory.directories.nil? || directory.directories.empty?
return result << '0:'
end
result << directory.directories.length.to_s + ':'
directory.directories.each do |key, value|
result << key.to_s + ':'
temp = serialize_directories(value)
result << temp.length.to_s + ':' + temp
end
end
def serialize()
if self.nil? || self.empty?
'0:0:'
else
serialize_directories(self)
end
end
def empty?
(@files.empty? || @files.nil?) &&
(@directories.empty? || @directories.nil?)
end
def [](name)
name = name.to_s
if @directories.has_key?(name)
@directories[name]
else
@files[name]
end
end
end
end

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

........FFFFFF........FFFF...F..F.FF.FF.FF.F

Failures:

  1) RBFS Directory serialization #serialize can serialize
     Failure/Error: expect(directory.serialize).to eq simple_serialized_string
       
       expected: "2:README:19:string:Hello world!spec.rb:20:string:describe RBFS1:rbfs:4:0:0:"
            got: {"rbfs"=>#<RBFS::Directory:0xba3cc55c @files={}, @directories={}>}
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"2:README:19:string:Hello world!spec.rb:20:string:describe RBFS1:rbfs:4:0:0:"
       +"rbfs" => #<RBFS::Directory:0xba3cc55c @directories={}, @files={}>
     # /tmp/d20141111-26053-1n1nb69/spec.rb:81: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 #serialize can serialize multiple directories recursively
     Failure/Error: expect(directory.serialize).to eq recursive_serialized_string
     TypeError:
       no implicit conversion of Hash into String
     # /tmp/d20141111-26053-1n1nb69/solution.rb:97:in `+'
     # /tmp/d20141111-26053-1n1nb69/solution.rb:97:in `block in serialize_directories'
     # /tmp/d20141111-26053-1n1nb69/solution.rb:94:in `each'
     # /tmp/d20141111-26053-1n1nb69/solution.rb:94:in `serialize_directories'
     # /tmp/d20141111-26053-1n1nb69/solution.rb:105:in `serialize'
     # /tmp/d20141111-26053-1n1nb69/spec.rb:95: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 ::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-1n1nb69/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)>'

  4) 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-1n1nb69/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)>'

  5) 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-1n1nb69/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)>'

  6) 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-1n1nb69/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)>'

  7) RBFS File data type nil can be detected
     Failure/Error: expect(file.data_type).to eq :nil
       
       expected: :nil
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-1n1nb69/spec.rb:222: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)>'

  8) RBFS File data type nil can be serialized
     Failure/Error: expect(file.serialize).to eq 'nil:'
       
       expected: "nil:"
            got: ":"
       
       (compared using ==)
     # /tmp/d20141111-26053-1n1nb69/spec.rb:226: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)>'

  9) RBFS File data type nil can be parsed
     Failure/Error: expect(file.data_type).to eq :nil
       
       expected: :nil
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-1n1nb69/spec.rb:232: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)>'

  10) RBFS File data type string can be detected
     Failure/Error: expect(file.data_type).to eq :string
       
       expected: :string
            got: "string"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:string
       +"string"
     # /tmp/d20141111-26053-1n1nb69/spec.rb:240: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)>'

  11) RBFS File data type symbol can be detected
     Failure/Error: expect(file.data_type).to eq :symbol
       
       expected: :symbol
            got: "symbol"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:symbol
       +"symbol"
     # /tmp/d20141111-26053-1n1nb69/spec.rb:266: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)>'

  12) RBFS File data type number can be detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: "number"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +"number"
     # /tmp/d20141111-26053-1n1nb69/spec.rb:285: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)>'

  13) RBFS File data type number can be parsed
     Failure/Error: expect(file.data     ).to eq 1234
       
       expected: 1234
            got: "1234"
       
       (compared using ==)
     # /tmp/d20141111-26053-1n1nb69/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)>'

  14) RBFS File data type float number can be detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: "number"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:number
       +"number"
     # /tmp/d20141111-26053-1n1nb69/spec.rb:304: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)>'

  15) RBFS File data type float number can be parsed
     Failure/Error: expect(file.data     ).to eq 3.14
       
       expected: 3.14
            got: "3.14"
       
       (compared using ==)
     # /tmp/d20141111-26053-1n1nb69/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)>'

  16) RBFS File data type boolean true can be detected
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: "boolean"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +"boolean"
     # /tmp/d20141111-26053-1n1nb69/spec.rb:323: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)>'

  17) RBFS File data type boolean true can be parsed
     Failure/Error: expect(file.data     ).to eq true
       
       expected: true
            got: "true"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -true
       +"true"
     # /tmp/d20141111-26053-1n1nb69/spec.rb:334: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)>'

  18) RBFS File data type boolean false can be detected
     Failure/Error: expect(file.data_type).to eq :boolean
       
       expected: :boolean
            got: "boolean"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:boolean
       +"boolean"
     # /tmp/d20141111-26053-1n1nb69/spec.rb:342: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)>'

  19) RBFS File data type boolean false can be parsed
     Failure/Error: expect(file.data     ).to eq false
       
       expected: false
            got: "false"
       
       (compared using ==)
     # /tmp/d20141111-26053-1n1nb69/spec.rb:353: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.04787 seconds
44 examples, 19 failures

Failed examples:

rspec /tmp/d20141111-26053-1n1nb69/spec.rb:76 # RBFS Directory serialization #serialize can serialize
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:84 # RBFS Directory serialization #serialize can serialize multiple directories recursively
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:221 # RBFS File data type nil can be detected
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:225 # RBFS File data type nil can be serialized
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:239 # RBFS File data type string can be detected
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:265 # RBFS File data type symbol can be detected
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:284 # RBFS File data type number can be detected
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:303 # RBFS File data type float number can be detected
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:321 # RBFS File data type boolean true can be detected
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:340 # RBFS File data type boolean false can be detected
rspec /tmp/d20141111-26053-1n1nb69/spec.rb:350 # RBFS File data type boolean false can be parsed

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

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

+module RBFS
+ class File
+ attr_reader :data_type
+ attr_accessor :data
+
+ def initialize(data=nil, data_type=nil)
+ @data = data
+ if @data.nil?
+ @data_type = nil
+ elsif data_type.nil? && !@data.nil?
+ @data_type = getType(data).to_sym
+ else
+ @data_type = data_type.to_sym
+ end
+ end
+
+ def getType(value)
+ case
+ when value.is_a?(String) then 'string'
+ when value.is_a?(Symbol) then 'symbol'
+ when value.is_a?(Numeric) then 'number'
+ when value.is_a?(TrueClass) || value.is_a?(FalseClass) then 'boolean'
+ end
+ end
+
+ def data=(data)
+ @data = data
+ @data_type = getType(data)
+ end
+
+ def data_type=(data_type)
+ @data_type = data_type
+ end
+
+ def serialize
+ "#{@data_type}:#{@data}"
+ end
+
+ def self.parse(string_data)
+ result = string_data.split(':', 2)
+ type, data = result[0], result[1]
+ if type == 'symbol'
+ data = data.to_sym
+ elsif type == 'nil'
+ data = nil
+ end
+ File.new(data, type)
+ end
+ end
+
+ class Directory
+ attr_reader :files,:directories
+
+ def initialize(files=Hash.new, directories=Hash.new)
+ @files = files
+ @directories = directories
+ end
+
+ def add_file(name, file=nil)
+ if name.include? ":"
+ print "The file name cannot contain: ':' character "
+ end
+ @files[name.to_s] = file
+ end
+
+ def add_directory(name, directories=nil)
+ if name.include? ":"
+ print "The directory name cannot contain: ':' character "
+ end
+ directories ||= Directory.new
+ @directories[name.to_s] = directories
+ end
+
+ def serialize_files(files=nil)
+ if files.nil? || files.empty? then '0:'
+ end
+ result = files.length.to_s + ':'
+ files.each do |key, value|
+ result << key.to_s + ':'
+ temp = value.serialize
+ result << temp.length.to_s + ':' + temp
+ end
+ result
+ end
+
+ def serialize_directories(directory=nil)
+ result = serialize_files(directory.files)
+
+ if directory.directories.nil? || directory.directories.empty?
+ return result << '0:'
+ end
+
+ result << directory.directories.length.to_s + ':'
+ directory.directories.each do |key, value|
+ result << key.to_s + ':'
+ temp = serialize_directories(value)
+ result << temp.length.to_s + ':' + temp
+ end
+ end
+
+ def serialize()
+ if self.nil? || self.empty?
+ '0:0:'
+ else
+ serialize_directories(self)
+ end
+ end
+
+ def empty?
+ (@files.empty? || @files.nil?) &&
+ (@directories.empty? || @directories.nil?)
+ end
+
+ def [](name)
+ name = name.to_s
+ if @directories.has_key?(name)
+ @directories[name]
+ else
+ @files[name]
+ end
+ end
+ end
+end