Check if List<Int32> values are consecutive

One-liner, only iterates until the first non-consecutive element:

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

Update: a couple examples of how this works:

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

* The Select will not yield the second 4 and the Distinct will not check it, as the Any will stop after finding the first 4.

Check if List<Int32> values are consecutive的更多相关文章

  1. error C4996: Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct

    使用VS13 跟  google protocbuf时出现了这个问题:真蛋疼,用别人的东西你就说不安全,用你自己的东西时你怎么不说不安全来着! 解决方案 在protoc   生成的头文件中加上 #pr ...

  2. check_mk 之 Check Parameters

    配置检测参数有几下方法 1. Creating manual checks instead of inventorized checks (using the variable checks). 2. ...

  3. android 官方文档 JNI TIPS

    文章地址  http://developer.android.com/training/articles/perf-jni.html JNI Tips JNI is the Java Native I ...

  4. NDK(5) Android JNI官方综合教程[JavaVM and JNIEnv,Threads ,jclass, jmethodID, and jfieldID,UTF-8 and UTF-16 Strings,Exceptions,Native Libraries等等]

    JNI Tips In this document JavaVM and JNIEnv Threads jclass, jmethodID, and jfieldID Local and Global ...

  5. [pytorch修改]npyio.py 实现在标签中使用两种delimiter分割文件的行

    from __future__ import division, absolute_import, print_function import io import sys import os impo ...

  6. Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册

    Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册 ctypes 是一个Python 标准库中的一个库.为了实现调用 DLL,或者共享库等C数据类型而设计.它可以把这 ...

  7. Android 性能优化(18)JNI优化:JNI Tips 提升性能技巧

    JNI Tips 1.In this document JavaVM and JNIEnv Threads jclass, jmethodID, and jfieldID Local and Glob ...

  8. An iOS zero-click radio proximity exploit odyssey

    NOTE: This specific issue was fixed before the launch of Privacy-Preserving Contact Tracing in iOS 1 ...

  9. 2016年最新mac下vscode配置golang开发环境支持debug

    网上目前还找不到完整的mac下golang环境配置支持,本人配置成功,现在整理分享出来. mac最好装下xcode,好像有依赖关系安装Homebrew打开终端窗口, 粘贴脚本执行/usr/bin/ru ...

随机推荐

  1. php数组,常量,遍历等

    php常量,常量是不能被改变的,由英文字母,下划线,和数字组成,但是数字不能作为首字母出现. bool define ( string $name , mixed $value [, bool $ca ...

  2. HAProxy-1.8.x版本源码编译

    源码编译HAProxy:  官网下载HAProxy包,并解压包,切换到haproxy包目录下 [root@centos17haproxy-1.8.20]#tar xvf haproxy-1.8.20. ...

  3. c实现二叉树

    C实现二叉树 简单说明 实现了先序遍历.中序遍历.后序遍历.搜索 本来想着和平衡二叉树一起放上来的,但是花了一个下午也只是把平衡二叉树原理弄懂和左右旋代码实现,最难的平衡左/右旋还没弄,就不显摆了,就 ...

  4. 云计算/云存储---Ceph和Openstack的cinder模块对接方法

    1.创建存储池 在ceph节点中执行如下语句. #ceph osd pool create volumes 2.配置 OPENSTACK 的 CEPH 客户端 在ceph节点两次执行如下语句,两次{y ...

  5. Vue中mapMutations映射方法的问题

    今天又被自己给蠢到,找了半天没发现问题.大家看下代码. mutation-types.js 里我新增了一个类型.INIT_CURRENTORDER export const GET_USERINFO ...

  6. 团队协作editconfig与eslint

    editconfig root = true [*] charset = utf-8 indent_style = space indent_size = 2 end_of_line = lf ins ...

  7. 2019-2020-1 20199312 《Linux内核原理与分析》 第八周作业

    ELF(Executable and Linkable Format)可执行的和可链接的格式.(对应Windows为PE) 其包含了以下三类: 可重定位文件:保存着代码和适当的数据,用来和其它的目标文 ...

  8. Python之sort()函数详解

    #从小到大排列 print(sorted([36, 5, -12, 9, -21])) #将待排序的值放入到key中的函数中,在进行比较排序 print(sorted([36, 5, -12, 9, ...

  9. vue引入js文件时报This dependency was not found:错误

    vue引入js文件时报This dependency was not found:错误 使用了很多方法,原来是这么小的问题,特此记录 解决办法 添加 ./

  10. 001——CC2530——终端或者路由器传输到不同多协调器上

    (一)参考文献:协调器太多,cc2530的终端或则路由器选择性加入协调器的方法 (二)频道和ID一样 进入协调器和终端和路由器的两个程序,找到Tools中的f8wConfig.cfg文件.修改默认PA ...