Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to group groupings of groupings?

So recently I ran into a problem, my team and I need to take a list of objects, and group them by conditions, then that group by more conditions, then that group by even more conditions, and so on for 7 or so levels. After thinking on it for a few days I finally came up with sort of a tree structure, although each level is manually defined (mainly for ease of reading, because once it is programed it will be set in stone). What is the best method to handle for this, and if possible, why? Here’s what I have so far using a list of random integers. The checks are: divisible by 2, divisible by 3, and divisible by 5 in that order (although the order of conditions don’t matter for the requirements):

Here's the code for the random list of integers plus the TopNode class

Random rand = new Random();
List<int> ints = new List<int>();
for (int i = 0; i < 10; i++)
{
   ints.Add(rand.Next(0, 10000001));
}

TopNode node = new TopNode(ints);

Here's the rest of the code for the top node class

public class TopNode 
{ 
    public Even Even { get; set; }
    public Odd Odd { get; set; }
    public TopNode(List<int> ints)
    {
        var even = ints.Where(x => x % 2 == 0).ToList();
        var odd = ints.Where(x => x % 2 != 0).ToList();
        if (even.Count > 0)
        {
            Even = new Even(even);
        }
        if (odd.Count > 0)
        {
            Odd = new Odd(odd);
        }
    }
} 

public class Even {
    public Mulitple3 Mulitple3 { get; set; }
    public NotMulitple3 NotMulitple3 { get; set; }
    public Even(List<int> ints)
    {
        var multiple = ints.Where(x => x % 3 == 0).ToList();
        var not = ints.Where(x => x % 3 != 0).ToList();
        if (multiple.Count > 0)
        {
            Mulitple3 = new Mulitple3(multiple);
        }
        if (not.Count > 0)
        {
            NotMulitple3 = new NotMulitple3(not);
        }
    }
} 

public class Odd {
    public Mulitple3 Mulitple3 { get; set; }
    public NotMulitple3 NotMulitple3 { get; set; }
    public Odd(List<int> ints)
    {
        var multiple = ints.Where(x => x % 3 == 0).ToList();
        var not = ints.Where(x => x % 3 != 0).ToList();
        if (multiple.Count > 0)
        {
            Mulitple3 = new Mulitple3(multiple);
        }
        if (not.Count > 0)
        {
            NotMulitple3 = new NotMulitple3(not);
        }
    }
}

public class Mulitple3
{
    public Multiple5 Multiple5 { get; set; }
    public NotMultiple5 NotMultiple5 { get; set; }
    public Mulitple3(List<int> ints)
    {
        var multiple = ints.Where(x => x % 5 == 0).ToList();
        var not = ints.Where(x => x % 5 != 0).ToList();
        if (multiple.Count > 0)
        {
            Multiple5 = new Multiple5(multiple);
        }
        if (not.Count > 0)
        {
            NotMultiple5 = new NotMultiple5(not);
        }
    }
}

public class NotMulitple3
{
    public Multiple5 Multiple5 { get; set; }
    public NotMultiple5 NotMultiple5 { get; set; }
    public NotMulitple3(List<int> ints)
    {
        var multiple = ints.Where(x => x % 5 == 0).ToList();
        var not = ints.Where(x => x % 5 != 0).ToList();
        if (multiple.Count > 0)
        {
            Multiple5 = new Multiple5(multiple);
        }
        if (not.Count > 0)
        {
            NotMultiple5 = new NotMultiple5(not);
        }
    }
}

public class Multiple5
{
    public List<int> ints { get; set; }
    public Multiple5(List<int> ints)
    {
        this.ints = ints;
    }
}

public class NotMultiple5
{
    public List<int> ints { get; set; }
    public NotMultiple5(List<int> ints)
    {
        this.ints = ints;
    }
}
like image 813
Madmmoore Avatar asked Nov 20 '25 11:11

Madmmoore


1 Answers

The simplest tree is just an IEnumerable<IEnumerable<...>> and you can form it using GroupBy.

Here's a simple example that groups some integers into a tree based on divisibility by 2, 3 and 5. It prints:

{{{{1,7,23,29},{5}},{{3,9,87,21}}},{{{4,8,34,56}},{{78},{30}}}}

.

public static void Main()
{
    int[] input = new int[]{1, 3, 4, 5, 7, 8, 9, 23, 34, 56, 78, 87, 29, 21, 2*3*5};

    // TREE
    var groupedTree = input.GroupBy(x => x % 2 == 0)
        .Select(g => g.GroupBy(x => x % 3 == 0)
        .Select(h => h.GroupBy(x => x % 5 == 0)));

    Console.WriteLine(Display(groupedTree));
}

// Hack code to dump the tree
public static string DisplaySequence(IEnumerable items) => "{" + string.Join(",", items.Cast<object>().Select(x => Display(x))) + "}";
public static string Display(object item) => item is IEnumerable seq ? DisplaySequence(seq) : item.ToString();
like image 106
Ian Mercer Avatar answered Nov 22 '25 00:11

Ian Mercer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!