How to resolve the algorithm Display an outline as a nested table step by step in the Julia programming language
How to resolve the algorithm Display an outline as a nested table step by step in the Julia programming language
Table of Contents
Problem Statement
The graphic representation of outlines is a staple of mind-mapping and the planning of papers, reports, and speeches. Given a outline with at least 3 levels of indentation, for example: write a program in your language which translates your outline into a nested table, with WikiTable or HTML colspan values attached (where needed) to parent nodes in the nested table. The WikiTable at the top of this page was generated from the indented outline shown above, producing the following markup string: Use background color to distinguish the main stages of your outline, so that the subtree of each node at level two is consistently colored, and the edges between adjacent subtrees are immediately revealed.
Display your nested table on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Display an outline as a nested table step by step in the Julia programming language
The provided Julia code is a program that takes a text string and displays it as a nested table. The table is constructed by parsing the text into a tree structure, where each line of text represents a node in the tree. The indentation of each line is used to determine the depth of the node in the tree. The code then calculates the width of each node in the tree, which is defined as the sum of the widths of its children. Finally, the code writes out the table with 'colspan' values that indicate the width of each node.
Here is a detailed breakdown of the code:
- The
processtable
function takes a text string as input and returns a DataFrame object. The DataFrame contains the following columns:TEXT
: The text of each line in the input string.INDENT
: The indentation of each line in the input string.COLSPAN
: The width of each node in the tree.PRESPAN
: The number of columns that should be spanned before the text of each node is displayed.LEVELONEPARENT
: The index of the parent node of each node in the tree.
- The
htmlfromdataframe
function takes a DataFrame object as input and writes out a HTML table. The table is formatted so that the nodes in the tree are displayed in a nested fashion. The width of each node is indicated by thecolspan
attribute. Thestyle
attribute is used to add color to the nodes.
Here is an example of how to use the code:
using DataFrames
text = """
Display an outline as a nested table.
Parse the outline to a tree,
measuring the indent of each line,
translating the indentation to a nested structure,
and padding the tree to even depth.
count the leaves descending from each node,
defining the width of a leaf as 1,
and the width of a parent node as a sum.
(The sum of the widths of its children)
and write out a table with 'colspan' values
either as a wiki table,
or as HTML.
"""
df = processtable(text)
htmlfromdataframe(df)
This code will produce a HTML table that looks like this:
<h4>A Rosetta Code Nested Table</h4>
<table style="width:100%" class="wikitable" >
<tr>
<td>Display an outline as a nested table.</td>
</tr>
<tr>
<td> </td>
<td colspan="3">Parse the outline to a tree,</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td colspan="2">measuring the indent of each line,</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>translating the indentation to a nested structure,</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>padding the tree to even depth.</td>
</tr>
<tr>
<td> </td>
<td colspan="3">count the leaves descending from each node,</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td colspan="2">defining the width of a leaf as 1,</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>and the width of a parent node as a sum. (The sum of the widths of its children)</td>
</tr>
<tr>
<td> </td>
<td colspan="3">and write out a table with 'colspan' values</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td colspan="2">either as a wiki table,</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>or as HTML.</td>
</tr>
</table>
Source code in the julia programming language
using DataFrames
text = """
Display an outline as a nested table.
Parse the outline to a tree,
measuring the indent of each line,
translating the indentation to a nested structure,
and padding the tree to even depth.
count the leaves descending from each node,
defining the width of a leaf as 1,
and the width of a parent node as a sum.
(The sum of the widths of its children)
and write out a table with 'colspan' values
either as a wiki table,
or as HTML.
"""
const bcolor = ["background: #ffffaa;", "background: #ffdddd;",
"background: #ddffdd;", "background: #ddddff;"]
colorstring(n) = bcolor[n == 1 ? 1 : mod1(n - 1, length(bcolor) - 1) + 1]
function processtable(txt)
df = DataFrame()
indents = Int[]
linetext = String[]
for line in split(txt, "\n")
if length(line) > 0
n = findfirst(!isspace, line)
push!(linetext, String(line[n:end]))
push!(indents, n - 1)
end
end
len = length(indents)
divisor = gcd(indents)
indents .= div.(indents, divisor)
parent(i) = (n = findlast(x -> indents[x] < indents[i], 1:i-1)) == nothing ? 0 : n
children(i) = findall(x -> parent(x) == i, 1:len)
treesize(i) = (s = children(i); isempty(s) ? 1 : sum(treesize, s))
prioronlevel(i) = (j = indents[i]; filter(x -> indents[x] == j, 1:i-1))
treesizeprior(i) = (s = prioronlevel(i); isempty(s) ? 0 : sum(treesize, s))
startpos(i) = (n = parent(i)) == 0 ? 0 : treesizeprior(n) - treesizeprior(i)
function leveloneparent(i)
p = parent(i)
return p < 1 ? 1 : p ==1 ? sum(x -> indents[x] <= 1, 1:i) : leveloneparent(p)
end
df.TEXT = linetext
df.INDENT = indents
df.COLSPAN = [treesize(i) for i in 1:len]
df.PRESPAN = [max(0, startpos(i)) for i in 1:len]
df.LEVELONEPARENT = [leveloneparent(i) for i in 1:len]
return df
end
function htmlfromdataframe(df)
println("<h4>A Rosetta Code Nested Table</h4><table style=\"width:100%\" class=\"wikitable\" >")
for ind in minimum(df.INDENT):maximum(df.INDENT)
println("<tr>")
for row in eachrow(df)
if row[:INDENT] == ind
if row[:PRESPAN] > 0
println("<td colspan=\"$(row[:PRESPAN])\"> </td>")
end
print("<td ")
if row[:COLSPAN] > 0
println("colspan=\"$(row[:COLSPAN])\"")
end
println(" style = \"$(colorstring(row[:LEVELONEPARENT]))\" >$(row[:TEXT])</td>")
end
end
println("</tr>")
end
println("</table>")
end
htmlfromdataframe(processtable(text))
textplus = text * " Optionally add color to the nodes."
htmlfromdataframe(processtable(textplus))
You may also check:How to resolve the algorithm Wordle comparison step by step in the Go programming language
You may also check:How to resolve the algorithm Fast Fourier transform step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the HolyC programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the Go programming language
You may also check:How to resolve the algorithm Associative array/Merging step by step in the Phix programming language