Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
sealed class Dumper
{
static public string Dump(Node root)
{
var sb = new StringBuilder();
Action<Node, string> action = null;
action = (node, indent) =>
{
// Precondition: indentation and prefix has already been output
// Precondition: indent is correct for node's *children*
sb.AppendLine(node.Text);
for (int i = 0; i < node.Children.Count; ++i)
{
bool last = i == node.Children.Count - 1;
var child = node.Children[i];
sb.Append(indent);
sb.Append(last ? '└' : '├');
sb.Append('─');
action(child, indent + (last ? " " : "│ "));
}
};
action(root, "");
return sb.ToString();
}
}
static public string Dump(Node root)
{
StringBuilder result = new StringBuilder();
Action<Node, string> action = null;
action = (node, indent) =>
{
result.AppendLine(node.Text);
for (int i = 0, lim = node.Children.Count, last=lim-1; i < lim; ++i)
{
string now = indent + "├─", next = indent + "│ ";
if (i == last)
{
now = indent + "└─";
next = indent + " ";
}
result.Append(now);
action(node.Children[i], next);
}
};
action(root, String.Empty);
return result.ToString();
}
* This source code was highlighted with Source Code Highlighter.static public string Dump(Node root)
{
return string.Join("",
new[] { root.Text + "\n" }.Concat(
root.Children.Take(root.Children.Count - 1).Select(Dump).Select(x => "├─" + x.TrimEnd().Replace("\n", "\n│ ")+"\n"))
.Concat(root.Children.Skip(root.Children.Count - 1).Select(Dump).Select(x => "└─" + x.TrimEnd().Replace("\n", "\n ")+"\n"))
.ToArray());
}
* This source code was highlighted with Source Code Highlighter.static public string Dump(Node root)
{
Func<Func<IEnumerable<Node>, int, IEnumerable<Node>>, string, string, Func<IEnumerable<Node>, IEnumerable<string>>> func =
(take, a, b) => source => take(source, root.Children.Count - 1).Select(Dump)
.Select(x => string.Format("{0}─{1}\n", a, x.TrimEnd().Replace("\n", "\n"+b+" ")));
return string.Join("",
new[] { root.
Text + "\n" }.Concat(
new []{func(Enumerable.Take,"├","│"),func(Enumerable.Skip,"└"," ")}.SelectMany(f => f(root.Children)))
.ToArray());
}
* This source code was highlighted with Source Code Highlighter.
Мини-задачка: «олд-скульное» дерево