The minimum value of a C # array in a certain range

All! How can I get the minimum value of an int array in a specific range in C #? For example: int [] array = new int {1,2,3,4,5,6,7,8,76,45}; And I want to get the minimum value between the 3rd and 8th elements. Maybe you can get through LINQ queries?

+3
source share
7 answers
array.Skip(2).Take(5).Min();
+12
source

I believe that I can add my dumbness to this. Since Jason objects to what we say, how much we skip, rather than the end index, we can add a simple extension method:

public static IEnumerable<T> WithIndexBetween<T>(this IEnumerable<T> source,
    int startInclusive, int endExclusive)
{
    // The two values can be the same, yielding no results... but they must
    // indicate a reasonable range
    if (endExclusive < startInclusive)
    {
        throw new ArgumentOutOfRangeException("endExclusive");
    }
    return source.Skip(startInclusive).Take(endExclusive - startInclusive);
}

Then:

int min = array.WithIndexBetween(2, 7).Min();

. ( , , :)

+6
int min = array.Where((value, index) => index >= 2 && index <= 7).Min(); 

, , 7. TakeWhile:

int min = array.TakeWhile((value, index) => index <= 7).Skip(2).Min();

, ... - , , , .

+3
int[] arr = {0,1,2,3,4,5,6,7,8};
int start = 3;
int end = 8;
int min = arr.Skip(start - 1).Take(end - start).Min();
+2

:

int start = 3;
int end = 8;
var min = Enumerable.Range(start - 1,end - start).Select(idx => array[idx]).Min();

AFAIK, "" , , .

, ( AFAIK) Skip() , (.. O (1)) .

+2
source
array.Skip(3).Take(4).Min();
0
source

Personally, I would prefer this:

public static class ArrayExtensions {
    public static bool ArrayAndIndexesAreValid(
        T[] array,
        int startInclusive,
        int endExclusive
    ) {
    return array != null &&
           array.Length > 0 &&
           startInclusive >= 0 && startInclusive < array.Length &&
           endExclusive >= 1 && endExclusive <= array.Length &&
           startInclusive < endExclusive;
    }
    public static IEnumerable<T> Slice<T>(
        this T[] array,
        int startInclusive,
        int endExclusive
    ) {
        Contract.Requires<ArgumentException>(ArrayAndIndexesAreValid(
            array,
            startInclusive,
            endExclusive)
        );
        for (int index = startInclusive; index < endExclusive; index++) {
            yield return array[index];
        }
    }
    public static T MinimumInIndexRange<T>(
        this T[] array,
        int startInclusive,
        int endExclusive
    ) where T : IComparable {
        Contract.Requires<ArgumentException>(ArrayAndIndexesAreValid(
            array,
            startInclusive,
            endExclusive)
        );
        return array.Slice(startInclusive, endExclusive).Min();
    }

    public static T MaximumInIndexRange<T>(
        this T[] array,
        int startInclusive,
        int endExclusive
    ) where T : IComparable {
        Contract.Requires<ArgumentException>(ArrayAndIndexesAreValid(
            array,
            startInclusive,
            endExclusive)
        );
        return array.Slice(startInclusive, endExclusive).Max();
    }
}
0
source

Source: https://habr.com/ru/post/1779651/


All Articles