是否有可能使用LINQ来检查,如果列表中的所有数字都在增加单调? [英] Is it possible to use LINQ to check if all numbers in a list are increasing monotonically?
问题描述
我很感兴趣,如果有一种方法,在LINQ,检查列表中的所有数字都在增加单调?
示例
名单,其中,双> List1中=新的名单,其中,双>(){1,2,3,4};
Debug.Assert的(list1.IsIncreasingMonotonically()==真);
名单<双> list2中=新的名单,其中,双>(){1,2,100,-5};
Debug.Assert的(list2.IsIncreasingMonotonically()==假);
我想问的原因是我想知道的技术元素在针对其previous元素,这是我一直使用LINQ时,永远无法理解的列表进行比较。
成品类实例在C#中
根据官方的回答从 @Servy
下面,这里是我现在使用的完整的类。它增加了扩展方法到项目中来检查列表增加/减少是单调,或严格单调。我试图去适应一个函数式编程风格,这是一个学习的好方法。
使用系统;
使用System.Collections.Generic;
使用System.Diagnostics程序;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
命名空间MyHelper
{
///<总结>
///类来检查列表增加或减少单调。看到:
/// http://stackoverflow.com/questions/14815356/is-it-possible-to-use-linq-to-check-if-all-numbers-in-a-list-are-increasing-mono#14815511
///注意严格单调和单调的区别,请参阅:
/// http://en.wikipedia.org/wiki/Monotonic_function
///< /总结>
公共静态类IsMonotonic
{
///<总结>
///如果在元素被越来越单调返回true。
///< /总结>
///< typeparam名=T>类型列表中的元素< / typeparam>
///< PARAM NAME =名单>列表中,我们有兴趣与< /参数>
///<返回>真,如果列表中所有的元素都在增加单调< /回报>
公共静态布尔IsIncreasingMonotonically< T>(名单< T>表),其中T:IComparable的
{
返回list.Zip(list.Skip(1),(A,B)=> a.CompareTo(二)及所述; = 0)。所有(B =>二);
}
///<总结>
///如果在元素被越来越严格单调返回true。
///< /总结>
///< typeparam名=T>类型列表中的元素< / typeparam>
///< PARAM NAME =名单>列表中,我们有兴趣与< /参数>
///<返回>真,如果列表中所有的元素都在增加单调< /回报>
公共静态布尔IsIncreasingStrictlyMonotonically< T>(名单< T>表),其中T:IComparable的
{
返回list.Zip(list.Skip(1),(A,B)=> a.CompareTo(二)及小于0)。所有(B =>二);
}
///<总结>
///如果该元素是单调递减返回true。
///< /总结>
///< typeparam名=T>类型列表中的元素< / typeparam>
///< PARAM NAME =名单>列表中,我们有兴趣与< /参数>
///<返回>真,如果列表中所有的元素都减少单调< /回报>
公共静态布尔IsDecreasingMonotonically< T>(名单< T>表),其中T:IComparable的
{
返回list.Zip(list.Skip(1),(A,B)=> a.CompareTo(二)及GT; = 0)。所有(B =>二);
}
///<总结>
///如果在元素被减小严格单调返回true。
///< /总结>
///< typeparam名=T>类型列表中的元素< / typeparam>
///< PARAM NAME =名单>列表中,我们有兴趣与< /参数>
///<返回>真,如果列表中所有的元素都下降严格单调< /回报>
公共静态布尔IsDecreasingStrictlyMonotonically< T>(名单< T>表),其中T:IComparable的
{
返回list.Zip(list.Skip(1),(A,B)=> a.CompareTo(二)及0)。所有(B =>二);
}
///<总结>
///如果在元素被越来越单调返回true。
///< /总结>
///< typeparam名=T>类型列表中的元素< / typeparam>
///< PARAM NAME =名单>列表中,我们有兴趣与< /参数>
///<返回>真,如果列表中所有的元素都在增加单调< /回报>
公共静态布尔IsIncreasingMonotonicallyBy< T>(名单< T>清单,Func键< T> X),其中T:IComparable的
{
返回list.Zip(list.Skip(1),(A,B)=> a.CompareTo(二)及所述; = 0)。所有(B =>二);
}
公共静态无效的UnitTest()
{
{
名单<双>名单=新的名单,其中,双>(){1,2,3,4};
Debug.Assert的(list.IsIncreasingMonotonically<双>()==真);
Debug.Assert的(list.IsIncreasingStrictlyMonotonically<双>()==真);
Debug.Assert的(list.IsDecreasingMonotonically<双>()==假);
Debug.Assert的(list.IsDecreasingStrictlyMonotonically<双>()==假);
}
{
名单<双>名单=新的名单,其中,双>(){1,2,100,-5};
Debug.Assert的(list.IsIncreasingMonotonically()==假);
Debug.Assert的(list.IsIncreasingStrictlyMonotonically()==假);
Debug.Assert的(list.IsDecreasingMonotonically()==假);
Debug.Assert的(list.IsDecreasingStrictlyMonotonically()==假);
}
{
名单<双>名单=新的名单,其中,双>(){1,1,2,2,3,3,4,4};
Debug.Assert的(list.IsIncreasingMonotonically()==真);
Debug.Assert的(list.IsIncreasingStrictlyMonotonically<双>()==假);
Debug.Assert的(list.IsDecreasingMonotonically()==假);
Debug.Assert的(list.IsDecreasingStrictlyMonotonically()==假);
}
{
名单<双>名单=新的名单,其中,双>(){4,3,2,1};
Debug.Assert的(list.IsIncreasingMonotonically()==假);
Debug.Assert的(list.IsIncreasingStrictlyMonotonically<双>()==假);
Debug.Assert的(list.IsDecreasingMonotonically()==真);
Debug.Assert的(list.IsDecreasingStrictlyMonotonically()==真);
}
{
名单<双>名单=新的名单,其中,双>(){4,4,3,3,2,2,1,1};
Debug.Assert的(list.IsIncreasingMonotonically()==假);
Debug.Assert的(list.IsIncreasingStrictlyMonotonically<双>()==假);
Debug.Assert的(list.IsDecreasingMonotonically()==真);
Debug.Assert的(list.IsDecreasingStrictlyMonotonically()==假);
}
}
}
}
公共静态布尔IsIncreasingMontonically< T>(名单< T>名单)
其中T:IComparable的
{
返回list.Zip(list.Skip(1),(A,B)=> a.CompareTo(二)及所述; = 0)
。所有(B => B);
}
请注意,此迭代序列的两倍。对于列表
,这不是在所有问题,对于的IEnumerable
或的IQueryable
,这可能是坏的,所以一定要小心,然后只需更改名单,其中,T>
到的IEnumerable< T>
。
I'm interested if there is a way, in LINQ, to check if all numbers in a list are increasing monotonically?
Example
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);
The reason I ask is that I would like to know the technique to compare an element in a list against its previous element, which is something I've never understood when using LINQ.
Finished Example Class in C#
As per official answer from @Servy
below, here is the complete class I am now using. It adds extension methods to your project to check if a list is increasing/decreasing either monotonically, or strictly monotonically. I'm trying to get used to a functional programming style, and this is a good way to learn.
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);
}
}
}
}
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);
}
Note that this iterates the sequence twice. For a List
, that's not a problem at all, for an IEnumerable
or IQueryable
, that could be bad, so be careful before you just change List<T>
to IEnumerable<T>
.
这篇关于是否有可能使用LINQ来检查,如果列表中的所有数字都在增加单调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!