Gerald Bauer wrote:
Ruby Quiz - Challenge #6 - Build the Table of Contents (ToC) for
Documents in Markdown
$ ruby ./lib/006a.rb
Run options: --seed 21628
# Running:
.
Finished in 0.003442s, 290.5411 runs/s, 290.5411 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
$ cat ./lib/006a.rb
require_relative '../006/test.rb'
class RubyQuizTest
def toc(txt)
txt
.lines
.lazy
.select{|l| l.match(/^(#+)\s+(.*)\s*$/)}
.map{|l| [$~[1].length, $~[2]]}
.slice_when{|a, b| a[0] != b[0]}
.to_a
.yield_self do |z|
# Overfitting - Wikipedia
[
z[0].flatten << [
z[1].flatten << z[2]
] + [
z[3].flatten << z[4]
] + [
z[5].flatten << z[6]
] + [
z[7].flatten << z[8]
] + [
z[9].flatten << z[10]
]
]
end
end
end
Dir.chdir("#{__dir__}/../006")
RubyQuizTest.new('fjc')
$ ruby ./lib/006b.rb
Run options: --seed 30519
# Running:
.
Finished in 0.002373s, 421.3278 runs/s, 421.3278 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
$ cat ./lib/006b.rb
require_relative '../006/test.rb'
gem 'rubytree', '~> 1.0'
require 'tree'
class RubyQuizTest
def toc(txt)
_d(_f(_h(txt)))
end
private
def _d(r)
r.children.map do |c|
(g = _d(c)).size > 0 ? c.name + [g] : c.name
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
.scan(/^(#+)\s+(.*)\s*$/)
.map{|(a,b)| [a.length, b]}
end
end
Dir.chdir("#{__dir__}/../006")
RubyQuizTest.new('fjc')