果冻,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)来形成包含k的n对〜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
,因为规范中会显示“ 非零整数 ”。