-19
\$\begingroup\$

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.

\$\endgroup\$
14
  • 1
    \$\begingroup\$ The empty array and a single-element array would probably be good test cases. \$\endgroup\$ May 2, 2016 at 17:33
  • 23
    \$\begingroup\$ 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. \$\endgroup\$ May 2, 2016 at 17:35
  • 3
    \$\begingroup\$ "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. \$\endgroup\$
    – Doorknob
    May 2, 2016 at 17:36
  • 10
    \$\begingroup\$ @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. \$\endgroup\$ May 2, 2016 at 17:42
  • \$\begingroup\$ 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. \$\endgroup\$
    – MegaTom
    May 2, 2016 at 18:03

30 Answers 30

13
\$\begingroup\$

Jelly, 1 byte

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

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Oh, there is a flat version of U? I've always been using U'... \$\endgroup\$
    – Leaky Nun
    May 3, 2016 at 12:06
12
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ All of a sudden, +2! Yay! (Maybe my username change...) \$\endgroup\$ Dec 1, 2016 at 18:28
6
\$\begingroup\$

Python 2, 8 bytes

reversed

This is too short.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ reversed is the name for a built in function in python that takes an iterable and reverses it. \$\endgroup\$ Jun 3, 2016 at 21:09
  • \$\begingroup\$ I think this returns an iterable, not an array. \$\endgroup\$ Sep 28, 2016 at 13:54
  • 3
    \$\begingroup\$ @EriktheGolfer Where does it say the output has to be an array \$\endgroup\$ Sep 28, 2016 at 17:32
6
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 3 bytes

Literally reverses array. Implicitly performs I/O.

\$\endgroup\$
0
4
\$\begingroup\$

Python 3, 16 bytes

lambda x:x[::-1]

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

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Clever! 8 more to go... \$\endgroup\$ May 3, 2016 at 8:52
4
\$\begingroup\$

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);
  }
}
\$\endgroup\$
3
\$\begingroup\$

Python 2, 19 bytes

print input()[::-1]
\$\endgroup\$
4
  • \$\begingroup\$ Hold on... doesn't python 2 have 'raw_input()' instead of 'input()'? \$\endgroup\$
    – Yytsi
    May 11, 2016 at 15:39
  • 2
    \$\begingroup\$ @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) \$\endgroup\$
    – DJMcMayhem
    May 11, 2016 at 19:30
  • \$\begingroup\$ @DrGreenEggsandHamDJ Oh. Thanks for telling O.o! \$\endgroup\$
    – Yytsi
    May 11, 2016 at 19:33
  • \$\begingroup\$ @DrGreenEggsandHamDJ We count in bytes, not chars, tho. \$\endgroup\$ May 12, 2016 at 7:42
3
\$\begingroup\$

05AB1E, 1 byte

Code:

R

Try it online!.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ @Downvoter any reason why this answer is less interesting than all others? \$\endgroup\$
    – Fatalize
    May 3, 2016 at 13:32
  • 3
    \$\begingroup\$ That guy seems to have down voted every answer. \$\endgroup\$ May 3, 2016 at 14:33
  • \$\begingroup\$ @CatsAreFluffy well that's interesting… \$\endgroup\$
    – Fatalize
    May 3, 2016 at 14:35
  • \$\begingroup\$ @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. \$\endgroup\$ May 3, 2016 at 20:59
  • \$\begingroup\$ Nope, that's downvoted too. \$\endgroup\$ May 3, 2016 at 22:10
3
\$\begingroup\$

Java 8, 62 38 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.

\$\endgroup\$
4
  • \$\begingroup\$ 71 bytes for the loop. \$\endgroup\$ Nov 14, 2017 at 10:02
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ Nov 14, 2017 at 10:05
  • \$\begingroup\$ 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. \$\endgroup\$ Nov 14, 2017 at 10:18
  • \$\begingroup\$ @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. \$\endgroup\$ Nov 14, 2017 at 11:37
2
\$\begingroup\$

Ruby, 14 bytes

you can't get simpler than this:

->a{a.reverse}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ If you want more idiomatic Ruby, a.method:reverse is only two bytes longer. \$\endgroup\$
    – anon
    May 9, 2016 at 21:31
2
\$\begingroup\$

JavaScript ES6, 14 bytes

x=>x.reverse()

Simple enough. Anonymous lambda that reverses an array.

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – anon
    May 9, 2016 at 21:32
  • 1
    \$\begingroup\$ @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) \$\endgroup\$ May 9, 2016 at 21:54
  • \$\begingroup\$ Oh, I see. Darn, that'd have saved a few bytes. \$\endgroup\$
    – anon
    May 9, 2016 at 21:59
2
\$\begingroup\$

Actually, 1 byte

R

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

\$\endgroup\$
2
\$\begingroup\$

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$
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Jolf, 1 + 1 = 2 bytes

_

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

\$\endgroup\$
2
  • \$\begingroup\$ Why did you add 1? \$\endgroup\$
    – msh210
    Jul 22, 2016 at 22:10
  • \$\begingroup\$ @msh210 1 byte to ensure the pretty print option is enabled. \$\endgroup\$ Jul 22, 2016 at 22:13
1
\$\begingroup\$

Pyke, 1 byte

_

Try it here!

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

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 11 bytes

A subroutine:

{reverse@_}
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

MySQL, 64 bytes

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

Tested on 5.6.24.

My first SQL answer; please provide feedback!

\$\endgroup\$
0
0
\$\begingroup\$

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);

\$\endgroup\$
0
\$\begingroup\$

Python, 16 bytes

lambda l:l[::-1]

Please examine this answer again. I was a bit lazy answering this :P

Thanks to 55749 (J843136028) for suggesting to convert this to a function :)

\$\endgroup\$
0
0
\$\begingroup\$

TI-Basic, 16 bytes

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

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

\$\endgroup\$
0
\$\begingroup\$

Triangularity, 7 bytes

.).
IER

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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)

\$\endgroup\$
1
  • \$\begingroup\$ I think that you could argue you define a function R like this. \$\endgroup\$ May 14, 2018 at 14:09
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 1 byte

Nothing interesting. MathGolf has a built-in.

x

Try it online!

\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog Unicode), 1 byte

This works both on lists of lists and proper multi-dimensional arrays.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Molecule, 8 bytes

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]
\$\endgroup\$
4
  • \$\begingroup\$ This isn't the correct output format, unfortunately. \$\endgroup\$ May 2, 2016 at 18:54
  • \$\begingroup\$ yeah, but that's just because this language doesn't do commas between objects while creating arrays. \$\endgroup\$
    – user47018
    May 2, 2016 at 18:57
  • \$\begingroup\$ As much as I'd agree with you, this challenge requires a strict I/o format. \$\endgroup\$ May 2, 2016 at 19:03
  • \$\begingroup\$ There is no longer a strict input format, I think. \$\endgroup\$ May 22, 2016 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.