# Reverse a 1-dimensional array

Note: This challenge is not the same.

# Challenge

Believe it or not, we haven't got ONE challenge for reversing one-dimensional arrays (although we've got one for n-dimensional ones)! This should operate only on the 1st dimension, not on all dimensions of an array.

# Rules

• Standard loopholes are denied
• [[1, 2], [3, 4]] becomes [[3, 4], [1, 2]], not [[4, 3], [2, 1]].
• This is , but no answer is accepted. Go beat the others!
• You can get the array any way, except hardcoding. You can also get a string and process it.
• The input will be an array (yes, commas or no commas, it needs to be an array).

# Test cases

These are the test cases:

[1, 2, 3, 4]
[1, 2, 'hello', 'world!']
[[1, 2, 3, 4], [2, 3, 4, 1]]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]


These are the supposed results:

[4, 3, 2, 1]
['world!', 'hello', 2, 1]
[[2, 3, 4, 1], [1, 2, 3, 4]]
[23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]


You may accept input as [1 2 3 4], or even 1 2 3 4, or any other form or array your language has.

• The empty array and a single-element array would probably be good test cases. – Martin Ender May 2 '16 at 17:33
• I'm not a huge fan of the very rigid input format. You've chosen one specific way languages denote arrays which gives an arbitrary benefit to all languages using this syntax. Other languages might not use , as separators (but only spaces), or use ; instead, and some might use () or {} instead of []. On top of that, many languages don't support arrays of mixed type, or strings at all. It's normally a good idea to allow people to take the array in the most natural form for their language. – Martin Ender May 2 '16 at 17:35
• "Believe it or not, we haven't got ONE challenge for reversing one-dimensional arrays" Probably because it's extremely trivial. One byte in both Pyth and Jelly. – Doorknob May 2 '16 at 17:36
• @Doorknob I don't think simple challenges are a problem. But since they are only interesting in languages which don't have built-ins for them (i.e. esolangs in this case), it's a bit pointless to pose a simple challenge without making it as inclusive for all languages as possible. – Martin Ender May 2 '16 at 17:42
• Do we need to take input as a string, or an array? Some of the specifications you list look like you want strings, but none of the active answers do it that way. – MegaTom May 2 '16 at 18:03

# Jelly, 1 byte

Ṛ


Try it online! It reverses an array. (yay this is my first Jelly answer!)

• Oh, there is a flat version of U? I've always been using U'... – Leaky Nun May 3 '16 at 12:06

# Pyth, 2 1 bytes

1 byte off thanks to Doorknob

_


_ is reverse, and Pyth takes implicit input. Yes, my first Pyth answer is an underscore.

• All of a sudden, +2! Yay! (Maybe my username change...) – NoOneIsHere Dec 1 '16 at 18:28

# Python 2, 8 bytes

reversed


This is too short.

• reversed is the name for a built in function in python that takes an iterable and reverses it. – pppery Jun 3 '16 at 21:09
• I think this returns an iterable, not an array. – Erik the Outgolfer Sep 28 '16 at 13:54
• @EriktheGolfer Where does it say the output has to be an array – pppery Sep 28 '16 at 17:32

# 𝔼𝕊𝕄𝕚𝕟, 3 bytes

ᴚ


Literally reverses array. Implicitly performs I/O.

# Python 3, 16 bytes

lambda x:x[::-1]


This isn't even enough characters to post as an answer.

• Clever! 8 more to go... – Erik the Outgolfer May 3 '16 at 8:52

# C, 65 bytes

Takes a pointer to the start of the list and a list length.

void f(int*a,int s){int t=*a;*a=a[--s];a[s--]=t;if(s>1)f(a+1,s);}


This is a recursive algorithm that swaps the first element with the last, and then runs the same function on the middle.

Ungolfed:

void f(int* a, int s){
int t = a[0];
a[0] = a[--s];
a[s--] = t;
if(s > 1){
f(a + 1, s - 2);
}
}


# Brachylog, 2 bytes

r.


which expects a list as Input and unifies the reverse with the Output.

### Alternative

rw


which expects a list as Input and writes the reverse to STDOUT.

• @Downvoter any reason why this answer is less interesting than all others? – Fatalize May 3 '16 at 13:32
• That guy seems to have down voted every answer. – CalculatorFeline May 3 '16 at 14:33
• @CatsAreFluffy well that's interesting… – Fatalize May 3 '16 at 14:35
• @CatsAreFluffy Except possibly the OP's answer. I checked before and after, but I can't be sure because I don't have 750 rep. – NoOneIsHere May 3 '16 at 20:59
• Nope, that's downvoted too. – CalculatorFeline May 3 '16 at 22:10

# Python 2, 19 bytes

print input()[::-1]

• Hold on... doesn't python 2 have 'raw_input()' instead of 'input()'? – Yytsi May 11 '16 at 15:39
• @TuukkaX Yes, but that returns a string. In python 2, input() is equivalent to eval(input()) meaning it returns a list instead of a string in this instance. Python 3 is significantly longer: print(eval(input()[::-1])) (26 chars) – DJMcMayhem May 11 '16 at 19:30
• @DrGreenEggsandHamDJ Oh. Thanks for telling O.o! – Yytsi May 11 '16 at 19:33
• @DrGreenEggsandHamDJ We count in bytes, not chars, tho. – Erik the Outgolfer May 12 '16 at 7:42

# 05AB1E, 1 byte

Code:

R


# Ruby, 14 bytes

you can't get simpler than this:

->a{a.reverse}

• If you want more idiomatic Ruby, a.method:reverse is only two bytes longer. – Fund Monica's Lawsuit May 9 '16 at 21:31

# JavaScript ES6, 14 bytes

x=>x.reverse()


Simple enough. Anonymous lambda that reverses an array.

• Couldn't you do x.reverse as well? It returns a function object which, when called, returns the reversed version of x, which is exactly what this one does. – Fund Monica's Lawsuit May 9 '16 at 21:32
• @QPaysTaxes Nope. x.reverse is the general method inherited from the Array prototype. For you solution to work, we'd have to re-bind it to the original instance, as x=>x.reverse.bind(x) – Conor O'Brien May 9 '16 at 21:54
• Oh, I see. Darn, that'd have saved a few bytes. – Fund Monica's Lawsuit May 9 '16 at 21:59

## Actually, 1 byte

R


Reverses the input list. These are words to make this answer long enough.

# SmileBASIC, 34 bytes

DEF R A
DIM T[LEN(A)]RSORT T,A
END


RSORT actually just sorts the arrays, and then reverses them.

### Cheating answer: (requires turning the screen upside down):

LINPUT S$ATTR 2?S$


# Python 3, 12 bytes

list.reverse


Surprised nobody else used this. Reverses in place, but I don't think there are any restrictions on that.

# Java 8, 6238 30 bytes

java.util.Collections::reverse


Modifies the input-ArrayList instead of returning a new one to save bytes.

-8 bytes thanks to @OliverGrégoire.

Try it here.

With an array as input instead of ArrayList (62 bytes):

import java.util.*;a->{Collections.reverse(Arrays.asList(a));}


Modifies the input-array instead of returning a new one to save bytes.

Try it here.

Looping over the array to reverse it would be 75 69 bytes instead:

a->{Object t;for(int i=0,j=a.length;i<--j;a[i++]=a[j],a[j]=t)t=a[i];}


Try it here.

-5 bytes thanks to @OliverGrégoire.

• 71 bytes for the loop. – Olivier Grégoire Nov 14 '17 at 10:02
• @OlivierGrégoire 69 you mean: a->{Object t;for(int s=0,e=a.length;s<--e;a[s++]=a[e],a[e]=t)t=a[s];} ;) But thanks – Kevin Cruijssen Nov 14 '17 at 10:05
• Yes, I don't know where my mind was. Sorry... Also, for the api-answer, java.util.Collections::reverse (30 bytes) should be enough, because a list is an array, conceptually speaking. – Olivier Grégoire Nov 14 '17 at 10:18
• @OlivierGrégoire Yeah, I had l->{java.util.Collections.reverse(l);} at first, but "The input will be an array (yes, commas or no commas, it needs to be an array)." sounded like I couldn't use a List. But I now realize he means it must be an array/list/set/etc. and cannot be a String. – Kevin Cruijssen Nov 14 '17 at 11:37

## Pyke, 1 byte

_


Try it here!

Pyke has implicit input and output. Polyglot with Pyth, Jolf,

# Perl 5, 11 bytes

A subroutine:

{reverse@_}


# Keg, 1 byte

Nothing interesting. Keg has a built-in. (Since Keg does not have any other means of representing an array, let's assume the stack is the array (and in fact it is).)

?


Try it online!

# Jolf, 1 + 1 = 2 bytes

_


Try it here! (bonus for polyglot with Pyth?) _ reverses (negates) an array. (Make sure pretty output is on.)

• Why did you add 1? – msh210 Jul 22 '16 at 22:10
• @msh210 1 byte to ensure the pretty print option is enabled. – Conor O'Brien Jul 22 '16 at 22:13

# Molecule, 8 bytes (non-competing)

I"Ar"+n


Explanation:

I"Ar"+n
I"Ar"    Read input, append the command (reverse array) as a string.
+   concatenate the 2 strings
n set the program's source code to that new string.


Simple I/O:

Input    | Output
[5 "n"]    [n 5.0]
['n 3]     [3.0 n]

• This isn't the correct output format, unfortunately. – Conor O'Brien May 2 '16 at 18:54
• yeah, but that's just because this language doesn't do commas between objects while creating arrays. – user47018 May 2 '16 at 18:57
• As much as I'd agree with you, this challenge requires a strict I/o format. – Conor O'Brien May 2 '16 at 19:03
• There is no longer a strict input format, I think. – NoOneIsHere May 22 '16 at 17:30

## Lua, 61 bytes

It beats C! \o/

It simply fill a new table in the reversed order.

function f(g)h={}for i=0,#g do h[#g-i]=g[i+1]end return h end


It could have been done by working on the pointer, suppressing the return statement and the new table definition, but it actually is longer by 2 bytes

function f(g)for i=1,#g/2 do x=#g-i+1g[x],g[i]=g[i],g[x]end end


# MySQL, 64 bytes

set@=1;select a from(select a,@:=@+1 n from t order by n desc)u;


Tested on 5.6.24.

# PHP, 17 bytes

$r=array_reverse;  call $r($array) non-built-in version, 53 bytes: function r($a){while($a)$b[]=array_pop($a);return$b;}


call r($array) generator for PHP >= 5.5, 45 bytes function y($a){while($a)yield array_pop($a);}


usage: foreach(y($array)as$v)var_dump(\$v);

# TI-Basic, 16 bytes

seq(Ans(I),I,dim(Ans),1,~1


No built-ins for reversing an array in TI-Basic...

# Triangularity, 7 bytes

.).
IER


Try it online!

# Dodos, 43 bytes

	main dab
dab - a
-
- dip
a
dot dab
dot


Try it online!

(the - function subtracts and a is lambda x: [sum(x[1:]), sum(x)]. So dab - a takes the first element of a list)

• I think that you could argue you define a function R like this. – Erik the Outgolfer May 14 '18 at 14:09

# Preproc, 279 bytes

#define v(x)Z(Z(Z(Z(Z(x)))))
#define Z(x)Y(Y(Y(Y(Y(x)))))
#define Y(x)X(X(X(X(X(x)))))
#define X(x)e(e(e(e(e(x)))))
#define e(x,...)x
#define d()
#define s(x,y,...)y
#define r(x,y...)i(x,,R d d d d()()()()()(y)x)
#define C,
#define R()r
#define i(x,v...)s d()(C##x v)
v(r e()(I))


Try it online!

Some notes.

• Because the language (one pass of the C preprocessor) is not Turing-complete (as far as I know), this can only process arrays up to a fixed size. That can be increased by making v(x) evaluate e(x) more times.
• Input on command line, separated with , (please only give non-negative integers)
• Output on stdout, separated with spaces.
• My first Preproc answer. Golfing suggestions are appreciated.
• I would appreciate it if someone explain why do I need 4 nested d.

# MathGolf, 1 byte

Nothing interesting. MathGolf has a built-in.

x


Try it online!

# Python, 16 bytes

lambda l:l[::-1]
`