Решение на Трета задача от Петя Петрова

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

Към профила на Петя Петрова

Резултати

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

Код

module RBFS
class File
attr_accessor :data
def self.parse(string_data)
type, data = arg.to_s.split(':')
new(data)
end
def initialize (data = nil)
@data = data
end
def data_type
case @data
when NilClass then :nil
when String then :string
when Symbol then :symbol
when Integer, Number then :number
when TrueClass, FalseClass then :boolean
end
end
def serialize
"#{data_type}:#{@data}"
end
end
class Directory
attr_reader :files
attr_reader :directories
def self.parse(string_data)
end
def initialize
@directories = {}
@files = {}
end
def add_file(name, file)
@files[name] = file
end
def add_directory(name, directory = Directory.new)
@directories[name] = directory
end
def [](name)
@directories[name] or @files[name]
end
def ser_files
ser_files = "#{@files.size}:"
@files.each do |name, file|
ser_files = "#{ser_files}#{name}:#{file.serialize.size}:#{file.serialize}"
end
ser_files
end
def ser_dirs(content)
content = "#{content}#{@directories.size}:"
@directories.each do |name, dir|
content = "#{content}#{name}:#{dir.serialize.size}:#{dir.serialize}"
end
content
end
def serialize
"#{ser_dirs(ser_files)}"
end
end
end

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

.........FFFFF..........F..FF..F..FFFFFFFFFF

Failures:

  1) RBFS Directory serialization #serialize can serialize multiple directories recursively
     Failure/Error: expect(directory.serialize).to eq recursive_serialized_string
     NameError:
       uninitialized constant RBFS::File::Number
     # /tmp/d20141111-26053-du6pd9/solution.rb:19:in `data_type'
     # /tmp/d20141111-26053-du6pd9/solution.rb:25:in `serialize'
     # /tmp/d20141111-26053-du6pd9/solution.rb:56:in `block in ser_files'
     # /tmp/d20141111-26053-du6pd9/solution.rb:55:in `each'
     # /tmp/d20141111-26053-du6pd9/solution.rb:55:in `ser_files'
     # /tmp/d20141111-26053-du6pd9/solution.rb:70:in `serialize'
     # /tmp/d20141111-26053-du6pd9/solution.rb:64:in `block in ser_dirs'
     # /tmp/d20141111-26053-du6pd9/solution.rb:63:in `each'
     # /tmp/d20141111-26053-du6pd9/solution.rb:63:in `ser_dirs'
     # /tmp/d20141111-26053-du6pd9/solution.rb:70:in `serialize'
     # /tmp/d20141111-26053-du6pd9/solution.rb:64:in `block in ser_dirs'
     # /tmp/d20141111-26053-du6pd9/solution.rb:63:in `each'
     # /tmp/d20141111-26053-du6pd9/solution.rb:63:in `ser_dirs'
     # /tmp/d20141111-26053-du6pd9/solution.rb:70:in `serialize'
     # /tmp/d20141111-26053-du6pd9/spec.rb:95: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 empty directories
     Failure/Error: expect(parsed_directory.files      ).to eq({})
     NoMethodError:
       undefined method `files' for nil:NilClass
     # /tmp/d20141111-26053-du6pd9/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)>'

  3) 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-du6pd9/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)>'

  4) 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-du6pd9/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)>'

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

  6) RBFS File data type nil can be parsed
     Failure/Error: file = RBFS::File.parse('nil:')
     NameError:
       undefined local variable or method `arg' for RBFS::File:Class
     # /tmp/d20141111-26053-du6pd9/solution.rb:6:in `parse'
     # /tmp/d20141111-26053-du6pd9/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)>'

  7) RBFS File data type string can be parsed
     Failure/Error: file = RBFS::File.parse('string:Hey there')
     NameError:
       undefined local variable or method `arg' for RBFS::File:Class
     # /tmp/d20141111-26053-du6pd9/solution.rb:6:in `parse'
     # /tmp/d20141111-26053-du6pd9/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)>'

  8) RBFS File data type string can parse a string with colons
     Failure/Error: file = RBFS::File.parse('string:Hay :)')
     NameError:
       undefined local variable or method `arg' for RBFS::File:Class
     # /tmp/d20141111-26053-du6pd9/solution.rb:6:in `parse'
     # /tmp/d20141111-26053-du6pd9/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)>'

  9) RBFS File data type symbol can be parsed
     Failure/Error: file = RBFS::File.parse('symbol:hello')
     NameError:
       undefined local variable or method `arg' for RBFS::File:Class
     # /tmp/d20141111-26053-du6pd9/solution.rb:6:in `parse'
     # /tmp/d20141111-26053-du6pd9/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)>'

  10) RBFS File data type number can be parsed
     Failure/Error: file = RBFS::File.parse('number:1234')
     NameError:
       undefined local variable or method `arg' for RBFS::File:Class
     # /tmp/d20141111-26053-du6pd9/solution.rb:6:in `parse'
     # /tmp/d20141111-26053-du6pd9/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)>'

  11) RBFS File data type float number can be detected
     Failure/Error: expect(file.data_type).to eq :number
     NameError:
       uninitialized constant RBFS::File::Number
     # /tmp/d20141111-26053-du6pd9/solution.rb:19:in `data_type'
     # /tmp/d20141111-26053-du6pd9/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)>'

  12) RBFS File data type float number can be serialized
     Failure/Error: expect(file.serialize).to eq 'number:666.6'
     NameError:
       uninitialized constant RBFS::File::Number
     # /tmp/d20141111-26053-du6pd9/solution.rb:19:in `data_type'
     # /tmp/d20141111-26053-du6pd9/solution.rb:25:in `serialize'
     # /tmp/d20141111-26053-du6pd9/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)>'

  13) RBFS File data type float number can be parsed
     Failure/Error: file = RBFS::File.parse('number:3.14')
     NameError:
       undefined local variable or method `arg' for RBFS::File:Class
     # /tmp/d20141111-26053-du6pd9/solution.rb:6:in `parse'
     # /tmp/d20141111-26053-du6pd9/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 detected
     Failure/Error: expect(file.data_type).to eq :boolean
     NameError:
       uninitialized constant RBFS::File::Number
     # /tmp/d20141111-26053-du6pd9/solution.rb:19:in `data_type'
     # /tmp/d20141111-26053-du6pd9/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)>'

  15) RBFS File data type boolean true can be serialized
     Failure/Error: expect(file.serialize).to eq 'boolean:true'
     NameError:
       uninitialized constant RBFS::File::Number
     # /tmp/d20141111-26053-du6pd9/solution.rb:19:in `data_type'
     # /tmp/d20141111-26053-du6pd9/solution.rb:25:in `serialize'
     # /tmp/d20141111-26053-du6pd9/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)>'

  16) RBFS File data type boolean true can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:true')
     NameError:
       undefined local variable or method `arg' for RBFS::File:Class
     # /tmp/d20141111-26053-du6pd9/solution.rb:6:in `parse'
     # /tmp/d20141111-26053-du6pd9/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)>'

  17) RBFS File data type boolean false can be detected
     Failure/Error: expect(file.data_type).to eq :boolean
     NameError:
       uninitialized constant RBFS::File::Number
     # /tmp/d20141111-26053-du6pd9/solution.rb:19:in `data_type'
     # /tmp/d20141111-26053-du6pd9/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)>'

  18) RBFS File data type boolean false can be serialized
     Failure/Error: expect(file.serialize).to eq 'boolean:false'
     NameError:
       uninitialized constant RBFS::File::Number
     # /tmp/d20141111-26053-du6pd9/solution.rb:19:in `data_type'
     # /tmp/d20141111-26053-du6pd9/solution.rb:25:in `serialize'
     # /tmp/d20141111-26053-du6pd9/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)>'

  19) RBFS File data type boolean false can be parsed
     Failure/Error: file = RBFS::File.parse('boolean:false')
     NameError:
       undefined local variable or method `arg' for RBFS::File:Class
     # /tmp/d20141111-26053-du6pd9/solution.rb:6:in `parse'
     # /tmp/d20141111-26053-du6pd9/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.05546 seconds
44 examples, 19 failures

Failed examples:

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

История (3 версии и 4 коментара)

Петя обнови решението на 08.11.2014 00:14 (преди около 10 години)

+module RBFS
+ class File
+ attr_accessor :data
+
+ def initialize (data = nil)
+ @data = data
+ end
+
+ def data_type
+ case @data
+ when NilClass then :nil
+ when String then :string
+ when Symbol then :symbol
+ when Integer, Number then :number
+ when TrueClass, FalseClass then :boolean
+ end
+ end
+
+ def serialize
+ self.data_type + '#{@data}'
+ end
+
+ def self.parse(string_data)
+ type, data = arg.to_s.split(':')
+ new(data)
+ end
+ end
+
+ class Directory
+ attr_accessor :files
+ attr_accessor :directories
+
+ def initialize
+ @directories = {}
+ @files = {}
+ end
+
+ def add_file(name, file)
+ @files[name] = file
+ end
+
+ def add_directory(name, directory)
+ if directory
+ @directories[name] = directory
+ else
+ @directories[name] = Directory.new
+ end
+ end
+
+ def [](name)
+ @directories[name] or @files[name]
+ end
+
+ def serialize
+ end
+
+ def self.parse(string_data)
+ end
+ end
+end

Предполагам, че решението ти е незавършено. Въпреки това, оставям малко бележки:

  • Имаш някакви проблеми с идентацията (отместването) на кода. Виж как ти излиза решението в сайта. Най-вероятно си смесвала табове с интервали.
  • Текущата ти версия на add_directory може да се запише така: @directories[name] = directory || Directory.new.
  • Няма нужда от self. на ред 20. Редът може и е по-добре да се запише така: "#{data_type}#{@data}".
  • Слагай клас-методите (def self.foo) преди инстанционните методи (по конвенция).
  • attr_accessor създава и setter методи, позволявайки промяна отвън на въпросния обект. Това най-вероятно е нежелано поведение в твоя код. Най-много attr_reader. Правилото е: ограничавай публичния интерфейс на обектите си. Не слагай излишни неща там.

Петя обнови решението на 09.11.2014 12:29 (преди около 10 години)

module RBFS
class File
attr_accessor :data
+ def self.parse(string_data)
+ type, data = arg.to_s.split(':')
+ new(data)
+ end
+
def initialize (data = nil)
@data = data
- end
+ end
- def data_type
+ def data_type
case @data
- when NilClass then :nil
- when String then :string
- when Symbol then :symbol
- when Integer, Number then :number
- when TrueClass, FalseClass then :boolean
- end
- end
+ when NilClass then :nil
+ when String then :string
+ when Symbol then :symbol
+ when Integer, Number then :number
+ when TrueClass, FalseClass then :boolean
+ end
+ end
- def serialize
- self.data_type + '#{@data}'
- end
-
- def self.parse(string_data)
- type, data = arg.to_s.split(':')
- new(data)
+ def serialize
+ '#{data_type}#{@data}'
end
end
class Directory
- attr_accessor :files
- attr_accessor :directories
+ attr_reader :files
+ attr_reader :directories
+ def self.parse(string_data)
+ end
+
def initialize
@directories = {}
- @files = {}
+ @files = {}
end
def add_file(name, file)
- @files[name] = file
- end
+ @files[name] = file
+ end
- def add_directory(name, directory)
- if directory
- @directories[name] = directory
- else
- @directories[name] = Directory.new
- end
- end
+ def add_directory(name, directory)
+ @directories[name] = directory || Directory.new
+ end
- def [](name)
- @directories[name] or @files[name]
- end
+ def [](name)
+ @directories[name] or @files[name]
+ end
- def serialize
- end
-
- def self.parse(string_data)
- end
+ def serialize
+ end
end
end

Опитай се за начало да измислиш serialize само за файловете в директорията. Функцията трябва да ти върне просто един стринг, където да имаш броя на файловете, конкатаниран с името, дължината и сериализираното съдържание на всеки файл в тази директория.

За да се справиш със сериализирането на вложените директории ще трябва да извикаш въпросната функция рекурсивно.

П.С

Пусни си примерните тестове!

От друга страна, ако serialize не ти върви, може да се окаже полезно да опиташ да направиш parse. Така е много по-вероятно да осмислиш добре какво трябва да има в един сериализиран низ и защо е точно такава структурата за сериализиране, което пък може да ти помогне да напишеш serialize.

Петя обнови решението на 10.11.2014 15:22 (преди около 10 години)

module RBFS
class File
attr_accessor :data
def self.parse(string_data)
type, data = arg.to_s.split(':')
new(data)
end
def initialize (data = nil)
@data = data
end
def data_type
case @data
when NilClass then :nil
when String then :string
when Symbol then :symbol
when Integer, Number then :number
when TrueClass, FalseClass then :boolean
end
end
def serialize
- '#{data_type}#{@data}'
+ "#{data_type}:#{@data}"
end
end
class Directory
attr_reader :files
attr_reader :directories
def self.parse(string_data)
end
def initialize
@directories = {}
@files = {}
end
def add_file(name, file)
@files[name] = file
end
- def add_directory(name, directory)
- @directories[name] = directory || Directory.new
+ def add_directory(name, directory = Directory.new)
+ @directories[name] = directory
end
def [](name)
@directories[name] or @files[name]
end
+ def ser_files
+ ser_files = "#{@files.size}:"
+ @files.each do |name, file|
+ ser_files = "#{ser_files}#{name}:#{file.serialize.size}:#{file.serialize}"
+ end
+ ser_files
+ end
+
+ def ser_dirs(content)
+ content = "#{content}#{@directories.size}:"
+ @directories.each do |name, dir|
+ content = "#{content}#{name}:#{dir.serialize.size}:#{dir.serialize}"
+ end
+ content
+ end
+
def serialize
+ "#{ser_dirs(ser_files)}"
end
end
end