是否可以使用LINQ检查列表中的所有数字是否单调增加?

时间:2013-02-11 15:34:09

标签: c# .net linq

我很感兴趣,如果在LINQ中有一种方法可以检查列表中的所有数字是否单调增加?

示例

List<double> list1 = new List<double>() { 1, 2, 3, 4 };
Debug.Assert(list1.IsIncreasingMonotonically() == true);

List<double> list2 = new List<double>() { 1, 2, 100, -5 };
Debug.Assert(list2.IsIncreasingMonotonically() == false);

我问的原因是我想知道将列表中的元素与其前一个元素进行比较的技术,这是我在使用LINQ时从未理解过的。

C#中的完成示例类

根据下面@Servy的官方回答,这是我现在使用的完整课程。它为您的项目添加扩展方法,以检查列表是单调增加/减少还是严格单调增加/减少。我正在尝试习惯一种函数式编程风格,这是一种很好的学习方法。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyHelper
{
    /// <summary>
    /// Classes to check if a list is increasing or decreasing monotonically. See:
    /// http://stackoverflow.com/questions/14815356/is-it-possible-to-use-linq-to-check-if-all-numbers-in-a-list-are-increasing-mono#14815511
    /// Note the difference between strictly monotonic and monotonic, see:
    /// http://en.wikipedia.org/wiki/Monotonic_function
    /// </summary>
    public static class IsMonotonic
    {
        /// <summary>
        /// Returns true if the elements in the are increasing monotonically.
        /// </summary>
        /// <typeparam name="T">Type of elements in the list.</typeparam>
        /// <param name="list">List we are interested in.</param>
        /// <returns>True if all of the the elements in the list are increasing monotonically.</returns>
        public static bool IsIncreasingMonotonically<T>(this List<T> list) where T : IComparable
        {
            return list.Zip(list.Skip(1), (a, b) => a.CompareTo(b) <= 0).All(b => b);
        }

        /// <summary>
        /// Returns true if the elements in the are increasing strictly monotonically.
        /// </summary>
        /// <typeparam name="T">Type of elements in the list.</typeparam>
        /// <param name="list">List we are interested in.</param>
        /// <returns>True if all of the the elements in the list are increasing monotonically.</returns>
        public static bool IsIncreasingStrictlyMonotonically<T>(this List<T> list) where T : IComparable
        {
            return list.Zip(list.Skip(1), (a, b) => a.CompareTo(b) < 0).All(b => b);
        }

        /// <summary>
        /// Returns true if the elements in the are decreasing monotonically.
        /// </summary>
        /// <typeparam name="T">Type of elements in the list.</typeparam>
        /// <param name="list">List we are interested in.</param>
        /// <returns>True if all of the the elements in the list are decreasing monotonically.</returns>
        public static bool IsDecreasingMonotonically<T>(this List<T> list) where T : IComparable
        {
            return list.Zip(list.Skip(1), (a, b) => a.CompareTo(b) >= 0).All(b => b);
        }

        /// <summary>
        /// Returns true if the elements in the are decreasing strictly monotonically.
        /// </summary>
        /// <typeparam name="T">Type of elements in the list.</typeparam>
        /// <param name="list">List we are interested in.</param>
        /// <returns>True if all of the the elements in the list are decreasing strictly monotonically.</returns>
        public static bool IsDecreasingStrictlyMonotonically<T>(this List<T> list) where T : IComparable
        {
            return list.Zip(list.Skip(1), (a, b) => a.CompareTo(b) > 0).All(b => b);
        }

        /// <summary>
        /// Returns true if the elements in the are increasing monotonically.
        /// </summary>
        /// <typeparam name="T">Type of elements in the list.</typeparam>
        /// <param name="list">List we are interested in.</param>
        /// <returns>True if all of the the elements in the list are increasing monotonically.</returns>
        public static bool IsIncreasingMonotonicallyBy<T>(this List<T> list, Func<T> x) where T : IComparable
        {
            return list.Zip(list.Skip(1), (a, b) => a.CompareTo(b) <= 0).All(b => b);
        }

        public static void UnitTest()
        {
            {
                List<double> list = new List<double>() { 1, 2, 3, 4 };
                Debug.Assert(list.IsIncreasingMonotonically<double>() == true);
                Debug.Assert(list.IsIncreasingStrictlyMonotonically<double>() == true);
                Debug.Assert(list.IsDecreasingMonotonically<double>() == false);
                Debug.Assert(list.IsDecreasingStrictlyMonotonically<double>() == false);
            }

            {
                List<double> list = new List<double>() { 1, 2, 100, -5 };
                Debug.Assert(list.IsIncreasingMonotonically() == false);
                Debug.Assert(list.IsIncreasingStrictlyMonotonically() == false);
                Debug.Assert(list.IsDecreasingMonotonically() == false);
                Debug.Assert(list.IsDecreasingStrictlyMonotonically() == false);
            }

            {
                List<double> list = new List<double>() {1, 1, 2, 2, 3, 3, 4, 4};
                Debug.Assert(list.IsIncreasingMonotonically() == true);
                Debug.Assert(list.IsIncreasingStrictlyMonotonically<double>() == false);
                Debug.Assert(list.IsDecreasingMonotonically() == false);
                Debug.Assert(list.IsDecreasingStrictlyMonotonically() == false);
            }

            {
                List<double> list = new List<double>() { 4, 3, 2, 1 };
                Debug.Assert(list.IsIncreasingMonotonically() == false);
                Debug.Assert(list.IsIncreasingStrictlyMonotonically<double>() == false);
                Debug.Assert(list.IsDecreasingMonotonically() == true);
                Debug.Assert(list.IsDecreasingStrictlyMonotonically() == true);
            }

            {
                List<double> list = new List<double>() { 4, 4, 3, 3, 2, 2, 1, 1 };
                Debug.Assert(list.IsIncreasingMonotonically() == false);
                Debug.Assert(list.IsIncreasingStrictlyMonotonically<double>() == false);
                Debug.Assert(list.IsDecreasingMonotonically() == true);
                Debug.Assert(list.IsDecreasingStrictlyMonotonically() == false);
            }
        }
    }
}

8 个答案:

答案 0 :(得分:12)

public static bool IsIncreasingMontonically<T>(List<T> list) 
    where T : IComparable
{
    return list.Zip(list.Skip(1), (a, b) => a.CompareTo(b) <= 0)
        .All(b => b);
}

请注意,这会迭代序列两次。对于List,这根本不是问题,对于IEnumerableIQueryable,可能不好,所以在将List<T>更改为{{1}之前要小心}}

答案 1 :(得分:5)

您是否不使用OrderBy()订购列表并将其与原始列表进行比较?如果它们是相同的那么它会给你的答案伪说话:

var increasing = orignalList.OrderBy(m=>m.value1).ToList();
var decreasing = orignalList.OrderByDescending(m=>m.value1).ToList();

var mono = (originalList == increasing || originalList == decreasing)

答案 2 :(得分:4)

使用循环!它简短,快速且易读。除了Servy的答案之外,这个线程中的大多数解决方案都不必要地慢(排序需要'n log n'时间)。

// Test whether a sequence is strictly increasing.
public bool IsIncreasing(IEnumerable<double> list)
{
    bool initial = true;
    double last = Double.MinValue;
    foreach(var x in list)
    {
        if (!initial && x <= last)
            return false;

        initial = false;
        last = x;
    }

    return true;
}

实施例

  1. IsIncreasing(new List<double>{1,2,3})返回True
  2. IsIncreasing(new List<double>{1,3,2})返回False

答案 3 :(得分:4)

使用Enumerable.Aggregate方法:

list1.Aggregate((a, i) => a > i ? double.MaxValue : i) != double.MaxValue;

答案 4 :(得分:3)

这是一个工作的单行

var isIncreasing = list.OrderBy(x => x).SequenceEqual(list);

或者,如果你想要表演,这里只有一行,只会遍历列表一次,并且一旦到达一个不按顺序的元素就退出:

var isIncreasing = !list.SkipWhile((x, i) => i == 0 || list[i - 1] <= x).Any();

答案 5 :(得分:1)

如果要检查列表是否总是从索引增加到索引:

IEnumerable<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 10 };
bool allIncreasing = !list
    .Where((i, index) => index > 0 && list.ElementAt(index - 1) >= i)
    .Any();

Demo

但在我看来,在这种情况下,一个简单的循环会更具可读性。

答案 6 :(得分:1)

考虑如下所示的实现,它只枚举给定的IEnumerable一次。枚举可能会产生副作用,如果可能,调用者通常会期望单次传递。

public static bool IsIncreasingMonotonically<T>(
    this IEnumerable<T> _this)
    where T : IComparable<T>
{
    using (var e = _this.GetEnumerator())
    {
        if (!e.MoveNext())
            return true;
        T prev = e.Current;
        while (e.MoveNext())
        {
            if (prev.CompareTo(e.Current) > 0)
                return false;
            prev = e.Current;
        }
        return true;
    }
}

答案 7 :(得分:1)

public static class EnumerableExtensions
{
    private static bool CompareAdjacentElements<TSource>(this IEnumerable<TSource> source,
        Func<TSource, TSource, bool> comparison)
    {
        using (var iterator = source.GetEnumerator())
        {
            if (!iterator.MoveNext())
                throw new ArgumentException("The input sequence is empty", "source");
            var previous = iterator.Current;
            while (iterator.MoveNext())
            {
                var next = iterator.Current;
                if (comparison(previous, next)) return false;
                previous = next;
            }
            return true;
        }
    }

    public static bool IsSorted<TSource>(this IEnumerable<TSource> source)
        where TSource : IComparable<TSource>
    {
        return CompareAdjacentElements(source, (previous, next) => previous.CompareTo(next) > 0);
    }

    public static bool IsSorted<TSource>(this IEnumerable<TSource> source, Comparison<TSource> comparison)
    {
        return CompareAdjacentElements(source, (previous, next) => comparison(previous, next) > 0);
    }

    public static bool IsStrictSorted<TSource>(this IEnumerable<TSource> source)
        where TSource : IComparable<TSource>
    {
        return CompareAdjacentElements(source, (previous, next) => previous.CompareTo(next) >= 0);
    }

    public static bool IsStrictSorted<TSource>(this IEnumerable<TSource> source, Comparison<TSource> comparison)
    {
        return CompareAdjacentElements(source, (previous, next) => comparison(previous, next) >= 0);
    }
}