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

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

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

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 31 успешни тест(а)
  • 13 неуспешни тест(а)

Код

module RBFS
class File
attr_accessor :data
attr_reader :data_type
def initialize(data=nil)
@data = data
if (data.is_a? String) then @data_type = :string
elsif (data.is_a? NilClass) then @data_type = :nil
elsif (data.is_a? Symbol) then @data_type = :symbol
elsif (data.is_a? TrueClass) then @data_type = :boolean
elsif (data.is_a? FalseClass) then @data_type = :boolean
elsif (data.is_a? Numeric) then @data_type = :number
end
end
def serialize
@data_type.to_s + ":" + @data.to_s
end
def self.parse(string_data)
arr = string_data.split(':',2)
if (arr[0] == "boolean") then RBFS::File.new(arr[1] == "true")
elsif (arr[0] == "string") then RBFS::File.new(arr[1])
elsif (arr[0] == "number") then RBFS::File.new(arr[1].to_i)
elsif (arr[0] == "nil") then RBFS::File.new
elsif (arr[0] == "symbol") then RBFS::File.new(arr[1].to_sym)
else RBFS::File.new(arr[1].to_f)
end
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize()
@files = {}
@directories = {}
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)
return @directories[name] if @directories.has_key?(name)
return @files[name] if @files.has_key?(name)
end
def serialize
data = @files.size.to_s + ":"
@files.each do |key, value|
data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
end
data += @directories.size.to_s + ":"
@directories.each do |key, value|
data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
end
data
end
def parse_helper(string_data)
array = string_data.split(':', 2)
mark, array = array[0].to_i , array[1]
while mark != 0 do
array = array.split(':', 3)
file = array[2].to_s
add_file(array[0].to_s, RBFS::File.parse(file[0,array[1].to_i]))
array, mark = file[array[1].to_i, array[2].size - 1] , mark - 1
end
parse_helper_two(array)
end
def parse_helper_two(string_data)
array = string_data.split(':',2)
mark, array = array[0].to_i , array[1]
while mark != 0 do
array = array.split(':',3)
file = array[2].to_s
add_directory(array[0].to_s, RBFS::Directory.parse(file[0,array[1].to_i]))
array = file[array[1].to_i, array[2].size - 1]
mark -= 1
end
end
def self.parse(string_data)
folder = RBFS::Directory.new
folder.parse_helper(string_data)
folder
end
end
end

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

.........................FF..FF.FF.FFFFF.FF.

Failures:

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

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

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

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

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

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

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

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

  9) 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-8bhhx3/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)>'

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

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

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

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

Failed examples:

rspec /tmp/d20141111-26053-8bhhx3/spec.rb:239 # RBFS File data type string can be detected
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:243 # RBFS File data type string can be serialized
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:265 # RBFS File data type symbol can be detected
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:269 # RBFS File data type symbol can be serialized
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:284 # RBFS File data type number can be detected
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:288 # RBFS File data type number can be serialized
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:303 # RBFS File data type float number can be detected
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:307 # RBFS File data type float number can be serialized
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:321 # RBFS File data type boolean true can be detected
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:326 # RBFS File data type boolean true can be serialized
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:340 # RBFS File data type boolean false can be detected
rspec /tmp/d20141111-26053-8bhhx3/spec.rb:345 # RBFS File data type boolean false can be serialized

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

Станимир обнови решението на 10.11.2014 11:16 (преди над 9 години)

+module RBFS
+
+ class File
+
+
+ attr_accessor :data
+ attr_reader :data_type
+
+
+ def initialize(data=nil)
+ @data = data
+ if (data.is_a? String) then @data_type = :string
+ elsif (data.is_a? NilClass) then @data_type = :nil
+ elsif (data.is_a? Symbol) then @data_type = :symbol
+ elsif (data.is_a? TrueClass) then @data_type = :boolean
+ elsif (data.is_a? FalseClass) then @data_type = :boolean
+ elsif (data.is_a? Numeric) then @data_type = :number
+ end
+ end
+
+
+ def serialize
+
+ @data_type.to_s + ":" + @data.to_s
+
+ end
+
+ def self.parse(string_data)
+
+ arr = string_data.split(':',2)
+ if (arr[0] == "boolean") then RBFS::File.new(arr[1] == "true")
+ elsif (arr[0] == "string") then RBFS::File.new(arr[1])
+ elsif (arr[0] == "number") then RBFS::File.new(arr[1].to_i)
+ elsif (arr[0] == "nil") then RBFS::File.new
+ elsif (arr[0] == "symbol") then RBFS::File.new(arr[1].to_sym)
+ else RBFS::File.new(arr[1].to_f)
+ end
+
+ end
+
+ end
+
+
+ class Directory
+
+ attr_reader :files
+ attr_reader :directories
+
+
+ def initialize()
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+
+ def add_directory(name, directory = nil)
+ @directories[name] = directory
+ end
+
+ def [](name)
+ return @directories[name] if @directories.has_key?(name)
+ return @files[name] if @files.has_key?(name)
+ end
+
+ def serialize
+
+ data = @files.size.to_s + ":"
+ @files.each do |key, value|
+ data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
+ end
+
+ data += @directories.size.to_s + ":"
+ @directories.each do |key, value|
+ data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
+ end
+
+ data
+
+ end
+
+ def parse_helper(string_data)
+
+ array = string_data.split(':', 2)
+ mark, array = array[0].to_i , array[1]
+
+ while mark != 0 do
+ array = array.split(':', 3)
+ file = array[2].to_s
+ add_file(array[0].to_s, RBFS::File.parse(file[0,array[1].to_i]))
+ array, mark = file[array[1].to_i, array[2].size - 1] , mark - 1
+
+ end
+
+ parse_helper_two(array)
+
+ end
+
+ def parse_helper_two(string_data)
+
+ array = string_data.split(':',2)
+ mark, array = array[0].to_i , array[1]
+
+ while mark != 0 do
+ array = array.split(':',3)
+ file = array[2].to_s
+ add_directory(array[0].to_s, RBFS::Directory.parse(file[0,array[1].to_i]))
+ array = file[array[1].to_i, array[2].size - 1]
+ mark -= 1
+ end
+
+ end
+
+ def self.parse(string_data)
+
+ folder = RBFS::Directory.new
+ folder.parse_helper(string_data)
+ folder
+
+ end
+
+ end
+
+end

Станимир обнови решението на 10.11.2014 12:43 (преди над 9 години)

module RBFS
class File
attr_accessor :data
attr_reader :data_type
def initialize(data=nil)
@data = data
if (data.is_a? String) then @data_type = :string
elsif (data.is_a? NilClass) then @data_type = :nil
elsif (data.is_a? Symbol) then @data_type = :symbol
elsif (data.is_a? TrueClass) then @data_type = :boolean
elsif (data.is_a? FalseClass) then @data_type = :boolean
elsif (data.is_a? Numeric) then @data_type = :number
end
end
def serialize
@data_type.to_s + ":" + @data.to_s
end
def self.parse(string_data)
arr = string_data.split(':',2)
if (arr[0] == "boolean") then RBFS::File.new(arr[1] == "true")
elsif (arr[0] == "string") then RBFS::File.new(arr[1])
elsif (arr[0] == "number") then RBFS::File.new(arr[1].to_i)
elsif (arr[0] == "nil") then RBFS::File.new
elsif (arr[0] == "symbol") then RBFS::File.new(arr[1].to_sym)
else RBFS::File.new(arr[1].to_f)
end
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize()
@files = {}
@directories = {}
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)
return @directories[name] if @directories.has_key?(name)
return @files[name] if @files.has_key?(name)
end
def serialize
data = @files.size.to_s + ":"
@files.each do |key, value|
data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
end
data += @directories.size.to_s + ":"
@directories.each do |key, value|
data += key.to_s + ":" + value.serialize.size.to_s + ":" + value.serialize
end
data
end
def parse_helper(string_data)
array = string_data.split(':', 2)
mark, array = array[0].to_i , array[1]
while mark != 0 do
array = array.split(':', 3)
file = array[2].to_s
add_file(array[0].to_s, RBFS::File.parse(file[0,array[1].to_i]))
array, mark = file[array[1].to_i, array[2].size - 1] , mark - 1
end
parse_helper_two(array)
end
def parse_helper_two(string_data)
array = string_data.split(':',2)
mark, array = array[0].to_i , array[1]
while mark != 0 do
array = array.split(':',3)
file = array[2].to_s
add_directory(array[0].to_s, RBFS::Directory.parse(file[0,array[1].to_i]))
array = file[array[1].to_i, array[2].size - 1]
mark -= 1
end
end
def self.parse(string_data)
folder = RBFS::Directory.new
folder.parse_helper(string_data)
folder
end
end
end