将整数列表中的相邻值逻辑划分为子列表。

huangapple go评论73阅读模式
英文:

what is the logic of neighbor values in to sub lists from integer list

问题

List integerList = [1, 2, 4, 11, 14, 15, 16, 16, 19, 30, 31, 50, 51, 100, 101, 105];

期望的输出是:

List sub = [[1, 2, 4], [11, 14, 15, 16, 16, 19], [30, 31], [50, 51], [100, 101, 105]];

1、2、4 差异至少为 7 与分隔整数列表时的值。

(注意:我只翻译了您提供的内容,不包括代码部分。)

英文:

input =>

 List integerList=[1,2,4,11,14,15,16,16,19,30,31,50,51,100,101,105];

expecting output =>

 List sub = [[1,2,4],[11,14,15,16,16,19],[30,31],[50,51],[100,101,105]];

1,2,4 7 difference to 11,14,15,16,16,19 11 difference to 30,31, 19 difference to 50,51, 49 difference to 100,101,105

 basic crietirea , atleast 7 difference with the values at the time of separation of
 integerlist.

答案1

得分: 1

integerList = [1, 2, 4, 11, 14, 15, 16, 16, 19, 30, 31, 50, 51, 100, 101, 105]; //输入
//输入应该按顺序排列否则您将无法获得预期的输出

var subList = integerList.splitBetween((v1, v2) => (v2 - v1).abs() > 6);
print(subList); //([1, 2, 4], [11, 14, 15, 16, 16, 19], [30, 31], [50, 51], [100, 101, 105])

//您应该从您的标准中减去1这里的7变成了6或者将其更改为(v2 - v1).abs() >= 7

谢谢https://stackoverflow.com/users/2252830/pskink 他是作者
英文:
List integerList=[1,2,4,11,14,15,16,16,19,30,31,50,51,100,101,105]; //input
//input should orderd, just sort your input otherwise you will not get expected output

var subList=integerList.splitBetween((v1, v2) => (v2 - v1).abs() > 6);
print(subList); //([1, 2, 4], [11, 14, 15, 16, 16, 19], [30, 31], [50, 51], [100, 101, 105])

//you should minus 1 from your criteria, here 7 goes to 6. or change it to:(v2 - v1).abs() >= 7

thanks, https://stackoverflow.com/users/2252830/pskink he is the author

答案2

得分: 0

    List<int> integerList = [1, 2, 3, 4, 8, 30, 31, 50, 51, 100]; // 输入应该已排序
    final result = integerList.fold<List<List<int>>>(
        [[]],
        (previousValue, element) => previousValue.last.isEmpty ||
                (previousValue.last.last - element).abs() < 10
            ? [
                ...previousValue.take(previousValue.length - 1),
                [...previousValue.last, element]
              ]
            : [
                ...previousValue,
                [element]
              ]);

    print(result); // [[1, 2, 3, 4, 8], [30, 31], [50, 51], [100]]

来源链接:https://stackoverflow.com/a/75054991/17971818
感谢 Ivo https://stackoverflow.com/users/1514861/ivo

英文:
List integerList = [1,2,3,4,8,30,31,50,51,100];. //input should be sorted
  final result = integerList.fold&lt;List&lt;List&lt;int&gt;&gt;&gt;(
      [[]],
      (previousValue, element) =&gt; previousValue.last.isEmpty ||
              (previousValue.last.last - element).abs() &lt; 10
          ? [
              ...previousValue.take(previousValue.length - 1),
              [...previousValue.last, element]
            ]
          : [
              ...previousValue,
              [element]
            ]);

  print(result); // [[1, 2, 3, 4, 8], [30, 31], [50, 51], [100]]

Source from this link https://stackoverflow.com/a/75054991/17971818
Thanks Ivo https://stackoverflow.com/users/1514861/ivo

huangapple
  • 本文由 发表于 2023年1月9日 19:09:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定