Skip to content

Instantly share code, notes, and snippets.

@ssartell

ssartell/10.cs Secret

Created May 15, 2019 17:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ssartell/21cbd3fd5ce51b3c854d2e7079dd6b0e to your computer and use it in GitHub Desktop.
/* LINQ Puzzle #10 *******************************************************
Summary: Assume we have a generic class that forms a tree like the Node class below.
Given an IEnumerable of Nodes, write an extension method that flattens all the nodes
of the tree(s) into a single list of nodes. The method MUST capable of accepting a
generic class. The only assumption you can make is that it has some property that
contains an IEnumerable of children of the same type.
************************************************************************/
void Main()
{
var nodes = Enumerable.Range(0, 3).Select(x => new Node() {
Children = Enumerable.Range(0, 3).Select(y => new Node() {
Children = Enumerable.Range(0, 1).Select(z => new Node()).ToList()
}).ToList()
}).ToList();
// call your extension method here
nodes.FlattenTree(x => x.Children).Dump();
}
public class Node {
public Node()
{
Guid = Guid.NewGuid();
}
public Guid Guid { get; set; }
public IEnumerable<Node> Children { get; set; }
}
// write your extension method here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment