英文:
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<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]]
Source from this link https://stackoverflow.com/a/75054991/17971818
Thanks Ivo https://stackoverflow.com/users/1514861/ivo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论