# Interleave numbers from 1 to n, with the same numbers reversed

### A simple one:

Take a positive integer n less than 1000, and output the integers from 1 to n interleaved with the integers from n to 1. You must concatenate the numbers so that they appear without any delimiters between them.

### Test cases:

n = 1
11

n = 4
14233241

n = 26
12622532442352262172081991810171116121513141413151216111710189198207216225234243252261

n = 100
110029939849759669579489399210911190128913881487158616851784188319822081218022792378247725762675277428732972307131703269336834673566366537643863396240614160425943584457455646554754485349525051515052495348544755465645574458435942604161406239633864376536663567346833693270317130722973287427752676257724782379228021812082198318841785168615871488138912901191109299389479569659749839921001


This is so the shortest submission in bytes in each language wins. Explanations are encouraged.

# Vyxal, 5 4 bytes

ɾḂYṅ


Try it Online!

### Explained

ɾḂYṅ
ɾ     # range of [1,input+1]
Ḃ    # dupe and reverse
Y  # interleave
ṅ # join with no spaces

• :Ṙ can be substituted into Ḃ, saving one byte. Commented Oct 12, 2022 at 10:19

# tinylisp 2, 46 32 bytes

(\(N)(} ,(m u({ ,(z(1to N)(to1 N


An anonymous function that takes an integer and returns a string. Try it at Replit. Example session:

tl2> (\(N)(} ,(m u({ ,(z(1to N)(to1 N
(() (N) (} , (m u ({ , (z (1to N) (to1 N))))))
tl2> (def interleave-numbers _)
interleave-numbers
tl2> (interleave-numbers 4)
"14233241"


### Explanation

(\(N)(} ,(m u({ ,(z(1to N)(to1 N))))))
(\                                   ) ; Lambda function
(N)                                  ; that takes an integer N:
(z              )     ;  Zip together these two lists:
(1to N)             ;   Numbers from 1 to N
(to1 N)      ;   Numbers from N to 1
({ ,                 )    ;  Right fold on list/string concatenation
(m u                      )   ;  Map unparse (convert to string) to each number
(} ,                           )  ;  Left fold on list/string concatenation


The reason for the different folds is that right fold is more efficient, but by default it starts with an empty list, which when concatenating strings would make the result a list of charcodes instead of a string. Left-folding strings, by contrast, results in a string.

# Actually, 9 bytes

R;R@Z♂iεj


Explanation:

R;R@Z♂iεj
R          range(1, n+1)
;R        duplicate and reverse
@Z      swap and zip
♂i    make 1D
εj  join with empty string


# Mathematica, 36 32 bytes

Row@Riffle[x=Range@#,Reverse@x]&


thanks Martin

• #+1-x is more efficient than Reverse@x. ------- Note: Row does not even make it a string, just a formatting. It looks identical to a string on some platforms, but it show as multiplication sign between on Wolfram sandbox. However it takes less bytes than ToString/@(""<>...) or ""<>ToString/@.... Commented Jun 10, 2017 at 15:32

# CJam, 13 12 bytes

1 byte removed thanks to Martin Ender

ri:X{_)X@-}%


Try it online!

### Explanation

ri               e# Read integer n
:X             e# Copy n into variable X
{     }%    e# Map over the array [0 1 2 ... n-1]
_          e# Duplicate
X        e# Push n
@       e# Rotate
-      e# Subtract


## Perl 5, 28 bytes

27 bytes code + 1 byte for -a.

map{print$_,"@F"+1-$_}1..$_  Try it online! # AWK, 34 30 bytes {for(i=1;i<=$1;i++)printf i$1-i+1} {for(;i<$1;i++)printf i+1$1-i}  Try it online! • could shave 4 with {for(;i<$1;i++)printf i+1$1-i} Commented May 30, 2017 at 13:50 # Jelly, 6 bytes RżU$FV


Try it online!

Explanation:

         Example: n = 6
R        Create a range from 1 to n             [1, 2, 3, 4, 5, 6]
U$and a reversed copy of that same range [6, 5, 4, 3, 2, 1] ż and interleave them [ [1, 6], [2, 5, ... ] F Flatten the list [1, 6, 2, 5, 3, 4 ...] V And Eval: Jelly code consisting of only numbers would simply print those numbers,  # Retina, 20 bytes .+$*__
\B
$.$.'
_



Try it online! Includes test cases. Explanation: Each pair of numbers sums to n+1, so we convert n to unary and add 1. Then, we match between each pair of _s, counting the number of _s to the left and right. This generates the pairs of numbers. Finally we delete the _s now that they've served their purpose.

## QBIC, 18 bytes

[:|Z=Z+!a$+!b-a+1$


Explanation

[:|       FOR a = 1 to n
Z=Z+      Add to Z$!b$+      a cast of the loop counter as string
!b-a+1$and a cast of (n+1) minus the loop counter to string Z$ is printed implicitly at the end of QBIC


# Perl 5, 25 21 + 1 = 26 22 bytes

Takes input from stdin, without a trailing newline.

Runs with the -n flag:

print++$x.$_--while$_  4 bytes saved thanks to Dada. • It doesn't work for numbers greater than 9. That's because the precedence of . is higher than the one of +. Adding parenthesis around $_+1 would solve this. Also, you can remove the parenthesis around $_--. But even shorter, you can take the input without final newline (with echo -n for instance), and then you can do print++$x.$_--while$_.
Commented Jun 1, 2017 at 8:01

# ><>, 16 12+3 = 19 15 bytes

l:n}:n1-:?!;


Input is expected on the stack, so +3 bytes for the -v flag.

Thanks to @TealPelican for pointing out a very clever way to save 4 bytes by using the size of the stack itself - on the first iteration it'll be 1, then 2, then 3... That way, the first number in each pair manages itself, no manual incrementing required!

Previous version:

1:n1+$:n1-:?!;$!


Try it online!

• Surprisingly I came up with something fairly similar before checking the answers for ><> but you can save a few bytes by replacing the; 1+$with } and then removing the final;$! at the end to come up with this; l:n}:n1-:?!; Commented Jun 2, 2017 at 15:25
• ln:n1-::?!;
– Jo King
Commented Nov 6, 2018 at 21:40

# Powershell, 37 bytes

param($n)-join(1..$n|%{$_,($n-$_+1)})  # C (gcc), 43 bytes a;f(n){for(a=1;n;a++)printf("%d%d",a,n--);}  Try it online! • You can save a byte by putting the ++ on the a in the printf. Commented Jun 10, 2017 at 15:00 # Python 3, 51 bytes lambda n:''.join("%d%d"%(x+1,n-x)for x in range(n))  Try it online! Port of Mego's Python 2 answer. # C# (.NET Core), 53 bytes a=>{for(int i=0;++i<=a;)Console.Write(i+""+(a-i+1));}  Try it online! Uses an Action delegate to pull in the input and not require a return. Ungolfed: a => { for(int i = 0; ++i <= a;) // initialize i and increment until a Console.Write(i + "" + (a - i + 1)); // output i and "inverse" of i // empty string required to set as string // parentheses required because C# is dumb and can't figure out what a minus sign is supposed to do without them }  # Japt -mP, 8 6 bytes °Us+N´  Try it  :For each U in the range [0,input) °U : Prefix increment U s : Convert to a string + : Append N´ : Postfix decrement the (singleton) array of inputs :Implicitly join and output  # Japt-P, 11 8 bytes Saved 2 byte thanks to @Shaggy õ í1õU)c  Try it online! ## Explanation: õ í1õU)c õ Range [1...Input] í Pair with: 1õU Range [Input...1] )c Flatten -P Join into a string  • Use the -P flag to save a byte. Link Commented Jun 6, 2017 at 16:11 • @Shaggy Thanks! When I wrote it, I tried this and assumed it wasn't joining due to a bug. Commented Jun 6, 2017 at 16:26 • 8 bytes Commented Nov 6, 2018 at 17:48 # Kotlin, 52 50 bytes fun g(e:Int)=(1..e).forEach{print("$it${e-it+1}")}  Try it online Older version, the one above also prints it, the one below produces a string. fun y(n:Int)=(1..n).joinToString(""){"$it${n-it+1}"}  Try it online! # APL (Dyalog Unicode), 10 bytesSBCS (∊⍪,⌽)⍕¨∘⍳  Try it online! Explanation: (∊⍪,⌽)⍕¨∘⍳ ⍝ Monadic function train ⍕¨∘⍳ ⍝ Generate integers from 1 to input, convert each to string ⍝ Call this vector "X" ⍪ ⍝ "Table" X, making a single-column matrix ,⌽ ⍝ Reverse X and concatenate it with the above table ⍝ This results in a two-column matrix with integers from ⍝ 1 to N and N to 1 side-by-side ∊ ⍝ "Enlist" the above - left-to-right, top-to-bottom, ⍝ recursively concatenate all items in the matrix  • Don't forget to claim your bounty! – Adám Commented Mar 9, 2019 at 21:05 • here's a shorter one: ∊⍕¨⌽,⌸⌽⍳⎕ – ngn Commented Mar 14, 2019 at 12:00 # Zsh, 32 bytes repeat$1 printf $[++i]$[$1+1-i]  Try it online! I like trying to beat Bash+coreutils with pure zsh, but sadly that is not the case here. Cooler, but one byte longer (33 bytes): a=({$1..1})
<<<${(j::)${(n)a}:^a}


Try it online!

# Braingolf v0.7, 18 bytes

VR.MUvU&,R{vMR}>&_


Try it online!

### Explanation:

VR.MUvU&,R{vMR}>&_  Implicit input of n to stack
.M                Duplicate n and move duplicate to new stack
U               Replace stack with 1-n, where n is last item on stack
vU             Replace 2nd stack with 1-n
&,           Reverse second stack
{   }     Map loop, runs for each item in the stack
vMR      Move to next stack, move last item to main stack
>    Cleanup after loop
&_  Print all items in stack with no delimiter


# Pyt, 5 bytes

řĐ↔Žǰ


Try it online!

ř          implicit input (n); push [1,2,...,n]
Đ         duplicate top of stack
↔        flip the array at the top of the stack
Ž       interleave the two arrays
ǰ      join the contents of the array to create a string; implicit print


# Ly, 12 bytes

Rsrp[ul,su]


Try it online!

This one generates the 1..N series on the stack, and it generates the N..1 numbers by manipulating an accumulator on the backup cell.

R             - Generate range of 0..N (where N is from STDIN)
s            - Save the upper limit to the backup cell
rp          - Reverse the stack, delete the "0"
[      ]  - While the stack isn't empty
u        - Print next number in the range
l,s     - Load backup cell, decrement and save
$$$$u   - Increment and print


# Go, 90 bytes

import."fmt"
func f(n int)(o string){for i:=1;i<=n;i++{o+=Sprintf("%d%d",i,n-i+1)};return}


Attempt This Online!

# J, 18 bytes

[:;],.&<&":&>:&i.-


Try it online!

# Julia 1.0, 33 22 bytes

~x=join([1:x x:-1:1]')


Try it online!

1. The forward and reverse sequences are collected from ranges.
2. This method of interleaving arrays in Julia can be adapted for code golf.
3. The print command is vectorized with the "dot" operator so that no delimiters are shown.
• -9 bytes thanks to MarcMush: avoid reshaping the array
• -2 bytes thanks to MarcMush: replace print. with join
• I think ~x=print.([1:x x:-1:1]') works Commented Jan 18, 2023 at 14:06
• You can also use join instead of print.() Commented Jan 18, 2023 at 14:08

# Pip, 10 bytes

-2 bytes thanks to DLosc!

Y\,ayWVRVy


Try It Online!

# Pip, 12 bytes

(Y++,a)WVRVy


How?

(Y++,a)WVRVy  : One arg(integer n)
a        : First arg
,         : Inclusive range from 1
++          : Increment
Y            : Yank
RV   : Alias for unary reverse
y  : Yanked range
WV     : Weave two iterables together, alternating their items


Try It Online!

• 10 bytes Commented Jan 19, 2023 at 18:32

## Clojure, 52 bytes

#(apply str(mapcat(fn[i][(inc i)(- % i)])(range %)))


Concatenates pair-wise integers (head and tail of the seq).

Python 2.7, 39 bytes

x=''.join(i+1+n-ifor i in range(n))

• I think you must either use input` or make this a lambda. Assuming the variable exists in the workspace is not acceptable I'm afraid. Commented May 30, 2017 at 15:45