向阵列施加波


24

今天的任务是对数字数组施加波动。波形如下所示:[1, 0, -1, 0, 1, 0, -1, 0, 1...]将其应用于给定数组意味着将第一个元素,第二个元素等加在一起。

更确切地说:

您的程序或函数将接收一个整数数组。它必须打印或返回大小相等的数组,并1添加到原始数组的1st,5th,9th等元素,-1添加到原始数组的3rd,7th,11th等元素,其余元素应该保持不变。

确保输入数组至少包含一个元素。

测试用例:

Input                               | Output
[0]                                 | [1]
[-1]                                | [0]
[-4, 3, 0, 1, 7, 9, 8, -2, 11, -88] | [-3, 3, -1, 1, 8, 9, 7, -2, 12, -88]
[0, 0, 0, 0, 0]                     | [1 ,0 ,-1 ,0 ,1]
[1, 1]                              | [2, 1]

这是,最短的代码获胜!


出乎意料的是,许多解决方案都在使用虚数魔术...
Pavel

2
虚数为什么有用是很有意义的,这是一个波动问题,虚数具有极好的极性历史记录。虚数可能是计算正弦和余弦的非常简单的方法,特别是对于这些整数四分之一旋转类型。数学很酷……
Wheat Wizard

3
@WheatWizard鉴于大多数语言不支持虚数,这是一个很大的比例。
帕维尔

Answers:


8

果冻,5个字节

Jı*Ċ+

在线尝试!

怎么运行的

Jı*Ċ+  Main link. Argument: A (array)

J      Indices; yield [1, ..., len(A)].
 ı*    Elevate the imaginary unit to the power 1, ..., len(A), yielding
       [0+1i, -1+0i, 0-1i, 1+0i, ...].
   Ċ   Take the imaginary part of each result.
    +  Add the results to the corresponding elements of A.


1
有什么解释吗?
Pureferret

1
@Pureferret的虚数的连续功率的虚部被添加到每个元件
心教堂

@Cœur是那个1, 2, 3 ...还是1, 0, -1, 0 ...
Pureferret

1
@Pureferret相同的解释,在回答MATLMath.JS数学[R或...
心教堂

14

LOGO,18字节

[map[?+sin 90*#]?]

没有“在线试用!” 链接,因为所有在线LOGO解释器均不支持模板列表。

那是一个模板列表(在其他语言中相当于lambda函数)。

用法:

pr invoke [map[?+sin 90*#]?] [-4 3 0 1 7 9 8 -2 11 -88]

invoke调用函数,pr打印结果)

版画[-3 3 -1 1 8 9 7 -2 12 -88]

说明(已经可以理解):

 map[?+sin 90*#]?       map a function over all the items of the input
              #         the 1-based index of the element in the input
       sin 90*#         equal to the required wave
     ?                  looping variable
     ?+sin 90*#         add the wave to the input

哈,我知道有人会提出一个基于正弦的答案。
ETHproductions

2
@ETHproductions在Mathematica中的第一个答案是基于Sine的,直到被击倒为止。在R中的第二个答案是使用正弦表达式。
帕维尔

1
@凤凰城,我没注意到我感到震惊...
ETHproductions '17

@ETHproductions 和.... Sine也从R答案中打了出来。我认为它的作用与Mathematica答案相同。
帕维尔

13

Haskell,26个字节

zipWith(+)$cycle[1,0,-1,0]

在线尝试!(运行所有测试用例)

说明:

zipWith(+)$cycle[1,0,-1,0]  -- anonymous tacit function
zipWith(+)                  -- pairwise addition between input list
          $cycle[1,0,-1,0]  -- and an infinitely-cycling "wave" list

9

JavaScript(ES6),28个字节

a=>a.map((x,i)=>x-(i%4-1)%2)

计算如下:

i%4  -1  %2
0    -1  -1
1     0   0
2     1   1
3     2   0

最后一点利用了以下事实:在JS中,调制后的负数将保留其负号(即-5 % 3 -> -2,而不是1Python中的负号)。


9

Mathematica, 26 23 22字节

Im[I^Range@Tr[1^#]]+#&

在线尝试!(数学)

注意:TIO链接适用于23字节版本,而22字节版本不兼容Mathics。


下面有一个19字节的Mathematica解决方案(带有4个初始化字节)
user202729


8

MATL11 8字节

Jyn:^Yj+

尝试一下 MATL在线上!

说明

J     % Push 1j (imaginary unit)
      % STACK; 1j
y     % Implicit input. Duplicate from below
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], 1j, [-4 3 0 1 7 9 8 -2 11 -88]
n     % Number of elements
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], 1j, 10
:     % Range
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], 1j, [1 2 3 4 5 6 7 8 9 10]
^     % Power, element-wise
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], [1j -1 -1j 1 1j -1 -1j 1 1j -1]
Yj    % Imaginary part
      % STACK: [-4 3 0 1 7 9 8 -2 11 -88], [1 0 -1 0 1 0 -1 0 1 0]
+     % Add, element-wise. Implicit display
      % STACK: [-3 3 -1 1 8 9 7 -2 12 -88]

+

@cairdcoinheringaahing谢谢,编辑
Luis

3

果冻,16字节

-1Jm2$$¦+2Jm4$$¦

在线尝试!

嘿,我确定这太长了

编辑

我知道5字节的解决方案是可能的,但是我的wifi似乎开始使我无法与之联系,所以明天我将打高尔夫球。如果有人在我打高尔夫球之前发布了简短的Jelly解决方案,那对我来说很好。我将其保留在此处以供参考,以了解我在Jelly大笑的另一种方式。我的意思是,我只能看一下Phoenix在评论中发布的链接,但是由于我仍在学习,因此,在我自己解决问题之前,我不希望看到该解决方案。这可能会损害我的声誉,但是学习是我在这里的目的:)))


LeakyNun在5聊天中做到了:剧透
Pavel

5
哦.__________。
HyperNeutrino

丹尼斯(Dennis)收到了它:codegolf.stackexchange.com/a/135145/60042
Pavel



3

哈斯克尔,26个字节

@Mego击败了我这个解决方案

zipWith(+)$cycle[1,0,-1,0]

在线尝试!

这就是Haskell擅长的领域。这声明了一个无点函数,该函数用无限列表压缩输入。

哈斯克尔,56个字节

这是使用复数的解决方案。由于进口的原因竞争力不强,但从来没有那么酷。

import Data.Complex
zipWith((+).realPart.((0:+1)^))[0..]

在线尝试!


2
ek!你忍了我20秒!
Mego

有两个相同的解决方案是没有意义的。由于您在没有归属的情况下接受了我的改进,并且使我们的答案相同,因此您会删除您的答案吗?
Mego

3

Mathematica,19个字节

i=1;#+Im[i*=I]&/@#&

说明

i=1;#+Im[i*=I]&/@#&
i=1;                 (* set variable i to 1 *)
               /@#   (* iterate through the input: *)
    #+Im[i   ]&      (* add the imaginary component of i... *)
          *=I        (* multiplying i by the imaginary unit each iteration *)

注意:i=1出现在函数之外,根据此meta共识可以


但是,此函数不一定可重用(如果在调用一次函数后的i值不同于1)
user202729

@ user202729我链接的元共识专门处理了该问题。可以在函数外部声明全局变量。
JungHwan Min

3

J,12个字节

+1 0 _1 0$~#

在线尝试!

因为J的形状运算符是$周期性填充的,所以当我们将其整形#为输入的长度时,它的作用完全符合我们想要的,我们可以将其添加到输入中]


您可以通过删除第一个](即使用钩子)来保存字节
-Tikkanz

@Tikkanz不错的收获。我已经更新了帖子。
乔纳

3

C ++,93 85 83 63字节

auto w=[](auto&i){for(int j=0;j<i.size();j+=2)i[j]+=j%4?-1:1;};

-8个字节,感谢此答案,我发现lambda参数可以auto并且您可以传递正确的参数,它将起作用

-2个字节,感谢Nevay

-2个字节,感谢Zacharý

我删除了vector包含。您将需要将满足以下条件的参数作为参数传递给wa container:

  • 有一个方法叫做 size没有参数
  • 下标运算符已重载

尊重下列条件STL容器是arrayvectorstringmapunordered_map,或者其他人

如果不允许通过修改参数输出参数,则:

C ++ 112110字节

#include<vector>
std::vector<int>w(std::vector<int>i){for(int j=0;j<i.size();j+=2)i[j]+=(j%4)?-1:1;return i;}

1
您的第一个有效的I / O。
帕维尔

1
您可以j%4用来保存2个字节。
涅瓦

1
我认为你不需要周围的人j%4
扎卡里


2

Dyalog APL,13个字节

⊢+1 0 ¯1 0⍴⍨≢

在线尝试!

怎么样?

1 0 ¯1 0 -数组[1、0,-1、0]

⍴⍨≢ -调整为输入的长度,循环

⊢+ -输入的矢量和


2

Perl 6,28个字节

{((1+0i,*×i...*)Z+$_)».re}

在线尝试!

1+0i, * × i ... *产生1, i, -1, -i循环中重复的数字的无限列表。将这些数字Z+与输入列表($_)进行加法运算(),然后提取所得复数的实数分量(».re)。



2

贾普特11)个10字节

利用Japt的索引包装。

Ë+[1TJT]gE

测试一下


说明

数组的隐式输入U

Ë

映射到数组。

+

在当前元素中添加...

gE

当前索引(E)处的元素...

[1TJT]

在数组中[1,0,-1,0]


1

实际上,11个字节

;r⌠╦½*C≈⌡M¥

在线尝试!(运行所有测试用例)

说明:

;r⌠╦½*C≈⌡M¥
;r           range(len(input))
  ⌠╦½*C≈⌡M   for each value in range:
   ˫*C      cos(pi/2*value)
       ≈     floor to integer
          ¥  pairwise addition of the input and the new list



1

Math.JS,34个字节

f(k)=k.map(j(x,y,z)=x+im(i^y[1]))

解释

f(k)=k.map(j(x,y,z)=x+im(i^y[1]))
f(k)=                               # Define a function f, which takes argument k.
     k.map(                     )   # Map k to a function
           j(x,y,z)=                # Function j. Takes arguments x, y, and z. Where x is the item, y is the index in the form [i], and z is the original list.
                      im(      )    # The imaginary component of...
                         i^y[1]     # i to the power of the index.
                    x+              # x +, which gives our wave.

在线尝试!


1

8th96 63字节

a:new swap ( swap 90 * deg>rad n:cos int + a:push ) a:each drop

此代码将结果数组留在TOS上

用法和示例

ok> [0,0,0,0,0] a:new swap ( swap 90 n:* deg>rad n:cos n:int n:+ a:push ) a:each drop .
[1,0,-1,0,1]

ok> [-4,3,0,1,7,9,8,-2,11,-88] a:new swap ( swap 90 * deg>rad n:cos int + a:push ) a:each drop .
[-3,3,-1,1,8,9,7,-2,12,-88]

说明

我们使用cos(x)来获得正确的序列[1,0,-1,0]。将每个数组元素的索引乘以90度,然后将其传递给cos()函数以获取所需的“波动系数”,以将其添加到相应的项中。

: f \ a -- a
  a:new    \ create output array
  swap     \ put input array on TOS
  \ array element's index is passed to cos in order to compute
  \ the "wave factor" to add to each item
  ( swap 90 n:* deg>rad n:cos n:int n:+ 
  a:push ) \ push new item into output array 
  a:each
  drop     \ get rid of input array and leave ouput array on TOS
;



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.