Решение на Трета задача от Елена Орешарова

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

Към профила на Елена Орешарова

Резултати

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

Код

module RBFS
class File
attr_reader :data_type
attr_accessor :data
def initialize(data = nil)
@data_type = case
when (data.is_a? String) then :string
when (data.is_a? Symbol) then :symbol
when (data.is_a? TrueClass) then :boolean
when (data.is_a? FalseClass) then :boolean
when (data.is_a? Numeric) then :number
else :nil
end
@data = data
end
def serialize
"#{@data_type}:#{@data.to_s}"
end
def File.parse(string_data)
partition = string_data.split(':', 2)
case partition[0]
when 'string' then RBFS::File.new(partition [1])
when 'symbol' then RBFS::File.new(partition[1].to_sym)
when 'boolean' then parse_helper(partition[1])
when 'number' then parse_helper(partition[1])
when 'nil' then RBFS::File.new
end
end
def parse_helper(data)
if data == "true" then RBFS::File.new(true)
else RBFS::File.new(false)
end
if data.include?('.')
RBFS::File.new(data.to_f)
else RBFS::File.new(data.to_i)
end
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files.store(name, file) unless name.include?(':')
end
def add_directory(name, directory = Directory.new)
@directories.store(name, directory) unless name.include?(':')
end
def [](name)
if @directories.include?(name) then @directories[name]
elsif @files.include?(name) then @files[name]
else nil
end
end
def serialize
serializer = RBFS::DirectorySerializer.new(@files, @directories)
serializer.serialize
end
def Directory.parse(string_data)
#not implemented
end
end
class DirectorySerializer
attr_accessor :files
attr_accessor :directories
def initialize(files = {}, directories = {})
@files = files
@directories = directories
end
def serialize
result = ''
result << @files.size.to_s + ':'
result << serialize_files
result << @directories.size.to_s + ':'
result << serialize_directories
result
end
def serialize_files
result = ''
@files.each do |key, value|
result << key.to_s + ':' + value.serialize.size.to_s + ':' + value.serialize
end
result
end
def serialize_directories
result = ''
serializer = DirectorySerializer.new
@directories.each do |key, value|
result << key.to_s + ':'
serializer.files = value.files
serializer.directories = value.directories
result << serializer.serialize.size.to_s + ':' + serializer.serialize
end
result
end
end
end

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

..........FFFF...........FF..FF.FFFFFFFFFFFF

Failures:

  1) RBFS Directory serialization ::parse can parse empty directories
     Failure/Error: expect(parsed_directory.files      ).to eq({})
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-j3d9yt/spec.rb:103: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: expect(parsed_directory.files.size     ).to eq    2
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-j3d9yt/spec.rb:110: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: expect(parsed_directory['dir1']        ).to be_an RBFS::Directory
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20141111-26053-j3d9yt/spec.rb:119: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: expect(parsed_directory.files.size     ).to eq 2
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-j3d9yt/spec.rb:127: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 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-j3d9yt/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)>'

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

  7) 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-j3d9yt/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)>'

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

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

  10) 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-j3d9yt/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)>'

  11) RBFS File data type number can be parsed
     Failure/Error: file = RBFS::File.parse('number:1234')
     NoMethodError:
       undefined method `parse_helper' for RBFS::File:Class
     # /tmp/d20141111-26053-j3d9yt/solution.rb:28:in `parse'
     # /tmp/d20141111-26053-j3d9yt/spec.rb:293: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 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-j3d9yt/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)>'

  13) 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-j3d9yt/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)>'

  14) RBFS File data type float number can be parsed
     Failure/Error: file = RBFS::File.parse('number:3.14')
     NoMethodError:
       undefined method `parse_helper' for RBFS::File:Class
     # /tmp/d20141111-26053-j3d9yt/solution.rb:28:in `parse'
     # /tmp/d20141111-26053-j3d9yt/spec.rb:312: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 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-j3d9yt/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)>'

  16) 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-j3d9yt/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)>'

  17) RBFS File data type boolean true can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:true')
     NoMethodError:
       undefined method `parse_helper' for RBFS::File:Class
     # /tmp/d20141111-26053-j3d9yt/solution.rb:27:in `parse'
     # /tmp/d20141111-26053-j3d9yt/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)>'

  18) 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-j3d9yt/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 serialized
     Failure/Error: expect(file.serialize).to eq 'boolean:false'
       
       expected: "boolean:false"
            got: "nil:false"
       
       (compared using ==)
     # /tmp/d20141111-26053-j3d9yt/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)>'

  20) RBFS File data type boolean false can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:false')
     NoMethodError:
       undefined method `parse_helper' for RBFS::File:Class
     # /tmp/d20141111-26053-j3d9yt/solution.rb:27:in `parse'
     # /tmp/d20141111-26053-j3d9yt/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.04597 seconds
44 examples, 20 failures

Failed examples:

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

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

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

+module RBFS
+ class File
+ attr_reader :data_type
+ attr_accessor :data
+
+ def initialize(data = nil)
+ @data_type = case
+ when (data.is_a? String) then :string
+ when (data.is_a? Symbol) then :symbol
+ when (data.is_a? TrueClass) then :boolean
+ when (data.is_a? FalseClass) then :boolean
+ when (data.is_a? Numeric) then :number
+ else :nil
+ end
+ @data = data
+ end
+
+ def serialize
+ "#{@data_type}:#{@data.to_s}"
+ end
+
+ def File.parse(string_data)
+ partition = string_data.split(':', 2)
+ case partition[0]
+ when 'string' then RBFS::File.new(partition [1])
+ when 'symbol' then RBFS::File.new(partition[1].to_sym)
+ when 'boolean' then parse_helper(partition[1])
+ when 'number' then parse_helper(partition[1])
+ when 'nil' then RBFS::File.new
+ end
+ end
+
+ def parse_helper(data)
+ if data == "true" then RBFS::File.new(true)
+ else RBFS::File.new(false)
+ end
+ if data.include?('.')
+ RBFS::File.new(data.to_f)
+ else RBFS::File.new(data.to_i)
+ end
+ end
+ end
+
+ class Directory
+ attr_reader :files
+ attr_reader :directories
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files.store(name, file) unless name.include?(':')
+ end
+
+ def add_directory(name, directory)
+ @directories.store(name, directory) unless name.include?(':')
+ end
+
+ def [](name)
+ if @directories.include?(name) then @directories[name]
+ elsif @files.include?(name) then @files[name]
+ else nil
+ end
+ end
+
+ def serialize
+ serializer = RBFS::DirectorySerializer.new(@files, @directories)
+ serializer.serialize
+ end
+
+ def Directory.parse(string_data)
+ #not implemented
+ end
+ end
+
+ class DirectorySerializer
+ attr_accessor :files
+ attr_accessor :directories
+
+ def initialize(files = {}, directories = {})
+ @files = files
+ @directories = directories
+ end
+
+ def serialize
+ result = ''
+ result << @files.size.to_s + ':'
+ result << serialize_files
+ result << @directories.size.to_s + ':'
+ result << serialize_directories
+ result
+ end
+
+ def serialize_files
+ result = ''
+ @files.each do |key, value|
+ result << key.to_s + ':' + value.serialize.size.to_s + ':' + value.serialize
+ end
+ result
+ end
+
+ def serialize_directories
+ result = ''
+ serializer = DirectorySerializer.new
+ @directories.each do |key, value|
+ result << key.to_s + ':'
+ serializer.files = value.files
+ serializer.directories = value.directories
+ result << serializer.serialize.size.to_s + ':' + serializer.serialize
+ end
+ result
+ end
+ end
+end

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

module RBFS
class File
attr_reader :data_type
attr_accessor :data
def initialize(data = nil)
@data_type = case
when (data.is_a? String) then :string
when (data.is_a? Symbol) then :symbol
when (data.is_a? TrueClass) then :boolean
when (data.is_a? FalseClass) then :boolean
when (data.is_a? Numeric) then :number
else :nil
end
@data = data
end
def serialize
"#{@data_type}:#{@data.to_s}"
end
def File.parse(string_data)
partition = string_data.split(':', 2)
case partition[0]
when 'string' then RBFS::File.new(partition [1])
when 'symbol' then RBFS::File.new(partition[1].to_sym)
when 'boolean' then parse_helper(partition[1])
when 'number' then parse_helper(partition[1])
when 'nil' then RBFS::File.new
end
end
def parse_helper(data)
if data == "true" then RBFS::File.new(true)
else RBFS::File.new(false)
end
if data.include?('.')
RBFS::File.new(data.to_f)
else RBFS::File.new(data.to_i)
end
end
end
class Directory
attr_reader :files
attr_reader :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files.store(name, file) unless name.include?(':')
end
- def add_directory(name, directory)
+ def add_directory(name, directory = Directory.new)
@directories.store(name, directory) unless name.include?(':')
end
def [](name)
if @directories.include?(name) then @directories[name]
elsif @files.include?(name) then @files[name]
else nil
end
end
def serialize
serializer = RBFS::DirectorySerializer.new(@files, @directories)
serializer.serialize
end
def Directory.parse(string_data)
#not implemented
end
end
class DirectorySerializer
attr_accessor :files
attr_accessor :directories
def initialize(files = {}, directories = {})
@files = files
@directories = directories
end
def serialize
result = ''
result << @files.size.to_s + ':'
result << serialize_files
result << @directories.size.to_s + ':'
result << serialize_directories
result
end
def serialize_files
result = ''
@files.each do |key, value|
result << key.to_s + ':' + value.serialize.size.to_s + ':' + value.serialize
end
result
end
def serialize_directories
result = ''
serializer = DirectorySerializer.new
@directories.each do |key, value|
result << key.to_s + ':'
serializer.files = value.files
serializer.directories = value.directories
result << serializer.serialize.size.to_s + ':' + serializer.serialize
end
result
end
end
end