$ ruby test.rb
Run options: --seed 11990
Finished in 0.004181s, 239.1899 runs/s, 239.1899 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
Reprising the tree-based solution from Challenge #6:
require 'tree'
def build(txt)
_d(_f(_h(txt))).to_yaml #tap{|y| puts y}
end
def _d(r)
r.children.map do |c|
(g = _d(c)).size > 0 \
? {'title' => c.name[1], 'sections' => g} \
: {'title' => c.name[1]}
end
end
def _f(h, n = Tree::TreeNode.new([0, nil]))
return n.root unless h[0]
c = Tree::TreeNode.new(h[0])
case h[0][0] <=> n.name[0]
when 1
n << c
when 0
n.parent << c
else
n.parent.parent << c
end
_f(h[1..-1], c)
end
def _h(txt)
txt
.lines
.reject{|line| line =~ /^```/ ... line =~ /^```/ ? true : nil}
.grep(/^#/)
.map{|l| l.match(/^(#+)\s+(.*)\s*$/); [$1.length, $2.delete(?")]}
end
···
On 2020-01-24 13:49, Gerald Bauer wrote:
Challenge #16 - quiz/016 at master · planetruby/quiz · GitHub