首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >块排序序列

块排序序列
EN

Code Golf用户
提问于 2021-12-10 16:48:28
回答 13查看 1.5K关注 0票数 17

您的任务是输入一个整数序列并对其进行块排序。您可以假设输入列表总是可以分组为1、2和3。

定义

块排序(我为这个挑战而编写的术语)是首先将序列分组为一个循环中的1、2和3组,然后对每个组进行排序。

测试用例

代码语言:javascript
复制
[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]]

评分

最短的代码赢了!

规则

请不要有标准的漏洞。

EN

回答 13

Code Golf用户

发布于 2021-12-10 17:05:11

红宝石,49字节

代码语言:javascript
复制
->l{x=5;l.chunk{441[(x+=1)%12]}.map{|a,b|b.sort}}

在网上试试!

字面意思是:大块,然后排序。

幻数441是一个位掩码,具有1,2,3位二进制数字,或者seto 1或0:

代码语言:javascript
复制
000 11 0 111 00 1

因为只有两个二进制数,所以我不能把它变成3个块,所以我把它变成6,所以我用mod 12来旋转。

票数 12
EN

Code Golf用户

发布于 2021-12-10 17:51:38

05AB1E,10 字节数

代码语言:javascript
复制
3LI∍£€{ʒgĀ

在网上试试验证所有测试用例.

或者(感谢N6%tò的@ovs ):

代码语言:javascript
复制
.yN6%tò}€{

在网上试试验证所有测试用例.

Explanation:

代码语言:javascript
复制
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

代码语言:javascript
复制
3LI∍£€{Ô¨

见所有测试用例,以及它如何不幸地对[1]失败。(在Ć之后添加I将修复边缘情况,但不幸的是,我们将有另一个10字节的替代方案。)

票数 8
EN

Code Golf用户

发布于 2021-12-10 20:00:50

外壳,6字节

代码语言:javascript
复制
mOC¢ḣ3

在网上试试!

怎么做?

代码语言:javascript
复制
    ḣ3      # 1..3
   ¢        # repeated infinitely;
  C         # now split the input into sublists
            # of these lengths (discarding any 
            # extra, unused lengths);
m           # finally, for each sublist:
 O          # sort it.
票数 8
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/238329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档