condition for listing

Asked

Viewed 18 times

0

Personal talk. to starting in C# (as you will notice in the question haha) and to with a doubt

I have a list of values list a = [1,2,4,5,9,8,1,4,5,2,10,18]

would like to know if there is any way to count these values based on a rule for example: double group1 = Count. a Where a < 2; double group2 = Count. a Where a> 4 and < 8;

(I know it’s not these commands, it’s just to illustrate the doubt)

I thought of something like

            countElements element = new countElements();
            list a = [1,2,4,5,9,8,1,4,5,2,10,18]

            foreach (var item in a)
            {
                if (item < 2)
                {
                    element.group1 = +1;
                }
                else (item > 4 and item< 8)
                {
                    element.group2 = +1;
                }
            }

            double group1 = element.group1;
            double group2 = element.group2;

thank you since

  • What is and how is this class countElements()?

1 answer

0

Using the System.Linq to facilitate iteration, you can think of an approach presented below:

class Program
{
    static void Main(string[] args)
    {

        var a = new int[] { 1, 2, 4, 5, 9, 8, 1, 4, 5, 2, 10, 18 };

        var countElements = new CountElements();
            countElements.Groups.AddRange(
                new List<Group>
                {
                    new Group {Name= "Grupo 1", Max = 2},
                    new Group {Name= "Grupo 2", Min = 4, Max = 8}
                }
            );

        countElements.CountGroups(a);

        foreach (var g in countElements.Groups)
            Console.WriteLine($"{g.Name } - Elementos({g.GetSum()}) -> {g.GetResult()}");

        Console.Read();

    }
}

public class CountElements
{
    public List<Group> Groups { get; set; } = new List<Group>();
    public void CountGroups(int[] lista)
    {
        if (Groups.Count() > 0 && (lista.Length > 0))
        {
            foreach (var g in Groups)
            {
                var search = lista.Where(x => (g.Min.HasValue && g.Max.HasValue && x > g.Min && x < g.Max)
                                                   || (!g.Min.HasValue && x < g.Max)
                                                   || (!g.Max.HasValue && x > g.Min)
                                           ).ToList();

                g.SetResult(search);
            }
        }
    }
}

public class Group
{
    public string Name { get; set; }
    public int? Min { get; set; }
    public int? Max { get; set; }
    private List<int> Result { get; set; } = new List<int>();

    public void SetResult(List<int> search)
    {
        Result = search;
    }
    public string GetResult()
    {
        return $"[{string.Join(',', Result.ToArray())}]";
    }

    public int GetSum()
    {
        return Result.Count();
    }

}

Output

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.