Reverse a 1-dimensional array

-19

1

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.

Erik the Outgolfer

Posted 2016-05-02T17:29:34.800

Reputation: 38 134

1The empty array and a single-element array would probably be good test cases. – Martin Ender – 2016-05-02T17:33:37.457

21

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 – 2016-05-02T17:35:51.293

3"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 – 2016-05-02T17:36:19.687

8@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 – 2016-05-02T17:42:48.223

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 – 2016-05-02T18:03:29.843

@Doorknob And it would be one byte in Vitsy, save for the rigid input format. – Addison Crump – 2016-05-02T18:10:18.963

@MartinBüttner Well, that IS the challenge! (replying to very rigid input format comment) – Erik the Outgolfer – 2016-05-03T08:54:05.870

@ΈρικΚωνσταντόπουλος That's also why the challenge is so unpopular: if you want to make a challenge about string processing, make a challenge about string processing. But making a challenge that is seemingly about reversing an array, but then is dominated by string processing in arbitrary set of languages (while it can be solved with a single built-in in languages that get the string processing for free because the format happens to match their syntax), is mostly not in the spirit of this community. – Martin Ender – 2016-05-03T08:57:17.130

@MartinBüttner I have specified that both arrays and strings are allowed. – Erik the Outgolfer – 2016-05-03T09:06:40.780

But we still have the strict output format. – CalculatorFeline – 2016-05-03T14:35:58.460

@CatsAreFluffy I don't see anywhere that we are supposed to output to stdout, even less that there's a strict output format. – Katenkyo – 2016-05-03T14:38:11.210

See the Molecule answer. – CalculatorFeline – 2016-05-03T14:40:32.353

@CatsAreFluffy if this is only in a comment on a submission, you can practically ignore it, as it isn't in the specs – Katenkyo – 2016-05-03T14:43:52.083

@CatsAreFluffy I changed output format, although I didn't like that change at all. – Erik the Outgolfer – 2016-05-12T07:44:02.587

Answers

12

Jelly, 1 byte

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

Conor O'Brien

Posted 2016-05-02T17:29:34.800

Reputation: 36 228

2Oh, there is a flat version of U? I've always been using U'... – Leaky Nun – 2016-05-03T12:06:27.413

9

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.

NoOneIsHere

Posted 2016-05-02T17:29:34.800

Reputation: 1 916

1All of a sudden, +2! Yay! (Maybe my username change...) – NoOneIsHere – 2016-12-01T18:28:58.207

6

Python 2, 8 bytes

reversed

This is too short.

pppery

Posted 2016-05-02T17:29:34.800

Reputation: 3 987

1reversed is the name for a built in function in python that takes an iterable and reverses it. – pppery – 2016-06-03T21:09:33.927

I think this returns an iterable, not an array. – Erik the Outgolfer – 2016-09-28T13:54:19.693

3@EriktheGolfer Where does it say the output has to be an array – pppery – 2016-09-28T17:32:23.093

6

, 3 bytes

Literally reverses array. Implicitly performs I/O.

emiflake

Posted 2016-05-02T17:29:34.800

Reputation: 131

4

Python 3, 16 bytes

lambda x:x[::-1]

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

Morgan Thrapp

Posted 2016-05-02T17:29:34.800

Reputation: 3 574

2Clever! 8 more to go... – Erik the Outgolfer – 2016-05-03T08:52:42.893

4

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

MegaTom

Posted 2016-05-02T17:29:34.800

Reputation: 3 787

3

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.

Fatalize

Posted 2016-05-02T17:29:34.800

Reputation: 32 976

@Downvoter any reason why this answer is less interesting than all others? – Fatalize – 2016-05-03T13:32:35.150

3That guy seems to have down voted every answer. – CalculatorFeline – 2016-05-03T14:33:04.003

@CatsAreFluffy well that's interesting… – Fatalize – 2016-05-03T14:35:39.427

@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 – 2016-05-03T20:59:50.127

Nope, that's downvoted too. – CalculatorFeline – 2016-05-03T22:10:17.907

The only non-downvoted one from May 2 is the Jelly answer... – caird coinheringaahing – 2017-06-21T14:14:52.743

2

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$

12Me21

Posted 2016-05-02T17:29:34.800

Reputation: 6 110

2

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.

internet_user

Posted 2016-05-02T17:29:34.800

Reputation: 314

2

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.

Kevin Cruijssen

Posted 2016-05-02T17:29:34.800

Reputation: 67 575

71 bytes for the loop. – Olivier Grégoire – 2017-11-14T10:02:21.690

1@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 – 2017-11-14T10:05:15.967

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 – 2017-11-14T10:18:14.783

@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 – 2017-11-14T11:37:31.177

2

Python 2, 19 bytes

print input()[::-1]

Erik the Outgolfer

Posted 2016-05-02T17:29:34.800

Reputation: 38 134

Hold on... doesn't python 2 have 'raw_input()' instead of 'input()'? – Yytsi – 2016-05-11T15:39:31.980

2@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) – James – 2016-05-11T19:30:20.490

@DrGreenEggsandHamDJ Oh. Thanks for telling O.o! – Yytsi – 2016-05-11T19:33:43.383

@DrGreenEggsandHamDJ We count in bytes, not chars, tho. – Erik the Outgolfer – 2016-05-12T07:42:11.830

2

05AB1E, 1 byte

Code:

R

Try it online!.

Adnan

Posted 2016-05-02T17:29:34.800

Reputation: 41 965

2

Ruby, 14 bytes

you can't get simpler than this:

->a{a.reverse}

MegaTom

Posted 2016-05-02T17:29:34.800

Reputation: 3 787

2If you want more idiomatic Ruby, a.method:reverse is only two bytes longer. – Fund Monica's Lawsuit – 2016-05-09T21:31:06.407

2

JavaScript ES6, 14 bytes

x=>x.reverse()

Simple enough. Anonymous lambda that reverses an array.

Conor O'Brien

Posted 2016-05-02T17:29:34.800

Reputation: 36 228

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 – 2016-05-09T21:32:11.053

1@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 – 2016-05-09T21:54:20.950

Oh, I see. Darn, that'd have saved a few bytes. – Fund Monica's Lawsuit – 2016-05-09T21:59:15.473

2

Actually, 1 byte

R

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

Mego

Posted 2016-05-02T17:29:34.800

Reputation: 32 998

1

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!

user85052

Posted 2016-05-02T17:29:34.800

Reputation:

1

Pyke, 1 byte

_

Try it here!

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

Blue

Posted 2016-05-02T17:29:34.800

Reputation: 26 661

1

Perl 5, 11 bytes

A subroutine:

{reverse@_}

msh210

Posted 2016-05-02T17:29:34.800

Reputation: 3 094

0

TI-Basic, 16 bytes

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

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

Timtech

Posted 2016-05-02T17:29:34.800

Reputation: 12 038

0

Triangularity, 7 bytes

.).
IER

Try it online!

Mr. Xcoder

Posted 2016-05-02T17:29:34.800

Reputation: 39 774

0

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)

user202729

Posted 2016-05-02T17:29:34.800

Reputation: 14 620

I think that you could argue you define a function R like this.

– Erik the Outgolfer – 2018-05-14T14:09:16.290

0

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.

user202729

Posted 2016-05-02T17:29:34.800

Reputation: 14 620

0

MathGolf, 1 byte

Nothing interesting. MathGolf has a built-in.

x

Try it online!

user85052

Posted 2016-05-02T17:29:34.800

Reputation:

0

Jolf, 1 + 1 = 2 bytes

_

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

Conor O'Brien

Posted 2016-05-02T17:29:34.800

Reputation: 36 228

Why did you add 1? – msh210 – 2016-07-22T22:10:06.440

@msh210 1 byte to ensure the pretty print option is enabled. – Conor O'Brien – 2016-07-22T22:13:48.913

0

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]

user47018

Posted 2016-05-02T17:29:34.800

Reputation:

This isn't the correct output format, unfortunately. – Conor O'Brien – 2016-05-02T18:54:41.090

yeah, but that's just because this language doesn't do commas between objects while creating arrays. – None – 2016-05-02T18:57:15.277

As much as I'd agree with you, this challenge requires a strict I/o format. – Conor O'Brien – 2016-05-02T19:03:07.720

There is no longer a strict input format, I think. – NoOneIsHere – 2016-05-22T17:30:07.037

0

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

Katenkyo

Posted 2016-05-02T17:29:34.800

Reputation: 2 857

0

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!

msh210

Posted 2016-05-02T17:29:34.800

Reputation: 3 094

0

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

Titus

Posted 2016-05-02T17:29:34.800

Reputation: 13 814

-1

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

Erik the Outgolfer

Posted 2016-05-02T17:29:34.800

Reputation: 38 134