Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. /* LINQ Puzzle #10 *******************************************************
  2.  
  3. Summary:  Assume we have a generic class that forms a tree like the Node class below.
  4. Given an IEnumerable of Nodes, write an extension method that flattens all the nodes
  5. of the tree(s) into a single list of nodes. The method MUST capable of accepting a
  6. generic class. The only assumption you can make is that it has some property that
  7. contains an IEnumerable of children of the same type.
  8.  
  9. ************************************************************************/
  10.  
  11. void Main()
  12. {
  13.     var nodes = Enumerable.Range(0, 3).Select(x => new Node() {
  14.         Children = Enumerable.Range(0, 3).Select(y => new Node() {
  15.             Children = Enumerable.Range(0, 1).Select(z => new Node()).ToList()
  16.         }).ToList()
  17.     }).ToList();
  18.    
  19.     // call your extension method here
  20. }
  21.  
  22. public class Node {
  23.     public Node()
  24.     {
  25.         Guid = Guid.NewGuid();
  26.     }
  27.    
  28.     public Guid Guid { get; set; }
  29.    
  30.     public IEnumerable<Node> Children { get; set; }
  31. }
  32.  
  33. // write your extension method here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement