| Bytes | Lang | Time | Link |
|---|---|---|---|
| 042 | AWK | 250905T193704Z | xrs |
| 004 | Thunno 2 | 230623T155836Z | The Thon |
| 006 | Vyxal | 221014T075741Z | DialFros |
| 005 | 05AB1E | 190308T154448Z | Cowabung |
| 123 | 1. Python 3.5 | 190308T145314Z | Rene |
| 095 | SNOBOL4 CSNOBOL4 | 190305T151411Z | Giuseppe |
| 020 | ><> | 190305T123719Z | Emigna |
| 006 | MathGolf | 190305T101542Z | maxb |
| 124 | Java 8 JDK 113 | 190305T033740Z | Benjamin |
| 045 | Ink | 190213T190123Z | Sara J |
| 2017 | C compiled with VC++ Visual Studio 2017 264bytes | 190217T233638Z | der bend |
| 052 | C gcc | 190213T194527Z | Jonathan |
| 046 | C gcc | 190217T104035Z | user7740 |
| nan | APL+WIN | 190214T000207Z | Graham |
| 023 | J | 190216T043501Z | Jonah |
| 048 | tinylisp | 190216T032758Z | DLosc |
| 015 | Brachylog | 190216T024729Z | DLosc |
| 062 | VBA Excel | 190215T080123Z | remoel |
| 062 | Clojure | 190215T073128Z | Unrelate |
| 035 | JavaScript | 190214T032454Z | tsh |
| 054 | Wolfram Language Mathematica | 190214T215032Z | Kai |
| 004 | Shortcuts for iOS | 190214T225749Z | pfg |
| 449 | JVM bytecode OpenJDK asmtools JASM | 190214T223043Z | Kitten |
| nan | PHP | 190214T014010Z | 640KB |
| 008 | Pyth | 190213T220853Z | user4854 |
| 067 | F# .NET Core | 190214T161936Z | LSM07 |
| 016 | Cubix | 190214T164153Z | user4854 |
| 046 | C# Visual C# Interactive Compiler | 190213T165057Z | Gymhgy |
| 037 | JavaScript Node.js | 190214T100206Z | ovs |
| 033 | Ruby | 190214T105645Z | Kirill L |
| 030 | Haskell | 190214T134824Z | proud ha |
| 006 | 05AB1E | 190213T170202Z | Emigna |
| 048 | Clean | 190214T012912Z | Οurous |
| 030 | Haskell | 190214T005637Z | xnor |
| 032 | Perl 6 | 190214T005058Z | Jo King |
| 017 | Charcoal | 190213T214225Z | Neil |
| 006 | Jelly | 190213T210319Z | Erik the |
| 036 | R | 190213T175909Z | digEmAll |
| 052 | Java JDK | 190213T172529Z | Olivier |
| 022 | Gol><> | 190213T202338Z | KrystosT |
| 029 | Julia 0.7 | 190213T203053Z | Kirill L |
| 052 | Forth gforth | 190213T180203Z | reffu |
| 040 | JavaScript | 190213T181334Z | Shaggy |
| 037 | Perl 5 ln | 190213T200249Z | Xcali |
| 062 | C#Visual C# Interactive Compiler | 190213T194637Z | Kramb |
| 052 | Haskell | 190213T192925Z | Jonathan |
| 039 | Haskell | 190213T190958Z | nimi |
| 019 | cQuents | 190213T182154Z | Stephen |
| 060 | R | 190213T165830Z | Sumner18 |
| 008 | MATL | 190213T175707Z | Luis Men |
| 051 | R | 190213T175938Z | user2390 |
| 048 | PowerShell | 190213T155138Z | AdmBorkB |
| 007 | Japt | 190213T172050Z | Shaggy |
| 066 | C# .NET Core | 190213T173038Z | Destroig |
| 039 | Haskell | 190213T171538Z | ovs |
| 044 | Python 2 | 190213T165805Z | ovs |
| 007 | Stax | 190213T165702Z | recursiv |
| 021 | APL dzaima/APL | 190213T160321Z | dzaima |
| 014 | Japt | 190213T154256Z | Luis fel |
Thunno 2, 4 bytes
IḲIU
Takes the upper number first.
Explanation
IḲIU # Implicit input
I # Inclusive range between the two inputs
Ḳ # Bifurcate: reverse a copy
I # Interleave them together
U # Uniquify the list
# Implicit output
Vyxal, 6 bytes
‹rI÷ṘY
Explained
‹rI÷ṘY
‹ # decrement
r # range
I÷ # split into two halves
Ṙ # reverse
Y # interleave
05AB1E, 5 bytes
ŸÂ.ιÙ
Takes input as upper first.
Explanation:
ŸÂ.ιÙ //full program
Ÿ //push [min .. max] stack: [[4, 5, 6]]
 //push range and reversed range stack: [[4, 5, 6], [6, 5, 4]]
.ι //interleave stack: [[4, 6, 5, 5, 6, 4]]
Ù //deduplicate stack: [[4, 6, 5]]
1. Python 3.5, 123 bytes
Script takes two params: interval from, interval to.
from sys import*;v=[*range(int(argv[1]),int(argv[2])+1)];o=[]
for i in range(len(v)):v.reverse();o.append(v.pop())
print(o)
example:
$ ./script.py 1 5
[1, 5, 2, 4, 3]
$ ./script.py -5 6
[-5, 6, -4, 5, -3, 4, -2, 3, -1, 2, 0, 1]
Explanation
# create range input params
v=[*range(int(argv[1]),int(argv[2])+1)]
# define ampty output array
o=[]
# loop input-array-lenght times
for i in range(len(v)):
# reverse input array
v.reverse()
# append last input element to output
o.append(v.pop())
# print output array
print(o)
SNOBOL4 (CSNOBOL4), 95 bytes
A =INPUT
B =INPUT
L OUTPUT =LE(A,B) A :F(END)
OUTPUT =GT(B,A) B
A =A + 1
B =B - 1 :(L)
END
MathGolf, 6 bytes
↨_x^─▀
Explanation with (1, 5)
↨ inclusive range from a to b [1, 2, 3, 4, 5]
_ duplicate TOS [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]
x reverse int/array/string [1, 2, 3, 4, 5], [5, 4, 3, 2, 1]
^ zip top two elements on stack [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]
─ flatten array [1, 5, 2, 4, 3, 3, 4, 2, 5, 1]
▀ unique elements of string/list [1, 5, 2, 4, 3]
The reason why this works is due to the fact that all elements in the output should be unique, so the unique elements operator will filter out the second half of the array, producing the correct output.
Java 8 (JDK): 113, 98, 124 bytes
(l,h)->{String s="";while(h>=l)s+=l+++" "+h--+" ";return s.substring(0,s.length()-(l-h==2?(l+(l<0?".":"")).length()+2:1));};
Thanka to Sriotchilism O'Zaic and Embodiment of Ignorance for saving me 15 bytes! Unfortunately those bytes were used to fix a bug Jo King mentioned.
Try it online
My first every golf code submission. Code is a functional interface that takes 2 integers and builds a string from those integers. The substring hack at the end hides a little quirk that duplicates the last element of the "array." Probably not the best solution, but for my first ever submission, I think I did well :)
Ink, 45 bytes
=h(I,A)
{I<=A:{I} {I<A:{A} ->h(I+1,A-1)}}->->
(I don't think there's an online interpreter for Ink, sorry)
Try it online!
Defines a stitch called h, which takes two arguments I and A, which are the bounds of the range.
Outputs by printing values, separated by spaces, to stdout.
Explanation
=h(min, max) // Define the stitch.
{min <= max:{min}/* print min unless it's greater than max */{min < max: {max} /*Also print max if it's greater than min*/->h(min+1, max-1)/*Then divert, with the arguments changed*/}}
->-> // If we didn't divert earlier, divert to wherever the stitch was called from
C (compiled with VC++ (Visual Studio 2017)) 264bytes
#include "stdafx.h"
void f(int l,int u){for(int t=1,h=0,H=0,i=0;i<u-l+1;i++,t^=1)printf("%d ",t?l+h++:u-H++);}void main(){f(1,5);}
choosing other Compiler may eliminate the Need for the include but vs2017 won't let me ommit it w/o Errors. the idea is to just use variables to Keep track of how many numbers have to be added to the Minimum or have to be subtracted from the maximum (2seperate variables) another variable is used to Keep track of from where the value has to be taken from front or from end. Setting it to 1 means lowest bit is set xor-ing it with 1 makes it toggle. i run a loop with the amount of iterations it takes to solve the Problem. inside the printf i use the Array Bounds and my variables to generate the number needed and increment my helper variables all at once. using the ternary Operator exp?then:else saves a few Bytes as it replaces a if-else Statement. further saving come through use of multiple variables defined in the for loop instead of only one (typically i) and putting the xor in the loop Header removes the Need for curly braces.
every Thing else i did is ommit spaces and linebreaks where possible and only use 1 character variable names and omitting ()s where ever possible
C (gcc), 54 53 52 bytes
- Saved
a bytetwo bytes thanks to Rogem.
s=1;f(x,y){printf("%d ",s*x),s=x-y?f(-y,~x,s=-s):1;}
C (gcc), 46 bytes
A recursive function, call as f(low, high). As an extra 0-byte bonus, if you call f(high, low) the opposite arrangement is produced.
f(x,y){printf("%i ",x);x-y&&f(y,x+1-2*(x>y));}
Degolf
f(x,y)
{
printf("%i ",x); // Print the value of x
x-y&& // If x-y == 0, the && operator shorts and the
// recursion ends.
f(y,x+1-2*(x>y)); // Adjust x by 1 towards y, and
// recurse with x and y swapped.
}
APL+WIN, bytes 17, 13, 26
4 bytes saved thank to Adám plus 13 bytes see Jonah's comment
(⍴m)↑∊m,¨⌽m←(1↓m)+0,⍳-/m←⎕
Try it online! Courtesy of Dyalog Classic
Explanation:
m←(1↓m)+0,⍳-/m←⎕ Prompts high end of range followed by low end and generates vector
∊m,¨⌽m Reverse the vector, pair elements from both vectors and flatten.
(⍴m)↑ Select the elements to the length of the original vector
tinylisp, 48 bytes
(d f(q((A B)(c A(i(e A B)()(f B((i(l A B)a s)A 1
Explanation
The code defines a recursive function f that takes two arguments A and B, which are initially the lower and upper bounds of the range.
- Base case: if
A = B, return a list containing the single elementA - Recursive case: prepend
Ato the result of a recursive call:- If
A < B, recurse with argumentsBandA+1 - If
A > B, recurse with argumentsBandA-1
- If
For example, with initial arguments 1 and 5:
Value Next call
1 5, 2
5 2, 4
2 4, 3
4 3, 3
3 Return
with a resulting list of (1 5 2 4 3).
Somewhat ungolfed:
(load lib/utilities)
(def f
(lambda (A B)
(cons A
(if (equal? A B)
nil
(f B
((if (less? A B) add2 sub2)
A
1))))))
Brachylog, 15 bytes
⟦₂{∅|b↔↰T&hg,T}
Input is a 2-element list [lo, hi]. Note that underscore is used for negative numbers. Try it online!
Explanation
⟦₂ 2-argument inclusive range: [1,5] -> [1,2,3,4,5]
{ } Call this recursive predicate to calculate the output:
∅ Base case: the input is empty list; nothing to do
| Otherwise (recursive case): [1,2,3,4,5]
b Behead the input list [2,3,4,5]
↔ Reverse [5,4,3,2]
↰ Call the predicate recursively [5,2,4,3]
T Label the result T
& Also, with the input list,
h Take the head 1
g Wrap it in a list [1]
,T Append T from earlier [1,5,2,4,3]
VBA (Excel), 62 bytes
Using immediate window and [a1] and [a2] as input.
a=[a1]:b=[a2]:For x=0To(b-a)/2:?a+x &IIf(b-x=a+x,"",b-x);:Next
Clojure, 62 bytes
#(distinct(let[r(range %1(inc %2))](interleave r(reverse r)))
Wow Clojure is a terrible golfing language. Still fun though.
JavaScript, 35 bytes
f=(a,b,s=1)=>a-b?a+[,f(b,a+s,-s)]:a
Thanks to Arnauld, 1 byte saved.
Wolfram Language (Mathematica), 56 54 bytes
This is my first time golfing!
f[a_,b_]:=(c=a~Range~b;Drop[c~Riffle~Reverse@c,a-b-1])
Saved 2 bytes using infix notation.
Explanation:
f[a_,b_]:= \function of two variables
c=a~Range~b; \list of integers from a to b
Reverse@c \same list in reverse
c~Riffle~Reverse@c \interleave the two lists
Drop[c~Riffle~Reverse@c,a-b-1] \drop last |a-b-1| elements (note a-b-1 < 0)
Alternatively, we could use Take[...,b-a+1] for the same result.
Tests:
f[4, 6]
f[1, 5]
f[-1, 1]
f[-1, 2]
Ouput:
{4, 6, 5}
{1, 5, 2, 4, 3}
{-1, 1, 0}
{-1, 2, 0, 1}
Shortcuts for iOS, 20 Actions, 4 KB
Shortcuts is a visual scripting language, so the code is linked as a screenshot and download.
Screenshot (very tall) / Shortcut iCloud link
How it works:
getVariable (input.s)
if Equals (input.s)
This tests if input.s exists. input.s will be nothing if it does not exist. In shortcuts, nothing does not equal itself.
otherwise
There is no way to test for not equal in shortcuts other than using the else side of an if statement
dictionary {a:input.a, b:input.b, c:1}
setVariable (input)
To set a single key in a dictionary, you need to get the variable, set dictionary value, and re-set the variable. Creating a new dictionary here saves one action but takes more bytes. I am optimizing for actions here.
getVariable (input.a)
if Equals (input.b)
getVariable(input.a)
otherwise
If both a and b are the same, this range only has one value. Values passed to otherwise are returned by End, similar to how ternary operators work in many languages.
number 0
calculate - (input.s)
getVariable (input.a)
calculate + (input.s)
No set variable is required because instead, magic variables are used to get the values from the calculate actions
dictionary{a: input.b, b:(Calculation Result), c: (Calculation Result)}
The two calculation results refer to different actions. The first to the a+s, and the second to the 0-s.
runShortcut ch-arrangement
Run this shortcut again with the new input and return the result.
To run, pass a dictionary containing a and b to the shortcut. Output is the list as a string (comma seperated). Screenshot / Link
There is no online interpreter for this. You need an iOS device and the Shortcuts app.
Logic from @ovs's javascript answer
JVM bytecode (OpenJDK asmtools JASM), 449 bytes
enum b{const #1=Method java/io/PrintStream.print:(I)V;static Method a:(II)V stack 2 locals 4{getstatic java/lang/System.out:"Ljava/io/PrintStream;";astore 3;ldc 0;istore 2;l:iload 2;ldc 1;if_icmpeq t;aload 3;iload 0;invokevirtual #1;iinc 0,1;iinc 2,1;goto c;t:aload 3;iload 1;invokevirtual #1;iinc 1,-1;iinc 2,-1;c:aload 3;ldc 32;i2c;invokevirtual java/io/PrintStream.print:(C)V;iload 0;iload 1;if_icmpne l;aload 3;iload 0;invokevirtual #1;return;}}
Ungolfed (and slightly cleaner)
enum b {
public static Method "a":(II)V stack 5 locals 4 {
getstatic "java/lang/System"."out":"Ljava/io/PrintStream;";
astore 3;
ldc 0;
istore 2;
loop:
iload 2;
ldc 1;
if_icmpeq true;
false:
aload 3;
iload 0;
invokevirtual "java/io/PrintStream"."print":"(I)V";
iinc 0,1;
iinc 2,1;
goto cond;
true:
aload 3;
iload 1;
invokevirtual "java/io/PrintStream"."print":"(I)V";
iinc 1,-1;
iinc 2,-1;
goto cond;
cond:
iload 0;
iload 1;
if_icmpne loop;
aload 3;
iload 0;
invokevirtual "java/io/PrintStream"."print":"(I)V";
return;
}
}
Standalone function, needs to be called from Java as b.a(num1,num2).
Explanation
This code uses the method parameters as variables, as well as a boolean in local #3 deciding which number to output. Each loop iteration either the left or right is output, and that number is incremented for the left or decremented for the right. Loop continues until both numbers are equal, then that number is output.
...I have a distinct feeling I'm massively outgunned on the byte count
PHP, 93 92 81 bytes
Standalone program, 81 bytes:
for($a=range($argv[1],$argv[2]);$a;)echo$x++%2?array_pop($a):array_shift($a),' ';
As a function, 92 bytes:
function($l,$h){for($a=range($l,$h);$a;)$b[]=$x++%2?array_pop($a):array_shift($a);return$b;}
function ( $l, $h ) {
for( $a = range( $l, $h ); $a; ) {
$b[] = $x++ % 2 ? array_pop( $a ) : array_shift( $a );
}
return $b;
}
Well, I thought it was a worth-trying idea. Perhaps in a more succinct language, this would work better. The Golf is not strong with this one.
Pyth, 10 8 bytes
{.iF_B}F
Explanation
{.iF_B}F
}FQ Generate the range between the (implicit) inputs.
.iF_B Interleave it with its reverse.
{ Deduplicate.
F# (.NET Core), 69 67 bytes
Translation of @nimi's answer.
fun a b->let rec h=function|a::b->[a]@h(List.rev b)|n->n in h[a..b]
Cubix, 16 bytes
;w(.II>sO-?@;)^/
Cubified
; 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.
C# (Visual C# Interactive Compiler), 46 bytes
a=>b=>{for(;a<=b;Write(a+(b>a++?b--+"":"")));}
Saved 4 bytes thanks to dana!
C# (Visual C# Interactive Compiler), 65 bytes
void z(int a,int b){if(a<=b){Write(a+(b>a?b+"":""));z(a+1,b-1);}}
Ruby, 37 36 33 bytes
f=->a,b{a>b ?[]:[a,b]|f[a+1,b-1]}
Recursive version with 3 bytes saved by G B.
Ruby, 38 bytes
->a,b{d=*c=a..b;c.map{d.reverse!.pop}}
Non-recursive version.
Haskell, 30 bytes
l%h=l:take(h-l)(h:(l+1)%(h-1))
Usage: 3%7 gives `[3,7,4,6,5]
For the inputs l, h the function calls recursively with the inputs l+1, h-1, and adds l,h to the beggining.
Instead of any halting condition, the code uses take(h-l) to shorten the sequence to the right length (which would otherwise be an infinite sequence of increasing and decreasing numbers).
05AB1E, 6 bytes
ŸDvć,R
Explanation
Ÿ # push range [min ... max]
D # duplicate
v # for each element in the copy
ć, # extract and print the head of the original list
R # and then reverse it
Perl 6, 32 bytes
{flat($_ Z [R,] $_)[^*/2]}o&[..]
Not too complicated, but there's a couple of tricks that help make the program shorter
Explanation:
{ }o&[..] # Convert the two inputs to a range
$_ Z [R,] $_ # Zip the range with its reverse
flat( ) # Flatten
[^*/2] # And take the first half of the elements
Charcoal, 17 bytes
≔…·NNθWθ«≔⮌θθ⟦I⊟θ
Try it online! Link is to verbose version of code. Explanation:
≔…·NNθ
Create an inclusive range between the two inputs.
Wθ«
Repeat until it is empty.
≔⮌θθ
Reverse it.
⟦I⊟θ
Remove the last element and print it on its own line.
R, 38 37 36 bytes
function(a,b)rbind(a:b,b:a)[a:b-a+1]
- -1 byte thanks to @user2390246
- -1 byte thanks to @Kirill L.
Exploiting the fact that R stores matrices column-wise
Gol><>, 22 bytes
IIT}:nP}:nM2K(?t2K=?h;
Fixed!!! It had a major bug where it would sometimes append a zero when not necessary, but no longer!
Forth (gforth), 52 bytes
: f 2dup - 1+ 0 do dup . i 2 mod 2* 1- - swap loop ;
Explanation
Loop from 0 to (End - Start). Place End and Start on top of the stack.
Each Iteration:
- Output the current number
- Add (or subtract) 1 from the current number
- Switch the current number with the other number
Code Explanation
: f \ start new word definition
2dup - \ get the size of the range (total number of integers)
1+ 0 \ add 1 to the size because forth loops are [Inclusive, Exclusive)
do \ start counted loop from 0 to size+1
dup . \ output the current top of the stack
i 2 mod \ get the index of the loop modulus 2
2* 1- \ convert from 0,1 to -1,1
- \ subtract result from top of stack (adds 1 to lower bound and subtracts 1 from upper)
swap \ swap the top two stack numbers
loop \ end the counted loop
; \ end the word definition
C#(Visual C# Interactive Compiler), 62 bytes
void a(int i,int j){Write((j-i)%2==0?i++:j--);if(i<=j)a(i,j);}
cQuents, 19 bytes
#|B-A+1&A+k-1,B-k+1
Note that it does not work on TIO right now because TIO's interpreter is not up to date.
Explanation
#|B-A+1&A+k-1,B-k+1
A is the first input, B is the second input
#|B-A+1 n = B - A + 1
& Print the first n terms of the sequence
k starts at 1 and increments whenever we return to the first term
A+k-1, Terms alternate between A + k - 1 and
B-k+1 B - k + 1
increment k
R, 65 64 61 60 bytes
-1 byte thanks to Robert S.
-4 more thanks to digEmAll
x=scan();z=x:x[2];while(sum(z|1)){cat(z[1],"");z=rev(z[-1])}
MATL, 8 bytes
&:t"1&)P
Explanation
&: % Take two inputs (implicit). Two-input range
t % Duplicate
" % For each
1&) % Push first element, then an array with the rest
P % Reverse array
% End (implicit). Display (implicit)
R, 51 bytes
function(x,y,z=x:y)matrix(c(z,rev(z)),2,,T)[seq(z)]
Explanation:
For a sequence x:y of length N, create a two-by-N matrix consisting of the sequence x:y in the top row and y:x in the bottom row matrix(c(z,rev(z)),2,,T). If we select the first N elements of the matrix [seq(z)], they will be chosen by column, giving the required output.
PowerShell, 59 48 bytes
param($a,$b)(($z=0..($b-$a))|%{$a+$_;$b-$_})[$z]
(Seems long...)
Takes input $a and $b, constructs the range 0 .. ($b-$a), stores that into $z, then loops through that range. The looping through that range is just used as a counter to ensure we get enough iterations. Each iteration, we put $a and $b on the pipeline with addition/subtraction. That gives us something like 1,5,2,4,3,3,4,2,5,1 so we need to slice into that from 0 up to the $b-$a (i.e., the count) of the original array so we're only left with the appropriate elements. That's left on the pipeline and output is implicit.
-11 bytes thanks to mazzy.
Japt, 7 bytes
Takes input as an array.
rõ
ÊÆÔv
:Implicit input of array U=[low,high]
r :Reduce by
õ : Inclusive, reversed range (giving the range [high,low])
\n :Reassign to U
Ê :Length
Æ :Map the range [0,Ê)
Ô : Reverse U
v : Remove the first element
C# (.NET Core), 66 bytes
a=>b=>{var s="";for(;b>=a;)s+=a+" "+(b==a++?"":b--+" ");return s;}