The 465 Arrangement

Here's the challenge. Write some code to output all the integers in a range. Sounds easy, but here's the tricky part. It will start with the lowest number, then the highest. Then the lowest number which isn't yet in the array. Then the highest which isn't yet in it.

Example:

Lets take 1 to 5 as our start

The numbers are [1, 2, 3, 4, 5].

We take the first, so . Remaining numbers are [2, 3, 4, 5]. We take the last, new array is [1, 5]. Remaining numbers are [2, 3, 4]. We take the first, new array is [1, 5, 2]. Remaining numbers are [3, 4]. We take the last, new array is [1, 5, 2, 4]. Remaining numbers are . We take the first, new array is [1, 5, 2, 4, 3]. No numbers remaining, we're done. Output [1, 5, 2, 4, 3]

Rules:

• This is code golf, write it in the fewest bytes, any language.
• No standard loopholes.
• Links to an online interpreter, please? (E.g. https://tio.run/)
• Two inputs, both integers. Low end of range, and high end of range.
• I don't mind what the data type of the output is, but it must show the numbers in the correct order.

Examples

Low: 4 High: 6 Result: 4 6 5

Low: 1 High: 5 Result: 1 5 2 4 3

Low: -1 High: 1 Result: -1 1 0

Low: -1 high: 2 Result: -1 2 0 1

Low: -50 High: 50 Result: -50 50 -49 49 -48 48 -47 47 -46 46 -45 45 -44 44 -43 43 -42 42 -41 41 -40 40 -39 39 -38 38 -37 37 -36 36 -35 35 -34 34 -33 33 -32 32 -31 31 -30 30 -29 29 -28 28 -27 27 -26 26 -25 25 -24 24 -23 23 -22 22 -21 21 -20 20 -19 19 -18 18 -17 17 -16 16 -15 15 -14 14 -13 13 -12 12 -11 11 -10 10 -9 9 -8 8 -7 7 -6 6 -5 5 -4 4 -3 3 -2 2 -1 1 0

Happy golfing!

• Almost duplicate (the difference being that this one requires reversing the second half before merging). – Peter Taylor Feb 13 at 15:13
• is the input always going to be in the order of low end, high end? – Sumner18 Feb 13 at 16:14
• @Sumner18 yes. The community here is dead-set against input validation, and I haven’t asked for a reverse-order input, so we can assume it’ll always be low - high. – AJFaraday Feb 13 at 16:21
• @Sumner18 How these challenges usually work is that we don't care how invalid inputs are handled. Your code is only judged to be successful by how it deals with valid inputs (i.e. both are integers, the first is lower than the second) – AJFaraday Feb 13 at 16:36
• @AJFaraday: you should add a note to the main post indicating that X will be always strictly lower than Y (i.e. X != Y), I missed this comment ;) – digEmAll Feb 13 at 18:23

R, 3837 36 bytes

function(a,b)rbind(a:b,b:a)[a:b-a+1]

Try it online!

• -1 byte thanks to @user2390246
• -1 byte thanks to @Kirill L.

Exploiting the fact that R stores matrices column-wise

• Using rbind is much better than my approach, but you can save 1 byte by using [seq(a:b)] instead of unique. – user2390246 Feb 13 at 18:03
• You're right, I missed the comment where has been specified that a < b (never equal), so we can use seq(a:b) – digEmAll Feb 13 at 18:06
• @digEmAll My solution was essentially a literal interpretation of the puzzle, I never would have even thought of doing something such as this. Impressive, have an upvote! – Sumner18 Feb 13 at 18:07
• -1 more – Kirill L. Feb 13 at 20:32

a%b=a:take(b-a)(b:(a+1)%(b-1))

Try it online!

• Dammit! I have just found the exact same solution. Oh well – proud haskeller Feb 14 at 13:49

R, 656461 60 bytes

-1 byte thanks to Robert S.

-4 more thanks to digEmAll

x=scan();z=x:x;while(sum(z|1)){cat(z,"");z=rev(z[-1])}

Try it online!

• You can replace length(z) with sum(z|1) to save 1 byte :) – Robert S. Feb 13 at 17:09
• I don't understand how that works but I guess it does. sum(z|1) seems like it would always evaluate to at least 1, which would cause the while loop to loop endlessly. but apparently not – Sumner18 Feb 13 at 17:27
• z is a vector. each element of that vector is |ed with 1. Which is always equal to 1. When you take the sum, you have a vector filled with TRUEs so the result is equal to the length of the vector. If the vector is empty, you the is nothing to | with so the output vector is logical(0). When you take that sum, it's 0 – OganM Feb 13 at 23:59

a#b|a>b=a:b#(a-1)|a<b=a:b#(a+1)|1>0=[a]

Try it online!

Python 2, 44 bytes

f=lambda a,b:[a]*(a==b)or[a]+f(b,a-cmp(a,b))

Try it online!

PowerShell, 59 48 bytes

param($a,$b)(($z=0..($b-$a))|%{$a+$_;$b-$_})[$z]

Try it online!

(Seems long...)

Try it online!

Ruby, 3736 33 bytes

f=->a,b{a>b ?[]:[a,b]|f[a+1,b-1]}

Try it online!

Recursive version with 3 bytes saved by G B.

Ruby, 38 bytes

->a,b{d=*c=a..b;c.map{d.reverse!.pop}}

Try it online!

Non-recursive version.

Cubix, 16 bytes

;w(.II>sO-?@;)^/

Try it here

; w
( .
I I > s O - ? @
; ) ^ / . . . .
. .
. .

Explanation

Basically, this moves the two bounds closer together one step at a time until they meet. Each time through the loop, we swap the bounds, Output, take the difference, and increment with ) or decrement with ( based on the sign.

Pyth, 10 8 bytes

{.iF_B}F

Try it here

Explanation

{.iF_B}F
}FQ  Generate the range between the (implicit) inputs.
.iF_B     Interleave it with its reverse.
{          Deduplicate.