Specifying Ranges in C#

后端 未结 3 1038
遇见更好的自我
遇见更好的自我 2021-01-23 02:46

I have 6 Ranges:

1000000-5000000
50000001-10000000
10000001-25000000
25000001-50000000
50000001-75000000
75000001-100000000

Now how do I say th

相关标签:
3条回答
  • 2021-01-23 03:09

    If your ranges are continuous like in the examples you gave, then please note that you do not need any 'interval'.

    Continuous ranges like 1-9, 10-19, 20-29 actually define a "threshold points": 9|10, 19|20 and so on. Instead of checking a 1-9, 10-19, 20-29 ranges, you may simply:

    if ( x <= 9 )
       ...
    else if ( x <= 19 )
       ...
    else if ( x <= 29 )
       ...
    

    Note that the else part guarantees you the lower bound in each case.

    EDIT:

    You've updated your code with result = 10 and etc. If you really need only such simple operation, then you can define a list:

    var levelsAndValues = List<Tuple<int, int>>();
    levelsAndValues.Add(Tuple.Create(5000000, 10));
    levelsAndValues.Add(Tuple.Create(10000000, 20));
    ...
    

    and run a simple loop over it:

    int limit = 1000000;
    int result = 0;
    foreach(var level in levelsAndValues)
        if(limit > level.Item1)
            result = level.Item2;
        else
            break;
    

    or linq-lookup:

    var result = levelsAndValues.Where(level => limit > level.Item1)
                                .Select(level => level.Item2)
                                .LastOrDefault();
    

    Now, if your ranges are noncontiguous - you just have to introduce third value to the tuple: {low,high,value} instead of just {high, value} like I wrote above, and then update the filtering accordingly. This might be a good time to also change the Tuple to a custom type.

    Or, to use the Interval datatype posted here, just like Marting hinted in the comments.

    0 讨论(0)
  • 2021-01-23 03:19

    You can declare class about this:

    public class Range
    {
        public int Min { get; set; }
        public int Max { get; set; }
        public int Limit { get; set; }
        public int Result{get;set;}
    
        public Range(int min, int max, int limit, int result)
        {
            this.Min = min;
            this.Max = max;
            this.Limit = limit;
            this.Result = result;
        }
    
        public bool InRange(int value)
        {
            if (value >= this.Min && value <= this.Max && value <= limit)
                return true;
            return false;
        }
    }
    

    and use this class like:

    List<Range> rangeList = new List<Range>();
            rangeList.Add(new Range(1000000, 5000000, 10000000, 10));
            rangeList.Add(new Range(5000001, 10000000, 10000000, 20));
            int? result = rangeList.Where(t => t.InRange(value)).Select(t => t.Result).FirstOrDefault();
    

    if variable result is not null, it's your final result.

    0 讨论(0)
  • 2021-01-23 03:28

    You could try making some little anonymous (or not-so-anonymous, for re-use) functions:

    Func<int, bool> range1 = i => (1000000 >= i) && (i <= 50000000);
    Func<int, bool> range2 = i => (50000001 >= i) && (i <= 10000000);
    Func<int, bool> limit =  i => i <= 10000000;
    
    var test = 2000000;
    
    if(limit(test) && range1(test))
    {
         result = 10;
    }
    else if(limit(test) && range2(test))
    {
         result = 20;
    }
    
    0 讨论(0)
提交回复
热议问题