| Bytes | Lang | Time | Link |
|---|---|---|---|
| 041 | AWK | 250910T174411Z | xrs |
| 009 | Pyt | 230218T135006Z | Kip the |
| 034 | Zsh | 230214T122954Z | roblogic |
| 022 | Perl 6 | 170723T203540Z | Sean |
| 033 | Arturo | 230214T064453Z | chunes |
| 008 | MATL | 170723T023850Z | Luis Men |
| 010 | Japt | 170723T094942Z | Shaggy |
| 063 | C++ | 170723T135340Z | HatsuPoi |
| 056 | D | 170904T164542Z | Adalynn |
| 038 | Ruby | 170724T191247Z | Alex |
| 016 | 05AB1E | 170724T180024Z | Magic Oc |
| 012 | J | 170723T032009Z | Jonah |
| 005 | Jelly | 170723T033112Z | Dennis |
| 050 | C# .NET Core | 170724T164415Z | jkelm |
| 063 | 8th | 170723T200003Z | Chaos Ma |
| 034 | Math.JS | 170723T230916Z | ATaco |
| 013 | Dyalog APL | 170723T140756Z | Uriel |
| 019 | Mathematica | 170723T141224Z | JungHwan |
| 018 | LOGO | 170723T023740Z | user2027 |
| 015 | CJam | 170723T102039Z | Erik the |
| 011 | Pyth | 170723T060915Z | deltaeps |
| 026 | Haskell | 170723T033249Z | Wheat Wi |
| 030 | Pari/GP | 170723T034117Z | alephalp |
| 040 | Python 2 | 170723T033213Z | xnor |
| 026 | Haskell | 170723T033310Z | user4594 |
| 042 | Python 2 | 170723T023105Z | musicman |
| 022 | Mathematica | 170723T022634Z | user2027 |
| 011 | Actually | 170723T024951Z | user4594 |
| 024 | R | 170723T022824Z | Giuseppe |
| 016 | Jelly | 170723T023017Z | hyperneu |
| 028 | JavaScript ES6 | 170723T022928Z | ETHprodu |
Pyt, 9 bytes
ĐŁřπ*₂šƖ+
Đ implicit input; Đuplicate
Ł get Łength
ř řangify
π*₂ multiply by pi/2
š take šin
Ɩ cast to Ɩnteger
+ add the two arrays element-wise
Perl 6, 28 22 bytes
{((1+0i,×i...)Z+$_)».re}
(*Z-(i*i,*i...*))».re
i*i, *i ... * produces an infinite list of the numbers -1, -i, 1, i repeateded in a cycle. Those numbers are zipped with subtraction (Z-) with the input list (*), and then the real components of the resulting complex numbers are extracted (».re).
MATL, 11 8 bytes
Jyn:^Yj+
Try it at MATL Online!
Explanation
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]
Japt, 11 10 bytes
Takes advantage of Japt's index wrapping.
Ë+[1TJT]gE
Explanation
Implicit input of array U.
Ë
Map over the array.
+
To the current element add...
gE
The element at the current index (E)...
[1TJT]
In the array [1,0,-1,0].
C++, 93 85 83 63 bytes
auto w=[](auto&i){for(int j=0;j<i.size();j+=2)i[j]+=j%4?-1:1;};
-8 bytes, thanks to this answer, i discovered that lambda parameters can be auto and you can pass with the correct parameter, it will work
-2 bytes thanks to Nevay
-2 bytes thanks to Zacharý
I removed the vector include. You will need to pass as argument to w a container that respect the following conditions :
- Have a method called
sizewith no arguments - Have overloaded the subscript operator
STL Containers that respect the following conditions are array, vector, string, map, unordered_map, and maybe others
If outputting by modifying arguments arguments is not allowed, then :
C++, 112 110 bytes
#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;}
D, 56 bytes
void w(T)(T[]i){for(T j;j<i.length;j+=2)i[j]+=j%4?-1:1;}
This is a port of HatsuPointerKun's C++ answer, so don't forget about them!
05AB1E, 16 bytes
vy3L2.SR0¸«Nè+})
3L2.SR0¸« is the shortest thing I can think of for sin(x % 4) in 05AB1E.
J, 12 bytes
+1 0 _1 0$~#
Because J's shape operator $ fills cyclically, when we shape it to the length # of the input, it does exactly what we want, and we can just add it to the input ]
Jelly, 5 bytes
Jı*Ċ+
How it works
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.
C# (.NET Core), 50 bytes
n=>{for(int i=0;i<n.Length;i+=2)n[i]+=i%4<1?1:-1;}
Uses a simple lambda. Modifies the original array and returns the output through reference.
8th, 96 63 bytes
Code
a:new swap ( swap 90 * deg>rad n:cos int + a:push ) a:each drop
This code leaves resulting array on TOS
Usage and examples
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]
Explanation
We use cos(x) in order get the right sequence [1,0,-1,0]. Each array element's index is multiplied by 90 degrees and then it is passed to cos() function to get the desired "wave factor" to be added to the corresponding item.
: 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
;
Math.JS, 34 bytes
f(k)=k.map(j(x,y,z)=x+im(i^y[1]))
Explained
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.
Dyalog APL, 13 bytes
⊢+1 0 ¯1 0⍴⍨≢
How?
1 0 ¯1 0 - the array [1, 0, -1, 0]
⍴⍨≢ - reshape to the length of the input, cyclic
⊢+ - vectorized sum with the input
Mathematica, 19 bytes
i=1;#+Im[i*=I]&/@#&
Explanation
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 *)
Note: i=1 appears outside of the function, which is okay per this meta consensus.
LOGO, 18 bytes
[map[?+sin 90*#]?]
There is no "Try it online!" link because all online LOGO interpreter does not support template-list.
That is a template-list (equivalent of lambda function in other languages).
Usage:
pr invoke [map[?+sin 90*#]?] [-4 3 0 1 7 9 8 -2 11 -88]
(invoke calls the function, pr prints the result)
prints [-3 3 -1 1 8 9 7 -2 12 -88].
Explanation (already pretty understandable):
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
Haskell, 26 bytes
@Mego beat me to this solution
zipWith(+)$cycle[1,0,-1,0]
This is what Haskell is great at. This declares a point-free function that zips the input with an infinite list.
Haskell, 56 bytes
Here's a solution that uses complex numbers. Not very competitive because of the import but never the less pretty cool.
import Data.Complex
zipWith((+).realPart.((0:+1)^))[0..]
Haskell, 26 bytes
zipWith(+)$cycle[1,0,-1,0]
Try it online! (runs all test cases)
Explanation:
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
Python 2, 50 42 bytes
Saved 8 bytes thanks to @Sisyphus!
lambda l:map(sum,zip(l,[1,0,-1,0]*len(l)))
53 bytes
lambda l:[int(x+(1j**i).real)for i,x in enumerate(l)]
Mathematica, 26 23 22 bytes
Im[I^Range@Tr[1^#]]+#&
Try it online! (Mathics)
Note: The TIO link is for the 23-byte version, the 22-byte version is not Mathics-compatible.
Actually, 11 bytes
;r⌠╦½*C≈⌡M¥
Try it online! (runs all test cases)
Explanation:
;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
Jelly, 16 bytes
-1Jm2$$¦+2Jm4$$¦
heh I'm sure this is too long
Edit
I know a 5 byte solution is possible but my wifi appears to be starting to cut me off so I'll golf this tomorrow. If someone posts the short Jelly solution before I can golf this, that's fine with me; I'll just keep this here for reference as to how bad I am at Jelly lol another way of doing it. I mean, I could just look at the link Phoenix posted in the comments, but since I'm still learning, I don't want to look at the solution until I've figured it out myself. This might cost me reputation but the learning is what I'm here for :)))
JavaScript (ES6), 28 bytes
a=>a.map((x,i)=>x-(i%4-1)%2)
The calculation goes like this:
i%4 -1 %2
0 -1 -1
1 0 0
2 1 1
3 2 0
The last bit taking advantage of the fact that in JS, a negative number when modulated will retain its negative sign (i.e. -5 % 3 -> -2, instead of 1 as it would be in Python).