Решение на Трета задача от Веселин Добрев

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

Към профила на Веселин Добрев

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 39 успешни тест(а)
  • 5 неуспешни тест(а)

Код

module RBFS
class File
attr_reader :data
attr_reader :data_type
def initialize(data = nil)
self.data = data
end
def data=(new_data)
@data = new_data
case new_data
when NilClass then @data_type = :nil
when String then @data_type = :string
when Symbol then @data_type = :symbol
when TrueClass, FalseClass then @data_type = :boolean
when Fixnum, Float then @data_type = :number
end
end
def serialize
"#{@data_type}:#{@data}"
end
def self.parse(string_data)
type, data = string_data.split(':', 2)
case type
when "string" then new(data)
when "symbol" then new(data.to_sym)
when "boolean" then new(check_boolean(data))
when "number" then new(format_num(data))
when "nil" then new(nil)
end
end
def check_boolean(data)
data == "true" ? true : false
end
def format_num(data)
data.include?('.') ? data.to_f : data.to_i
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 = Directory.new)
@directories[name] = directory
end
def [](name)
if @directories.has_key? name then @directories[name]
elsif @files.has_key? name then @files[name]
end
end
def serialize
result = "#{files.length}:"
@files.each do |key, value|
result << "#{key}:#{value.serialize.length}:#{value.serialize}"
end
result << "#{directories.length}:"
@directories.each do |key, value|
result << "#{key}:#{value.serialize.length}:#{value.serialize}"
end
result
end
def self.parse(string_data)
parsed_dir = Directory.new
files, remaining_data = parse_files(string_data)
files.each { |name, file| parsed_dir.add_file(name, file) }
directories = parse_directories(remaining_data)
directories.each { |name, directory| parsed_dir.add_directory(name, directory) }
parsed_dir
end
def self.parse_files(string_data)
files = {}
number_of_files, remaining_data = string_data.split(':', 2)
number_of_files.to_i.times do
file_name, file_length, remaining_data = remaining_data.split(':', 3)
file_contents = remaining_data.slice!(0, file_length.to_i)
files[file_name] = File.parse(file_contents)
end
[files, remaining_data]
end
def self.parse_directories(string_data)
directories = {}
number_of_directories, remaining_data = string_data.split(':', 2)
number_of_directories.to_i.times do
dir_name, dir_length, remaining_data = remaining_data.split(':', 3)
dir_contents = remaining_data.slice!(0, dir_length.to_i)
directories[dir_name]= Directory.parse(dir_contents)
end
directories
end
private_class_method :parse_files, :parse_directories
end
end

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

.............F....................F..F..F..F

Failures:

  1) RBFS Directory serialization ::parse can parse directories recursively
     Failure/Error: parsed_directory = RBFS::Directory.parse(recursive_serialized_string)
     NoMethodError:
       undefined method `check_boolean' for RBFS::File:Class
     # /tmp/d20141111-26053-18pjmby/solution.rb:30:in `parse'
     # /tmp/d20141111-26053-18pjmby/solution.rb:95:in `block in parse_files'
     # /tmp/d20141111-26053-18pjmby/solution.rb:92:in `times'
     # /tmp/d20141111-26053-18pjmby/solution.rb:92:in `parse_files'
     # /tmp/d20141111-26053-18pjmby/solution.rb:82:in `parse'
     # /tmp/d20141111-26053-18pjmby/solution.rb:106:in `block in parse_directories'
     # /tmp/d20141111-26053-18pjmby/solution.rb:103:in `times'
     # /tmp/d20141111-26053-18pjmby/solution.rb:103:in `parse_directories'
     # /tmp/d20141111-26053-18pjmby/solution.rb:84:in `parse'
     # /tmp/d20141111-26053-18pjmby/solution.rb:106:in `block in parse_directories'
     # /tmp/d20141111-26053-18pjmby/solution.rb:103:in `times'
     # /tmp/d20141111-26053-18pjmby/solution.rb:103:in `parse_directories'
     # /tmp/d20141111-26053-18pjmby/solution.rb:84:in `parse'
     # /tmp/d20141111-26053-18pjmby/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)>'

  2) RBFS File data type number can be parsed
     Failure/Error: file = RBFS::File.parse('number:1234')
     NoMethodError:
       undefined method `format_num' for RBFS::File:Class
     # /tmp/d20141111-26053-18pjmby/solution.rb:31:in `parse'
     # /tmp/d20141111-26053-18pjmby/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)>'

  3) RBFS File data type float number can be parsed
     Failure/Error: file = RBFS::File.parse('number:3.14')
     NoMethodError:
       undefined method `format_num' for RBFS::File:Class
     # /tmp/d20141111-26053-18pjmby/solution.rb:31:in `parse'
     # /tmp/d20141111-26053-18pjmby/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)>'

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

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

Failed examples:

rspec /tmp/d20141111-26053-18pjmby/spec.rb:124 # RBFS Directory serialization ::parse can parse directories recursively
rspec /tmp/d20141111-26053-18pjmby/spec.rb:292 # RBFS File data type number can be parsed
rspec /tmp/d20141111-26053-18pjmby/spec.rb:311 # RBFS File data type float number can be parsed
rspec /tmp/d20141111-26053-18pjmby/spec.rb:331 # RBFS File data type boolean true can be parsed
rspec /tmp/d20141111-26053-18pjmby/spec.rb:350 # RBFS File data type boolean false can be parsed

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

Веселин обнови решението на 06.11.2014 21:29 (преди около 10 години)

+module RBFS
+ class File
+ attr_reader :data
+ attr_reader :data_type
+
+ def initialize(data = nil)
+ self.data = data
+ end
+
+ def data=(new_data)
+ @data = new_data
+ case new_data
+ when NilClass then @data_type = :nil
+ when String then @data_type = :string
+ when Symbol then @data_type = :symbol
+ when TrueClass, FalseClass then @data_type = :boolean
+ when Fixnum, Float then @data_type = :number
+ end
+ end
+
+ def serialize
+ "#{@data_type}:#{@data}"
+ end
+
+ def self.parse(string_data)
+ type, data = string_data.split(':')
+ case type
+ when "string" then new(data)
+ when "symbol" then new(data.to_sym)
+ when "boolean" then new(check_boolean(data))
+ when "number" then new(format_num(data))
+ when "nil" then new(nil)
+ end
+ end
+
+ def check_boolean(data)
+ data == "true" ? true : false
+ end
+
+ def format_num(data)
+ data.include?('.') ? data.to_f : data.to_i
+ 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 = Directory.new)
+ @directories[name] = directory
+ end
+
+ def [](name)
+ if @directories.has_key? name then @directories[name]
+ elsif @files.has_key? name then @files[name]
+ end
+ end
+
+ def serialize
+ result = "#{files.length}:"
+ @files.each do |key, value|
+ result << "#{key}:#{value.serialize.length}:#{value.serialize}"
+ end
+ result << "#{directories.length}:"
+ @directories.each do |key, value|
+ result << "#{key}:#{value.serialize.length}:#{value.serialize}"
+ end
+ result
+ end
+
+ def self.parse(string_data)
+ parsed_dir = Directory.new
+ files, remaining_data = parse_files(string_data)
+ files.each { |name, file| parsed_dir.add_file(name, file) }
+ directories = parse_directories(remaining_data)
+ directories.each { |name, directory| parsed_dir.add_directory(name, directory) }
+ parsed_dir
+ end
+
+ def self.parse_files(string_data)
+ files = {}
+ number_of_files, remaining_data = string_data.split(':', 2)
+ number_of_files.to_i.times do
+ file_name, file_length, remaining_data = remaining_data.split(':', 3)
+ file_contents = remaining_data.slice!(0, file_length.to_i)
+ files[file_name] = File.parse(file_contents)
+ end
+ [files, remaining_data]
+ end
+
+ def self.parse_directories(string_data)
+ directories = {}
+ number_of_directories, remaining_data = string_data.split(':', 2)
+ number_of_directories.to_i.times do
+ dir_name, dir_length, remaining_data = remaining_data.split(':', 3)
+ dir_contents = remaining_data.slice!(0, dir_length.to_i)
+ directories[dir_name]= Directory.parse(dir_contents)
+ end
+ directories
+ end
+
+ private_class_method :parse_files, :parse_directories
+ end
+end

Веселин обнови решението на 08.11.2014 16:23 (преди около 10 години)

module RBFS
class File
attr_reader :data
attr_reader :data_type
def initialize(data = nil)
self.data = data
end
def data=(new_data)
@data = new_data
case new_data
when NilClass then @data_type = :nil
when String then @data_type = :string
when Symbol then @data_type = :symbol
when TrueClass, FalseClass then @data_type = :boolean
when Fixnum, Float then @data_type = :number
end
end
def serialize
"#{@data_type}:#{@data}"
end
def self.parse(string_data)
- type, data = string_data.split(':')
+ type, data = string_data.split(':', 2)
case type
when "string" then new(data)
when "symbol" then new(data.to_sym)
when "boolean" then new(check_boolean(data))
when "number" then new(format_num(data))
when "nil" then new(nil)
end
end
def check_boolean(data)
data == "true" ? true : false
end
def format_num(data)
data.include?('.') ? data.to_f : data.to_i
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 = Directory.new)
@directories[name] = directory
end
def [](name)
if @directories.has_key? name then @directories[name]
elsif @files.has_key? name then @files[name]
end
end
def serialize
result = "#{files.length}:"
@files.each do |key, value|
result << "#{key}:#{value.serialize.length}:#{value.serialize}"
end
result << "#{directories.length}:"
@directories.each do |key, value|
result << "#{key}:#{value.serialize.length}:#{value.serialize}"
end
result
end
def self.parse(string_data)
parsed_dir = Directory.new
files, remaining_data = parse_files(string_data)
files.each { |name, file| parsed_dir.add_file(name, file) }
directories = parse_directories(remaining_data)
directories.each { |name, directory| parsed_dir.add_directory(name, directory) }
parsed_dir
end
def self.parse_files(string_data)
files = {}
number_of_files, remaining_data = string_data.split(':', 2)
number_of_files.to_i.times do
file_name, file_length, remaining_data = remaining_data.split(':', 3)
file_contents = remaining_data.slice!(0, file_length.to_i)
files[file_name] = File.parse(file_contents)
end
[files, remaining_data]
end
def self.parse_directories(string_data)
directories = {}
number_of_directories, remaining_data = string_data.split(':', 2)
number_of_directories.to_i.times do
dir_name, dir_length, remaining_data = remaining_data.split(':', 3)
dir_contents = remaining_data.slice!(0, dir_length.to_i)
directories[dir_name]= Directory.parse(dir_contents)
end
directories
end
private_class_method :parse_files, :parse_directories
end
end