Решение на Трета задача от Александър Пирнарев

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

Към профила на Александър Пирнарев

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def initialize(*args)
raise ArgumentError if args.size > 1
@data = case args[0]
when String, FalseClass, TrueClass, Symbol, Fixnum, Integer, Float then args[0]
end
end
def data_type
case
when @data.kind_of?(Numeric) then :number
when (@data == true or @data == false) then :boolean
when @data == nil then :nil
else @data.class.name.downcase.to_sym
end
end
def serialize
"#{data_type.to_s}:#{@data.to_s}"
end
def to_n(temp)
if temp[1].include? "." then
newFile = RBFS::File.new(temp[1].to_f)
else
newFile = RBFS::File.new(temp[1].to_i)
end
end
def parse(string_data)
temp = string_data.split(/: */)
case temp[0]
when "nil" then newFile = RBFS::File.new
when "string" then newFile = RBFS::File.new(temp[1])
when "boolean" then newFile = RBFS::File.new(temp[1].to_bool)
when "symbol" then newFile = RBFS::File.new(temp[1].to_sym)
when "number" then to_n(temp)
end
end
def ser_len
serialize.length
end
end
class Directory
attr_reader :files, :directories
def initialize
@files = {}
@directories = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, *directory)
@directories[name] = {} if directory == nil
@directories[name] = directory[0]
end
def [](*name)
if @directories.has_key?(name[0])
@directories[name[0]]
elsif @files.has_key?(name[0])
@files[name[0]]
else
nil
end
end
def len
result = @files.size.to_s.length + 1 + @directories.size.to_s.length + 1
@files.each do |name, file|
result += name.size + file.ser_len.to_s.size + file.ser_len + 3
end
@directories.each { |name, dir| result += name.size + 2 + dir.len }
result
result if @directories.size == 0 or @directories.has_value? nil
end
def serialize
serial_str = ""
serial_str << @files.size.to_s + ":"
@files.each { |name, file| serial_str << "#{name}:#{file.ser_len}:#{file.serialize}"}
serial_str << @directories.size.to_s + ":"
@directories.each { |name, dir| serial_str << "#{name}:#{len}" + dir.serialize }
serial_str << "#{name}:4:0:0:" if @directories.has_value? nil
serial_str
end
def parse(string_data)
end
end
end
class String
def to_bool
return true if self == "true"
return false if self == "false"
end
end

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

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

Failures:

  1) RBFS Directory can create empty directory
     Failure/Error: expect(directory.directories['home']).to be_an RBFS::Directory
       expected nil to be a kind of RBFS::Directory
     # /tmp/d20141111-26053-txvytv/spec.rb:153: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 serialization #serialize can serialize
     Failure/Error: expect(directory.serialize).to eq simple_serialized_string
     NoMethodError:
       undefined method `len' for nil:NilClass
     # /tmp/d20141111-26053-txvytv/solution.rb:81:in `block in len'
     # /tmp/d20141111-26053-txvytv/solution.rb:81:in `each'
     # /tmp/d20141111-26053-txvytv/solution.rb:81:in `len'
     # /tmp/d20141111-26053-txvytv/solution.rb:91:in `block in serialize'
     # /tmp/d20141111-26053-txvytv/solution.rb:91:in `each'
     # /tmp/d20141111-26053-txvytv/solution.rb:91:in `serialize'
     # /tmp/d20141111-26053-txvytv/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)>'

  3) RBFS Directory serialization #serialize can serialize multiple directories recursively
     Failure/Error: directory['rbfs'].add_file         'solution.rb', RBFS::File.new(:hidden)
     NoMethodError:
       undefined method `add_file' for nil:NilClass
     # /tmp/d20141111-26053-txvytv/spec.rb:91: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 empty directories
     Failure/Error: parsed_directory = RBFS::Directory.parse('0:0:')
     NoMethodError:
       undefined method `parse' for RBFS::Directory:Class
     # /tmp/d20141111-26053-txvytv/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)>'

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

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

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

  8) RBFS File data type nil can be parsed
     Failure/Error: file = RBFS::File.parse('nil:')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-txvytv/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)>'

  9) RBFS File data type string can be parsed
     Failure/Error: file = RBFS::File.parse('string:Hey there')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-txvytv/spec.rb:248: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: file = RBFS::File.parse('string:Hay :)')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-txvytv/spec.rb:255: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 parsed
     Failure/Error: file = RBFS::File.parse('symbol:hello')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-txvytv/spec.rb:274: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 parsed
     Failure/Error: file = RBFS::File.parse('number:1234')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-txvytv/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)>'

  13) RBFS File data type float number can be parsed
     Failure/Error: file = RBFS::File.parse('number:3.14')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-txvytv/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)>'

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

  15) RBFS File data type boolean false can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:false')
     NoMethodError:
       undefined method `parse' for RBFS::File:Class
     # /tmp/d20141111-26053-txvytv/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.04453 seconds
44 examples, 15 failures

Failed examples:

rspec /tmp/d20141111-26053-txvytv/spec.rb:149 # RBFS Directory can create empty directory
rspec /tmp/d20141111-26053-txvytv/spec.rb:76 # RBFS Directory serialization #serialize can serialize
rspec /tmp/d20141111-26053-txvytv/spec.rb:84 # RBFS Directory serialization #serialize can serialize multiple directories recursively
rspec /tmp/d20141111-26053-txvytv/spec.rb:100 # RBFS Directory serialization ::parse can parse empty directories
rspec /tmp/d20141111-26053-txvytv/spec.rb:107 # RBFS Directory serialization ::parse can parse directories with files
rspec /tmp/d20141111-26053-txvytv/spec.rb:116 # RBFS Directory serialization ::parse can parse directory trees without files
rspec /tmp/d20141111-26053-txvytv/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-txvytv/spec.rb:229 # RBFS File data type nil can be parsed
rspec /tmp/d20141111-26053-txvytv/spec.rb:247 # RBFS File data type string can be parsed
rspec /tmp/d20141111-26053-txvytv/spec.rb:254 # RBFS File data type string can parse a string with colons
rspec /tmp/d20141111-26053-txvytv/spec.rb:273 # RBFS File data type symbol can be parsed
rspec /tmp/d20141111-26053-txvytv/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-txvytv/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-txvytv/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-txvytv/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Александър обнови решението на 10.11.2014 12:53 (преди около 10 години)

+module RBFS
+ class File
+ attr_accessor :data
+
+ def initialize(*args)
+ raise ArgumentError if args.size > 1
+ @data = case args[0]
+ when String, FalseClass, TrueClass, Symbol, Fixnum, Integer, Float then args[0]
+ end
+ end
+
+ def data_type
+ case
+ when @data.kind_of?(Numeric) then :number
+ when (@data == true or @data == false) then :boolean
+ when @data == nil then :nil
+ else @data.class.name.downcase.to_sym
+ end
+ end
+
+ def serialize
+ "#{data_type.to_s}:#{@data.to_s}"
+ end
+
+ def to_n(temp)
+ if temp[1].include? "." then
+ newFile = RBFS::File.new(temp[1].to_f)
+ else
+ newFile = RBFS::File.new(temp[1].to_i)
+ end
+ end
+
+ def parse(string_data)
+ temp = string_data.split(/: */)
+ case temp[0]
+ when "nil" then newFile = RBFS::File.new
+ when "string" then newFile = RBFS::File.new(temp[1])
+ when "boolean" then newFile = RBFS::File.new(temp[1].to_bool)
+ when "symbol" then newFile = RBFS::File.new(temp[1].to_sym)
+ when "number" then to_n(temp)
+ end
+ end
+
+ def ser_len
+ serialize.length
+ end
+ end
+
+ class Directory
+ attr_reader :files, :directories
+
+ def initialize
+ @files = {}
+ @directories = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, *directory)
+ @directories[name] = {} if directory == nil
+ @directories[name] = directory[0]
+ end
+
+ def [](*name)
+ if @directories.has_key?(name[0])
+ @directories[name[0]]
+ elsif @files.has_key?(name[0])
+ @files[name[0]]
+ else
+ nil
+ end
+ end
+
+ def len
+ result = @files.size.to_s.length + 1 + @directories.size.to_s.length + 1
+ @files.each do |name, file|
+ result += name.size + file.ser_len.to_s.size + file.ser_len + 3
+ end
+ @directories.each { |name, dir| result += name.size + 2 + dir.len }
+ result
+ result if @directories.size == 0 or @directories.has_value? nil
+ end
+
+ def serialize
+ serial_str = ""
+ serial_str << @files.size.to_s + ":"
+ @files.each { |name, file| serial_str << "#{name}:#{file.ser_len}:#{file.serialize}"}
+ serial_str << @directories.size.to_s + ":"
+ @directories.each { |name, dir| serial_str << "#{name}:#{len}" + dir.serialize }
+ serial_str << "#{name}:4:0:0:" if @directories.has_value? nil
+ serial_str
+ end
+
+ def parse(string_data)
+ end
+ end
+end
+class String
+ def to_bool
+ return true if self == "true"
+ return false if self == "false"
+ end
+end