Решение на Трета задача от Евгений Янев

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

Към профила на Евгений Янев

Резултати

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

Код

module RBFS
class File
def initialize(data = nil)
@data = data
end
def data_type
if @data.is_a?(String)
type = :string
elsif @data.is_a?(Numeric)
type = :number
elsif @data.is_a?(Symbol)
type= :symbol
elsif @data.is_a?(TrueClass) or @data.is_a?(FalseClass)
type = :boolean
end
end
def data= data
@data = data
end
def data
@data
end
def serialize
"#{data_type}:#{@data}"
end
def self.parse(data)
return File.new(data.split(':')[1].to_sym)
end
end
class Directory
def initialize()
@files = Hash.new
@directories = Hash.new
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = Directory.new)
@directories[name] = directory
end
def [](name)
result = @directories[name]
if not result
result = @files[name]
end
result
end
def serialize_files()
data = "#{@files.size}:"
@files.each do |name, file|
ser_file = file.serialize
data = "#{data}#{name}:#{ser_file.size}:#{ser_file}"
end
data
end
def serialize_dirs(data)
data = "#{data}#{@directories.size}"
@directories.each do |name, dir|
ser_dir = dir.serialize_directory
data = "#{data}:#{name}:#{ser_dir.size+1}:#{ser_dir}"
end
data
end
def serialize_directory()
data = serialize_files()
data = "#{serialize_dirs(data)}"
end
def serialize
data = "#{serialize_directory}:"
end
# def self.parse(serialized)
# dir = Directory.new
# files, rest = parse_entries(serialized)
# directories, = parse_entries(rest, true)
# files.each do |name, value|
# dir.add_file(name, File.parse(value))
# end
# directories.each do |name, value|
# dir.add_directory(name, value)
# end
# dir
# end
# def self.parse_entries(serialized, directories=false)
# num_entries, rest = serialized.split(':', 2)
# num_entries = Integer(num_entries)
# entries = {}
# num_entries.times do
# name, content, rest = parse_entry(rest)
# entries[name] = directories ? parse(content) : content
# end
# [ entries, rest ]
# end
# def self.parse_entry(serialized)
# name, len, rest = serialized.split(':', 3)
# len = Integer(len)
# content = rest.slice!(0, len)
# [ name, content, rest ]
# end
end
end

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

FFFF.FFF..FFFF........FFF..FF.....F..F..F..F

Failures:

  1) RBFS Directory can add a file
     Failure/Error: expect(directory.files).to eq({'README' => file})
     NoMethodError:
       undefined method `files' for #<RBFS::Directory:0xb9326a7c>
     # /tmp/d20141111-26053-1eju63v/spec.rb:146: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 can create empty directory
     Failure/Error: expect(directory.directories.keys   ).to eq    ['home']
     NoMethodError:
       undefined method `directories' for #<RBFS::Directory:0xb9325cd0>
     # /tmp/d20141111-26053-1eju63v/spec.rb:152: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)>'

  3) RBFS Directory can add a directory
     Failure/Error: expect(directory.directories).to eq({'home' => subdirectory})
     NoMethodError:
       undefined method `directories' for #<RBFS::Directory:0xb9324e98>
     # /tmp/d20141111-26053-1eju63v/spec.rb:161: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)>'

  4) RBFS Directory without files returns empty hash for the files
     Failure/Error: expect(directory.files).to eq({})
     NoMethodError:
       undefined method `files' for #<RBFS::Directory:0xb93241dc @files={}, @directories={}>
     # /tmp/d20141111-26053-1eju63v/spec.rb:7:in `block (4 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 without directories returns empty hash for the directories
     Failure/Error: expect(directory.directories).to eq({})
     NoMethodError:
       undefined method `directories' for #<RBFS::Directory:0xb9321374 @files={}, @directories={}>
     # /tmp/d20141111-26053-1eju63v/spec.rb:17:in `block (4 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 with files and directories returns correct file hash
     Failure/Error: expect(directory.files).to eq({'README' => readme, 'spec.rb' => spec})
     NoMethodError:
       undefined method `files' for #<RBFS::Directory:0xb9320370>
     # /tmp/d20141111-26053-1eju63v/spec.rb:34:in `block (4 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 with files and directories returns correct directory hash
     Failure/Error: expect(directory.directories).to eq({'rbfs' => rbfs})
     NoMethodError:
       undefined method `directories' for #<RBFS::Directory:0xb931eed0>
     # /tmp/d20141111-26053-1eju63v/spec.rb:38:in `block (4 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 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-1eju63v/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)>'

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

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

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

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

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

  14) RBFS File data type nil can be parsed
     Failure/Error: file = RBFS::File.parse('nil:')
     NoMethodError:
       undefined method `to_sym' for nil:NilClass
     # /tmp/d20141111-26053-1eju63v/solution.rb:27:in `parse'
     # /tmp/d20141111-26053-1eju63v/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)>'

  15) RBFS File data type string can be parsed
     Failure/Error: expect(file.data     ).to eq 'Hey there'
       
       expected: "Hey there"
            got: :"Hey there"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"Hey there"
       +:"Hey there"
     # /tmp/d20141111-26053-1eju63v/spec.rb:250: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 string can parse a string with colons
     Failure/Error: expect(file.data     ).to eq 'Hay :)'
       
       expected: "Hay :)"
            got: :"Hay "
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -"Hay :)"
       +:"Hay "
     # /tmp/d20141111-26053-1eju63v/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)>'

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

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

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

  20) 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-1eju63v/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.04373 seconds
44 examples, 20 failures

Failed examples:

rspec /tmp/d20141111-26053-1eju63v/spec.rb:141 # RBFS Directory can add a file
rspec /tmp/d20141111-26053-1eju63v/spec.rb:149 # RBFS Directory can create empty directory
rspec /tmp/d20141111-26053-1eju63v/spec.rb:156 # RBFS Directory can add a directory
rspec /tmp/d20141111-26053-1eju63v/spec.rb:6 # RBFS Directory without files returns empty hash for the files
rspec /tmp/d20141111-26053-1eju63v/spec.rb:16 # RBFS Directory without directories returns empty hash for the directories
rspec /tmp/d20141111-26053-1eju63v/spec.rb:33 # RBFS Directory with files and directories returns correct file hash
rspec /tmp/d20141111-26053-1eju63v/spec.rb:37 # RBFS Directory with files and directories returns correct directory hash
rspec /tmp/d20141111-26053-1eju63v/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-1eju63v/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-1eju63v/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-1eju63v/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-1eju63v/spec.rb:221 # RBFS File data type nil can be detected
rspec /tmp/d20141111-26053-1eju63v/spec.rb:225 # RBFS File data type nil can be serialized
rspec /tmp/d20141111-26053-1eju63v/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-1eju63v/spec.rb:247 # RBFS File data type string can be parsed
rspec /tmp/d20141111-26053-1eju63v/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-1eju63v/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-1eju63v/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-1eju63v/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-1eju63v/spec.rb:350 # RBFS File data type boolean false can be parsed

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

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

+module RBFS
+ class File
+ def initialize(data = nil)
+ @data = data
+ end
+ def data_type
+ if @data.is_a?(String)
+ type = :string
+ elsif @data.is_a?(Numeric)
+ type = :number
+ elsif @data.is_a?(Symbol)
+ type= :symbol
+ elsif @data.is_a?(TrueClass) or @data.is_a?(FalseClass)
+ type = :boolean
+ end
+ end
+ def data= data
+ @data = data
+ end
+ def data
+ @data
+ end
+ def serialize
+ "#{data_type}:#{@data}"
+ end
+ def self.parse(data)
+ return File.new(data.split(':')[1].to_sym)
+ end
+ end
+ class Directory
+ def initialize()
+ @files = Hash.new
+ @directories = Hash.new
+ end
+ def add_file(name, file)
+ @files[name] = file
+ end
+ def add_directory(name, directory = Directory.new)
+ @directories[name] = directory
+ end
+ def [](name)
+ result = @directories[name]
+ if not result
+ result = @files[name]
+ end
+ result
+ end
+ def serialize_files()
+ data = "#{@files.size}:"
+ @files.each do |name, file|
+ ser_file = file.serialize
+ data = "#{data}#{name}:#{ser_file.size}:#{ser_file}"
+ end
+ data
+ end
+ def serialize_dirs(data)
+ data = "#{data}#{@directories.size}"
+ @directories.each do |name, dir|
+ ser_dir = dir.serialize_directory
+ data = "#{data}:#{name}:#{ser_dir.size+1}:#{ser_dir}"
+ end
+ data
+ end
+ def serialize_directory()
+ data = serialize_files()
+ data = "#{serialize_dirs(data)}"
+ end
+ def serialize
+ data = "#{serialize_directory}:"
+ end
+ # def self.parse(serialized)
+ # dir = Directory.new
+ # files, rest = parse_entries(serialized)
+ # directories, = parse_entries(rest, true)
+ # files.each do |name, value|
+ # dir.add_file(name, File.parse(value))
+ # end
+ # directories.each do |name, value|
+ # dir.add_directory(name, value)
+ # end
+ # dir
+ # end
+ # def self.parse_entries(serialized, directories=false)
+ # num_entries, rest = serialized.split(':', 2)
+ # num_entries = Integer(num_entries)
+ # entries = {}
+ # num_entries.times do
+ # name, content, rest = parse_entry(rest)
+ # entries[name] = directories ? parse(content) : content
+ # end
+ # [ entries, rest ]
+ # end
+ # def self.parse_entry(serialized)
+ # name, len, rest = serialized.split(':', 3)
+ # len = Integer(len)
+ # content = rest.slice!(0, len)
+ # [ name, content, rest ]
+ # end
+ end
+end