21
List<Int32> dansConList = new List<Int32>();
dansConList[0] = 1;
dansConList[1] = 2;
dansConList[2] = 3;

List<Int32> dansRandomList = new List<Int32>();
dansRandomList[0] = 1;
dansRandomList[1] = 2;
dansRandomList[2] = 4;

I need a method that, when evaluating the above lists, will return false for dansRandomList and true for dansConList based on the fact dansConList has a consecutive number sequence in it's values, and dansRandomList does not (missing the value 3).

Using LINQ is preferable, if possible.

What I've Tried:

  • For the sake of achieving the end result, I have used a for loop and compare with 'i' (loop counter) to evaluate the values, but as mentioned above I'd like to use LINQ for this.
4

13 に答える 13

63

ワンライナー、連続していない最初の要素までのみ反復します。

bool isConsecutive = !myIntList.Select((i,j) => i-j).Distinct().Skip(1).Any();

更新: これがどのように機能するかのいくつかの例:

Input is { 5, 6, 7, 8 }
Select yields { (5-0=)5, (6-1=)5, (7-2=)5, (8-3=)5 }
Distinct yields { 5, (5 not distinct, 5 not distinct, 5 not distinct) }
Skip yields { (5 skipped, nothing left) }
Any returns false
Input is { 1, 2, 6, 7 }
Select yields { (1-0=)1, (2-1=)1, (6-2=)4, (7-3=)4 } *
Distinct yields { 1, (1 not distinct,) 4, (4 not distinct) } *
Skip yields { (1 skipped,) 4 }
Any returns true

* Select は 2 番目の 4 を生成せず、Distinct はそれをチェックしません。Any は最初の 4 を見つけた後に停止するためです。

于 2012-11-13T11:07:39.720 に答える
10
var min = list.Min();
var max = list.Max();
var all = Enumerable.Range(min, max - min + 1);
return list.SequenceEqual(all);
于 2012-11-13T10:50:44.143 に答える
9
var result = list
    .Zip(list.Skip(1), (l, r) => l + 1 == r)
    .All(t => t);
于 2012-11-13T11:07:17.927 に答える
6

この拡張メソッドを使用できます。

public static bool IsConsecutive(this IEnumerable<int> ints )
{
    //if (!ints.Any())
    //    return true; //Is empty consecutive?
    // I think I prefer exception for empty list but I guess it depends
    int start = ints.First();
    return !ints.Where((x, i) => x != i+start).Any();
}

次のように使用します。

[Test]
public void ConsecutiveTest()
{
    var ints = new List<int> {1, 2, 4};
    bool isConsecutive = ints.IsConsecutive();
}
于 2012-11-13T10:52:43.730 に答える
3

延長方法:

public static bool IsConsecutive(this IEnumerable<int> myList)
{
    return myList.SequenceEqual(Enumerable.Range(myList.First(), myList.Last()));
}

用途:

bool isConsecutive = dansRandomList.IsConsecutive();
于 2012-11-13T10:51:09.377 に答える
0

これがもう1つです。{1,2,3,4} と {4,3,2,1} の両方をサポートします。連番の差が 1 または -1 に等しいかどうかをテストします。

Function IsConsecutive(ints As IEnumerable(Of Integer)) As Boolean
    If ints.Count > 1 Then
        Return Enumerable.Range(0, ints.Count - 1).
            All(Function(r) ints(r) + 1 = ints(r + 1) OrElse ints(r) - 1 = ints(r + 1))
    End If

    Return False
End Function
于 2012-12-08T10:18:14.783 に答える
0

警告: 空の場合は true を返します。

var list = new int[] {-1,0,1,2,3};
var isConsecutive = list.Select((n,index) => n == index+list.ElementAt(0)).All (n => n);
于 2017-02-10T20:26:25.743 に答える
0
// 1 | 2 | 3 | 4 | _
// _ | 1 | 2 | 3 | 4
//   | 1 | 1 | 1 |    => must be 1 (or 2 for even/odd consecutive integers)

var numbers = new List<int>() { 1, 2, 3, 4, 5 };
const step = 1; // change to 2 for even and odd consecutive integers

var isConsecutive = numbers.Skip(1)
   .Zip(numbers.SkipLast(1))
   .Select(n => {
       var diff = n.First - n.Second;
       return (IsValid: diff == step, diff);
   })
   .Where(diff => diff.IsValid)
   .Distinct()
   .Count() == 1;

または、もう少し短く書くこともできますが、読みにくくなります。

var isConsecutive = numbers.Skip(1)
   .Zip(numbers.SkipLast(1), (l, r) => (IsValid: (l-r == step), l-r))
   .Where(diff => diff.IsValid)
   .Distinct()
   .Count() == 1;
于 2020-09-03T23:03:03.170 に答える
-1

一意のリストでのみ機能します。

List<Int32> dansConList = new List<Int32>();
dansConList.Add(7);
dansConList.Add(8);
dansConList.Add(9);

bool b = (dansConList.Min() + dansConList.Max())*((decimal)dansConList.Count())/2.0m == dansConList.Sum();
于 2012-11-13T10:59:20.037 に答える
-2

これはC版のコードですが、論理的に他の言語に書き換えるのは簡単だと思います。

int isConsecutive(int *array, int length) {
     int i = 1;
     for (; i < length; i++) {
          if (array[i] != array[i - 1] + 1)
              return 0; //which means false and it's not a consecutive list
     }

     return 1;
}
于 2012-11-13T10:52:35.947 に答える