# Fun Quiz with C#

I have not written code in C# in a long time lately I have been consumed with Java, SpingBoot and Scala, when I found this a fun quiz I decided to code it in C#. The Quiz states the following given a list of line as follows

We want to emit HTML that would represent the above as bolts as follows:

The list can contain more nested levels than 5.

# Solution Algorithm

Provided we took care of the converting the strings into a list of classes Heading . containing the weight (1 -,2,3 ..) and text. The problem becomes going through the list one by one and creating a list of subtrees. The imaginary item with H0 is the root of the tree. All H1 nodes are subtrees of the main tree.

The alogrithm goes like this

• For each item in the Headings list if the next item in the list has a weight higher than the current item
• Add the next item, and its children as children of the current node.

The code in C# below shows the solution heart for this Quiz.

private

{

null;

Node currentNode = null;

Node root = new Node(new Heading(0, “”));

currentNode = root;

int currentWeight = 0;

for (int i = 0; i < headings.Count;)

{

/// check if the next heading

{

}

}

return root;

}

private
int i)

{

List<Node> result = new List<Node>();

if (headings.Count <= 0) return result;

{

{

}

}

return result;

}

## Complete Code

Below is the complete code, the test class checks two cases on header and one with multiple cases :

[TestClass]

public
class
UnitTest1

{

[TestMethod]

public
void TestOutlineSingle()

{

string[] lines = {“H1 header 1”};

Debug.WriteLine( nodes.Solution.Driver(lines));

}

[TestMethod]

public
void TestOutlineMultiple()

{

string[] lines = {“H1 header 1”,

};

Debug.WriteLine(nodes.Solution.Driver(lines));

}

}

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

using System.Text;

namespace nodes

{

class

{

int weight;

String text;

public
int Weight

{

get { return weight; }

}

public String Text

{

get { return text; }

}

{

this.weight = weight;

this.text = text;

}

public
override
string ToString()

{

return

;

}

}

class
Node

{

List<Node> children;

{

}

public List<Node> Children

{

get { return children; }

}

{

this.children = new List<Node>();

}

}

public
class
Solution

{

public
static
string Driver(string[] lines)

{

foreach (var line in lines)

{

}

var html = toHtml(outline);

return html;

}

private

{

null;

Node currentNode = null;

Node root = new Node(new Heading(0, “”));

currentNode = root;

int currentWeight = 0;

for (int i = 0; i < headings.Count;)

{

if (i >= headings.Count) break; ;

{

}

}

return root;

}

private
int i)

{

List<Node> result = new List<Node>();

if (headings.Count <= 0) return result;

{

{

}

}

return result;

}

/** Parses a line of input.

This implementation is correct for all predefined test cases. */

private

{

String[] parts = record.Split(” “.ToCharArray(), 2);

int weight = Int32.Parse(parts.Substring(1));

}

/** Converts a node to HTML.

This implementation is correct for all predefined test cases. */

private
static String toHtml(Node node)

{

StringBuilder buf = new StringBuilder();

{

buf.Append(“\n”);

}

if (node.Children.Count > 0)

{

buf.Append(“<ol>”);

var childStrings = node.Children.Select(child =>

“<li>” + toHtml(child) + “</li>”

);

buf.Append(String.Join(“\n”, childStrings));

buf.Append(“</ol>”);

}

return buf.ToString();

}

}

}