生成循环数组


15

介绍

指针数组是一个数组L,其中的非零整数0 ≤ L[i]+i < len(L)为所有指数保持i(假定从0开始的索引)。我们说索引i 指向索引L[i]+i。如果索引形成一个length周期,则指针数组是一个循环len(L)。这里有些例子:

  • [1,2,-1,3]不是指针数组,因为3不会指向索引。
  • [1,2,-1,-3]是一个指针数组,但不是循环,因为没有索引指向-1
  • [2,2,-2,-2] 是一个指针数组,但不是一个循环,因为索引形成两个周期。
  • [2,2,-1,-3] 是一个循环。

输入值

您的输入是以任何合理格式显示的非零整数的非空列表。它可能未排序和/或包含重复项。

输出量

您的输出将是一个循环,该循环包含输入列表中的所有整数(也可能还有其他整数),并计算重复数。它们不必以与输入中相同的顺序发生,并且输出在任何意义上都不必最小。

对于输入[2,-4,2],可接受的输出为[2,2,-1,1,-4]

规则和计分

您可以编写完整的程序或函数。最低字节数获胜,并且不允许出现标准漏洞。感谢您在答案中包含几个示例输入和输出。

测试用例

这些以格式给出input -> some possible output(s)

[1] -> [1,-1] or [1,1,1,-3]
[2] -> [2,-1,-1] or [1,2,-2,-1]
[-2] -> [1,1,-2] or [3,1,2,-2,-4]
[2,-2] -> [2,-1,1,-2] or [2,-1,2,-2,-1]
[2,2,2] -> [2,-1,2,-2,2,-2,-1] or [2,2,2,2,-3,-5]
[2,-4,2] -> [2,2,-1,1,-4] or [2,5,1,1,1,-4,2,-7,-1]
[3,-1,2,-2,-1,-5] -> [2,3,-1,2,-1,-5] or [3,3,-1,-1,2,2,-1,6,1,1,1,1,-12,-5]
[-2,-2,10,-2,-2,-2] -> [10,-1,1,-2,-2,1,-2,-2,1,-2,-2]
[-15,15,-15] -> [15,-1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,-15,-15]
[1,2,3,4,5] -> [1,2,3,-1,4,-1,5,-1,-1,-9,-1,-1]

Answers:


11

果冻,12个字节

ż~Ṣ€FxA$;L$U

在线尝试!

背景

考虑一对整数n,〜n,其中n≥0表示按位非,即〜n =-(n + 1)

通过将Ñ副本Ñ向左的n + 1个拷贝〜n的,如果我们开始遍历从最右边的指针数组〜n的,我们会遍历所有2N + 1组的元素,并在左侧的最左边的发现自己ñ

例如,如果n = 4

X  4  4  4  4  -5 -5 -5 -5 -5
                            ^
            ^
                         ^
         ^
                      ^
      ^
                   ^
   ^
                ^
^

对于特殊情况n = 0,元素n本身被重复0次,从而保持:

X -1
   ^
^

对于输入中的每个整数k,我们可以通过设置n = k(如果k> 0)n =〜k(如果k <0)来形成包含kn〜n。。这是有效的,因为是对合,即~~ k = k

剩下要做的就是链接生成的元组并添加其合并长度,因此最左边的元素将我们带回到最右边的元素。

例子

[1] -> [3, 1, -2, -2]
[2] -> [5, 2, 2, -3, -3, -3]
[-2] -> [3, 1, -2, -2]
[2, -2] -> [8, 1, -2, -2, 2, 2, -3, -3, -3]
[2, 2, 2] -> [15, 2, 2, -3, -3, -3, 2, 2, -3, -3, -3, 2, 2, -3, -3, -3]
[2, -4, 2] -> [17, 2, 2, -3, -3, -3, 3, 3, 3, -4, -4, -4, -4, 2, 2, -3, -3, -3]
[3, -1, 2, -2, -1, -5] -> [26, 4, 4, 4, 4, -5, -5, -5, -5, -5, -1, 1, -2, -2, 2, 2, -3, -3, -3, -1, 3, 3, 3, -4, -4, -4, -4]
[-2, -2, 10, -2, -2, -2] -> [36, 1, -2, -2, 1, -2, -2, 1, -2, -2, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, 1, -2, -2, 1, -2, -2]
[-15, 15, -15] -> [89, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, -15]
[1, 2, 3, 4, 5] -> [35, 5, 5, 5, 5, 5, -6, -6, -6, -6, -6, -6, 4, 4, 4, 4, -5, -5, -5, -5, -5, 3, 3, 3, -4, -4, -4, -4, 2, 2, -3, -3, -3, 1, -2, -2]

怎么运行的

ż~Ṣ€FxA$;L$U  Main link. Argument: A (list of integers)

 ~            Yield the bitwise not of each k in A.
ż             Zipwith; pair each k in A with ~k.
  Ṣ€          Sort each pair, yielding [~n, n] with n ≥ 0.
    F         Flatten the list of pairs.
       $      Combine the previous two links into a monadic chain:
      A         Yield the absolute values of all integers in the list.
                |n| = n and |~n| = |-(n + 1)| = n + 1
     x          Repeat each integer m a total of |m| times.
          $   Combine the previous two links into a monadic chain:
         L      Yield the length of the generated list.
        ;       Append the length to the list.
           U  Upend; reverse the generated list.

您不需要处理特殊情况n = 0,因为规范中会显示“ 非零整数 ”。
彼得·泰勒

虽然0绝不会在输入发生,我仍然需要对0,-1如果-1一样。
丹尼斯
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.