質問

みんな! C#の特定の範囲でINT配列の最小値を取得するにはどうすればよいですか?例:int [] array = new int {1,2,3,4,5,6,7,8,76,45};そして、3 RDと8番目の要素の間で最小値を取得したいと思います。たぶんそれはlinqクエリを介して取得することができますか?

役に立ちましたか?

解決

array.Skip(2).Take(5).Min();

他のヒント

私はこれに私のタペンスを追加するかもしれないと思います。ジェイソンは、エンドインデックスではなくスキップしている数を言っているという事実に反対しているため、簡単な拡張法を追加できます。

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);
}

それで:

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

エクステンションメソッド名を味わって調整します。 (命名は難しいです、そして、私はここで素敵なものを思いつく年齢を費やすつもりはありません:)

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

編集

実際、上記のアプローチは非常に非効率的です。これは、7を超えるインデックスを持つアイテムに興味がない場合でも、シーケンス全体を列挙しているためです。 TakeWhile:

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

残念ながら、それはあまり読みやすいわけではありません...より良いものにするための最良の選択肢は、おそらくJonの答えに示されているように、カスタム拡張法を作成することです。

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();

別のオプションを追加するだけです:

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

Afaik、これは、1つの終わり近くに範囲を取る必要がある場合、「理論的に」より速く、アレイは本当に長いです。

それは(再びafaik)からです Skip() それを考慮していません(つまり、O(1)でランダムにアクセスできます)、とにかく列挙します。

array.Skip(3).Take(4).Min();

個人的に、私はこれを好む:

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();
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top