首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >双胞胎补语

双胞胎补语
EN

Code Golf用户
提问于 2021-12-22 20:03:42
回答 8查看 1.1K关注 0票数 22

考虑一个正整数的长度-n数组.

  • 带有索引i的元素的补码是索引n - i - 1的元素。
  • 元素a的孪生体是除去所有不等于a的元素之后的补体。

例如,在数组[1, 2, 3, 4, 2, 1, 3, 2]中,第一个2的补充是最后一个3,第一个2的双胞胎是最后一个2。(一般来说,第一个a的双胞胎是最后一个a,第二个a的双胞胎是第二个到最后一个a,等等)

给定一个正整数的非空数组,将每个元素替换为其孪生元素的补码.

代码语言:javascript
复制
[1, 2, 3, 4, 2, 1, 3, 2] -> [3, 1, 2, 2, 4, 2, 1, 3]
[1, 2, 1, 3] -> [2, 1, 3, 1]
[10] -> [10]
[8, 3, 1, 8, 1, 8, 3, 10, 3, 8, 10, 8] -> [8, 8, 10, 1, 8, 3, 8, 3, 10, 3, 1, 8]
EN

回答 8

Code Golf用户

发布于 2021-12-22 23:47:38

果冻,8字节

代码语言:javascript
复制
ĠUFỤịỤịU

在网上试试!

约拿港的J回答:

代码语言:javascript
复制
Ġ          Group indices
 U         Reverse each group
  F        Flatten
   Ụ       Grade up
    ịỤ     Index into graded-up original list
      ịU   Index into reversed list

           e.g. input: “XYXZYY” (needn't be integers!)
        Ġ: [[1,3],[2,5,6],[4]]
        U: [[3,1],[6,5,2],[4]]
        F: [ 3,1,  6,5,2,  4 ]
        Ụ: [2,5,1,6,4,3]

           This is a "rank" of the input, breaking ties backwards:
            X Y X Z Y Y
            2   1          rank Xs backwards
              5     4 3    rank Ys backwards
                  6        rank Zs backwards
         → [2,5,1,6,4,3].

           Meanwhile, Ụ is [1,3,2,5,6,4] (equiv. to ĠF),
           indices that sort the input:

           [X,Y,X,Z,Y,Y]
           [1,  3,         indices of Xs
              2,    5,6,   indices of Ys
                  4]       indices of Zs

           Now ịỤ permutes one list by the other,
           mapping each element to its twin's index:
       ịỤ: [3,6,1,4,5,2]

            X Y X Z Y Y
            3   1          Twin indices of Xs
              6     5 2    Twin indices of Ys
                  4        Twin indices of Zs
           
           And we only have to index into the backwards input:
       ịU: “ZXYXYY”
票数 7
EN

Code Golf用户

发布于 2021-12-24 02:57:46

小矮人,30字节(如果编码为17.7字节)

代码语言:javascript
复制
mhoithzsmqmscbnqgihcmmzsbbnlis

Minipyth是我设计的一种新的极简语言。它只使用小写字母,不包含变量、第二个函数等。

因为Minipyth只使用小写字母,所以可以重新编码,使其更短。因此,我还列出了它的信息论长度,它比\log(26)/\log(256) \approxeq 59\%.短一倍。

Minipyth还没有联机执行器,但是可以通过上面链接的存储库脱机使用它。

它按函数应用程序顺序从右到左执行。

我会把它分解成几个部分来说明发生了什么。

首先,我们准备一个由表单补充、指数、价值的元素组成的列表,其中值是输入的一个元素,索引是它在输入中的位置,而补语是它的补充。

代码语言:javascript
复制
cmmzsbbnlis
     b         Bifurcate: make a list by applying two functions to the input
      b        Function 1: Bifurcate on
       n       negate, reversing the input,
        l      length, the length of the input
         is    inverse sum, wrap the input in a list.
    s          sum, concatenate into a single list of 3 elements
 m             map on
  mz           the default map, identify on lists, casts int x to [0, ..., x-1]
c              chop, transpose the three lists to form lists of 3 elements.

接下来,我们根据它们的值对这些元素进行分组。这是简单的gih,按逆头分组(最后一个元素)。

现在,我们需要组成双胞胎。

代码语言:javascript
复制
smqmscbnq
 m           Map over the groups.
      bn     Bifurcate on n, reversing the group.
     c       Chop, forming pairs.
   ms        Sum the pairs, concatenating into one list
s            Sum, concatenating the lists back into one

Z和Q使一切都能正确地解析。

现在,我们的元素有六个元素列表:双胞胎的补充,双胞胎的指数,孪生的价值,补充,指数,价值。

我们只需要使用索引将事物重新按原来的顺序排列,然后提取双胞胎的补码。

代码语言:javascript
复制
 mhoithz
   o         Order by
    i        Inverse of
     t       Tail
      h      Head
             The inverse of tail removes the final element,
             then the inverse of head returns the second-to-last
 mh          Map to head

在撰写本文时,有一个注意事项(提交4436b81a3)逆逆其参数的应用顺序。这是为了模拟反函数,但我决定我不喜欢它,我会撤销它。在将来,等价的程序将交换h和t:

代码语言:javascript
复制
mhoithzsmqmscbnqgihcmmzsbbnlis
票数 7
EN

Code Golf用户

发布于 2021-12-22 20:21:17

果冻,9字节

代码语言:javascript
复制
ĠUF,ỤyJịU

在网上试试!

代码语言:javascript
复制
ĠUF,ỤyJịU  Main Link
Ġ          Group indices by equal elements
 U         Reverse each block
  F        Flatten
   ,Ụ      Pair with the grade of the original list
     yJ    Apply this as a translation to the indices of the list
       ịU  Index that into the reverse of the original list

-1字节,感谢Lynn的分级技巧。对一个列表进行分级,根据其相应的值对索引进行排序。

票数 5
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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