This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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