none
c# find number to group correlation RRS feed

  • Question

  • i have ranges of numbers that can change, for example

    group 1: 1->10000

    group 2: 10001->25000 and so on....

    I get a number 23212

    I want to know that it belongs to group 2

    what the best way to do it?

    Friday, May 17, 2019 7:58 PM

All replies

  • If you have a list of range for your groups, you can use a WHILE statement to move down at end of your list asking for if you number is between the actual range somethin like this.

    --Estructura lista de rango

    struct rango {

     id_group int;

     init_val int;

    end_val int;

    };

    List<rango> MyRanges() = new List()....

    int number = 23212;

    int grupo =0;

     while(grupo==0 && MyRanges!=end) {

      if(number>=MyRanges.init_val && number<=MyRanges.end_val)

       {

         grupo = MyRanges.Index;

       }

    }

    Something like that, of course some syntax is not good. I hope can help you.

    Friday, May 17, 2019 8:24 PM
  • If you know the ranges are contiguous (no gaps), then all you have to store is the upper limit.  You can stop the loop as soon as your number is below the upper limit of group N.

    using System;
    using System.Collections.Generic;
    
    class one
    {
        static public void Main()
        {
            int target = 32123;
            List<int> ranges = new List<int>(){10000,25000,37000};
            for( var i = 0; i < ranges.Count; i++ )
            {
                if( target <= ranges[i] )
                {
                    Console.WriteLine( "Found {0} in range {1}", target, i );
                    break;
                }
            }
        }
    }
    


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, May 17, 2019 9:09 PM
  • i look for a solution how to do it in Linq

    for example :

    var index1 = arrayRanges.TakeWhile((p, i) => 33 < p)
                          .Count() + 1;

    th problem is that if I look for a number bigger then tha last cell in the array

    I will still get the last cell index

    i

    Sunday, May 19, 2019 9:10 AM
  • Hi 

    Thank you for posting here.

    Based on your description, you want to find the number to group correlation.

    I make a simple code, you could have a look.

    class Program
        {
            static void Main(string[] args)
            {
                List<int> list = new List<int>();
                for (int i = 0; i < 25000; i++)
                {
                    list.Add(i + 1);
                }
                var arrange1 = list.Where(x => x.IsBetween(1, 10000)).ToList();
                var arrange2 = list.Where(x => x.IsBetween(10001, 25000)).ToList();
                int m = 23212;
                List<List<int>> result = new List<List<int>>() {arrange1,arrange2 };
                int count = 1;
                foreach (var item in result)
                {
                    var test = item.Where(p => p == m).Count();
                    if (test > 0)
                    {
                        Console.WriteLine("m's value:{0} belongs to arrange{1}", m, count);
                    }
                    count++;
                }
    
                Console.ReadKey();
    
    
            }
        }
    
        public static class Extensions
        {
            public static bool IsBetween(this int value, int min, int max)
            {
                return value >= min && value <= max;
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by zarahhhh Thursday, May 23, 2019 9:18 AM
    Monday, May 20, 2019 2:51 AM
    Moderator