-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\$
  • 1
    \$\begingroup\$ The empty array and a single-element array would probably be good test cases. \$\endgroup\$ – Martin Ender May 2 '16 at 17:33
  • 21
    \$\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\$ – Martin Ender May 2 '16 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 '16 at 17:36
  • 8
    \$\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\$ – Martin Ender May 2 '16 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 '16 at 18:03

29 Answers 29

12
\$\begingroup\$

Jelly, 1 byte

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

|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ Oh, there is a flat version of U? I've always been using U'... \$\endgroup\$ – Leaky Nun May 3 '16 at 12:06
9
\$\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.

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ All of a sudden, +2! Yay! (Maybe my username change...) \$\endgroup\$ – NoOneIsHere Dec 1 '16 at 18:28
6
\$\begingroup\$

Python 2, 8 bytes

reversed

This is too short.

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

𝔼𝕊𝕄𝕚𝕟, 3 bytes

Literally reverses array. Implicitly performs I/O.

|improve this answer|||||
\$\endgroup\$
4
\$\begingroup\$

Python 3, 16 bytes

lambda x:x[::-1]

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

|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ Clever! 8 more to go... \$\endgroup\$ – Erik the Outgolfer May 3 '16 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);
  }
}
|improve this answer|||||
\$\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.

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

Python 2, 19 bytes

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

05AB1E, 1 byte

Code:

R

Try it online!.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Ruby, 14 bytes

you can't get simpler than this:

->a{a.reverse}
|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ If you want more idiomatic Ruby, a.method:reverse is only two bytes longer. \$\endgroup\$ – Fund Monica's Lawsuit May 9 '16 at 21:31
2
\$\begingroup\$

JavaScript ES6, 14 bytes

x=>x.reverse()

Simple enough. Anonymous lambda that reverses an array.

|improve this answer|||||
\$\endgroup\$
  • \$\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\$ – Fund Monica's Lawsuit May 9 '16 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\$ – Conor O'Brien May 9 '16 at 21:54
  • \$\begingroup\$ Oh, I see. Darn, that'd have saved a few bytes. \$\endgroup\$ – Fund Monica's Lawsuit May 9 '16 at 21:59
2
\$\begingroup\$

Actually, 1 byte

R

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

|improve this answer|||||
\$\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$
|improve this answer|||||
\$\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.

|improve this answer|||||
\$\endgroup\$
2
\$\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.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ 71 bytes for the loop. \$\endgroup\$ – Olivier Grégoire Nov 14 '17 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\$ – Kevin Cruijssen Nov 14 '17 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\$ – Olivier Grégoire Nov 14 '17 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\$ – Kevin Cruijssen Nov 14 '17 at 11:37
1
\$\begingroup\$

Pyke, 1 byte

_

Try it here!

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

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 11 bytes

A subroutine:

{reverse@_}
|improve this answer|||||
\$\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!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Jolf, 1 + 1 = 2 bytes

_

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

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Why did you add 1? \$\endgroup\$ – msh210 Jul 22 '16 at 22:10
  • \$\begingroup\$ @msh210 1 byte to ensure the pretty print option is enabled. \$\endgroup\$ – Conor O'Brien Jul 22 '16 at 22:13
0
\$\begingroup\$

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]
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ This isn't the correct output format, unfortunately. \$\endgroup\$ – Conor O'Brien May 2 '16 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 '16 at 18:57
  • \$\begingroup\$ As much as I'd agree with you, this challenge requires a strict I/o format. \$\endgroup\$ – Conor O'Brien May 2 '16 at 19:03
  • \$\begingroup\$ There is no longer a strict input format, I think. \$\endgroup\$ – NoOneIsHere May 22 '16 at 17:30
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
|improve this answer|||||
\$\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!

|improve this answer|||||
\$\endgroup\$
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);

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 16 bytes

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

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

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Triangularity, 7 bytes

.).
IER

Try it online!

|improve this answer|||||
\$\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)

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I think that you could argue you define a function R like this. \$\endgroup\$ – Erik the Outgolfer May 14 '18 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.
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 1 byte

Nothing interesting. MathGolf has a built-in.

x

Try it online!

|improve this answer|||||
\$\endgroup\$
-1
\$\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 :)

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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