Решение на Трета задача от Тодор Табаков

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

Към профила на Тодор Табаков

Резултати

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

Код

module RBFS
class File
attr_accessor :data, :data_type
def initialize(data = nil)
@data = data
if @data.is_a? String then @data_type = :string
elsif @data.is_a? Symbol then @data_type = :symbol
elsif @data.is_a? Fixnum then @data_type = :number
elsif @data.is_a? Float then @data_type = :number
elsif @data.is_a? TrueClass then @data_type = :boolean
elsif @data.is_a? FalseClass then @data_type = :boolean
else @data_type = data
end
end
def serialize
@file_serialize = "#{@data_type.to_s}:#{@data}"
end
def self.parse(string_data = nil)
data_type = string_data.split(":")[0]
data = string_data.split(":")[1]
if data_type == "symbol" then File.new(data.to_sym)
elsif data_type == "string" then File.new data
elsif data_type == "number" then File.new(data.to_i)
elsif data_type == "boolean" then File.new(data == "true")
elsif data.include?"." then File.new(data.to_f)
else File.new
end
end
end
class Directory
attr_accessor :directories, :files
def initialize
@directories = {}
@files = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = nil)
if directory.nil?
@directories[name] = RBFS::Directory.new
else
@directories[name] = directory
end
end
def [](name)
if @directories.has_key?(name)
@directories[name]
elsif @files.has_key?(name)
@files[name]
else
nil
end
end
def serialize
return_data = @files.size.to_s + ":"
@files.each do |key, value|
return_data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
end
return_data += @directories.size.to_s + ":"
@directories.each do |key, value|
return_data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
end
return_data
end
end
end

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

..........FFFF........FFFFF.FFF.FF.FFFFF.FF.

Failures:

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

  2) 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-r5q1gl/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)>'

  3) 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-r5q1gl/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)>'

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

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

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

  7) RBFS File data type nil can be parsed
     Failure/Error: file = RBFS::File.parse('nil:')
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20141111-26053-r5q1gl/solution.rb:29:in `parse'
     # /tmp/d20141111-26053-r5q1gl/spec.rb:230: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 string can be detected
     Failure/Error: expect(file.data_type).to eq :string
       
       expected: :string
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/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)>'

  9) RBFS File data type string can be serialized
     Failure/Error: expect(file.serialize).to eq 'string:Hi'
       
       expected: "string:Hi"
            got: ":Hi"
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/spec.rb:244: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 parse a string with colons
     Failure/Error: expect(file.data     ).to eq 'Hay :)'
       
       expected: "Hay :)"
            got: "Hay "
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/spec.rb:257: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: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/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 symbol can be serialized
     Failure/Error: expect(file.serialize).to eq 'symbol:yo'
       
       expected: "symbol:yo"
            got: ":yo"
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/spec.rb:270: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 detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/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)>'

  14) RBFS File data type number can be serialized
     Failure/Error: expect(file.serialize).to eq 'number:666'
       
       expected: "number:666"
            got: ":666"
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/spec.rb:289: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 detected
     Failure/Error: expect(file.data_type).to eq :number
       
       expected: :number
            got: nil
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/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)>'

  16) RBFS File data type float number can be serialized
     Failure/Error: expect(file.serialize).to eq 'number:666.6'
       
       expected: "number:666.6"
            got: ":666.6"
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/spec.rb:308: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)>'

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

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

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

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

  21) RBFS File data type boolean false can be serialized
     Failure/Error: expect(file.serialize).to eq 'boolean:false'
       
       expected: "boolean:false"
            got: ":false"
       
       (compared using ==)
     # /tmp/d20141111-26053-r5q1gl/spec.rb:347: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.0454 seconds
44 examples, 21 failures

Failed examples:

rspec /tmp/d20141111-26053-r5q1gl/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:221 # RBFS File data type nil can be detected
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:225 # RBFS File data type nil can be serialized
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:239 # RBFS File data type string can be detected
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:243 # RBFS File data type string can be serialized
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:265 # RBFS File data type symbol can be detected
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:269 # RBFS File data type symbol can be serialized
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:284 # RBFS File data type number can be detected
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:288 # RBFS File data type number can be serialized
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:303 # RBFS File data type float number can be detected
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:307 # RBFS File data type float number can be serialized
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:321 # RBFS File data type boolean true can be detected
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:326 # RBFS File data type boolean true can be serialized
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:340 # RBFS File data type boolean false can be detected
rspec /tmp/d20141111-26053-r5q1gl/spec.rb:345 # RBFS File data type boolean false can be serialized

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

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

+module RBFS
+
+ class File
+ attr_accessor :data, :data_type
+
+ def initialize(data = nil)
+ @data = data
+ if @data.is_a? String then @data_type = :string
+ elsif @data.is_a? Symbol then @data_type = :symbol
+ elsif @data.is_a? Fixnum then @data_type = :number
+ elsif @data.is_a? Float then @data_type = :number
+ elsif @data.is_a? TrueClass then @data_type = :boolean
+ elsif @data.is_a? FalseClass then @data_type = :boolean
+ else @data_type = data
+ end
+ end
+
+ def serialize
+ @file_serialize = "#{@data_type.to_s}:#{@data}"
+ end
+
+ def self.parse(string_data = nil)
+ data_type = string_data.split(":")[0]
+ data = string_data.split(":")[1]
+ if data_type == "symbol" then File.new(data.to_sym)
+ elsif data_type == "string" then File.new data
+ elsif data_type == "number" then File.new(data.to_i)
+ elsif data_type == "boolean" then File.new(data == "true")
+ elsif data.include?"." then File.new(data.to_f)
+ else File.new
+ end
+ end
+
+ end
+
+ class Directory
+ attr_accessor :directories, :files
+
+ def initialize
+ @directories = {}
+ @files = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory = nil)
+ if directory.nil?
+ @directories[name] = RBFS::Directory.new
+ else
+ @directories[name] = directory
+ end
+ end
+
+ def [](name)
+ if @directories.has_key?(name)
+ @directories[name]
+ elsif @files.has_key?(name)
+ @files[name]
+ else
+ nil
+ end
+
+ end
+
+ def serialize
+ return_data = @files.size.to_s + ":"
+ @files.each do |key, value|
+ return_data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
+ end
+ return_data += @directories.size.to_s + ":"
+ @directories.each do |key, value|
+ return_data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
+ end
+ return_data
+ end
+
+ end
+
+end