您的任务是输入一个整数序列并对其进行块排序。您可以假设输入列表总是可以分组为1、2和3。
块排序(我为这个挑战而编写的术语)是首先将序列分组为一个循环中的1、2和3组,然后对每个组进行排序。
[1, 2, 3, 4, 5, 6, 7, 8, 9] => [[1], [2, 3], [4, 5, 6], [7], [8, 9]]
[5, 9, -2, 8, -6, 4, 5, 9, 1] => [[5], [-2, 9], [-6, 4, 8], [5], [1, 9]]
[8, 3, -1, 6, -8, 7, -1] => [[8], [-1, 3], [-8, 6, 7], [-1]]最短的代码赢了!
请不要有标准的漏洞。
发布于 2021-12-10 17:05:11
发布于 2021-12-10 17:51:38
3LI∍£€{ʒgĀ或者(感谢N6%tò的@ovs ):
.yN6%tò}€{3L # Push list [1,2,3]
I∍ # Extend the [1,2,3] list to the same size as the input-list
£ # Split the (implicit) input-list into those parts,
# which includes trailing empty lists at the end
€{ # Sort each inner list
ʒgĀ # Remove the empty lists:
ʒ # Filter the list of lists:
gĀ # Check if the list-length is not 0
# (after which the result is output implicitly)
.y # Adjacent group the (implicit) input-list by:
N # Push the 0-based group-by index
6% # Modulo-6
t # Take the square-root of that
ò # Round it to the nearest integer
}€{ # After the group-by: sort each inner list
# (after which the result is output implicitly)N6%tò导致序列[0,1,1,2,2,2,0,1,1,2,2,2,...],通过将输入列表拆分为[1,2,3,1,2,3,...]大小的部分,从而导致相邻的组,这正是我们想要的。
如果输入长度保证为字节数,则第一个程序可以是9 n>1:
3LI∍£€{Ô¨见所有测试用例,以及它如何不幸地对[1]失败。(在Ć之后添加I将修复边缘情况,但不幸的是,我们将有另一个10字节的替代方案。)
https://codegolf.stackexchange.com/questions/238329
复制相似问题