39
\$\begingroup\$

Reading a book is easy, but printing a book can be a bit tricky. When printing a booklet, the printer needs to have the pages arranged in a certain manner in order to be read from left to right. The way this is done is using a pattern like below

n, 1, 2, n-1, n-2, 3, 4, n-3, n-4, 5, 6, n-5, n-6, 7, 8, n-7, n-8, 9, 10, n-9, n-10, 11, 12, n-11…

Test Cases

4 page booklet: 4, 1, 2, 3

8 page booklet: 8,1,2,7,6,3,4,5

12 page booklet: 12,1,2,11,10,3,4,9,8,5,6,7

16 page booklet: 16,1,2,15,14,3,4,13,12,5,6,11,10,7,8,9

20 page booklet: 20,1,2,19,18,3,4,17,16,5,6,15,14,7,8,13,12,9,10,11

Task

Your task is to, given an integer n that is a multiple of 4, display an array of numbers that could be used to print a book of n pages.

Note: As long as the output generates the correct numbers, whether delimited by spaces, commas, hyphens, or parenthesis, any method to getting to a solution can be used

This is a question so answers will be scored in bytes, with the fewest bytes winning.

\$\endgroup\$
  • \$\begingroup\$ Are we guaranteed that input will always be divisible by 4 or even an even number? Either way, could you add a few more test cases, please? And welcome to PPCG :) \$\endgroup\$ – Shaggy Aug 6 '17 at 23:51
  • 8
    \$\begingroup\$ Welcome to PPCG and nice first challenge! Note that we recommend proposing new challenges in the sandbox before posting them. \$\endgroup\$ – Oliver Ni Aug 6 '17 at 23:57
  • 1
    \$\begingroup\$ Your input needs to be a multiple of 4 \$\endgroup\$ – tisaconundrum Aug 6 '17 at 23:57
  • 1
    \$\begingroup\$ Would be nice (but maybe trivial) to support any value, filling with blank pages if needed (another challenge, maybe?) \$\endgroup\$ – Barranka Aug 7 '17 at 14:56
  • 1
    \$\begingroup\$ Can we delimit the array with a space, hyphen, or other delimiter instead of a comma? \$\endgroup\$ – TehPers Aug 7 '17 at 20:01

31 Answers 31

8
\$\begingroup\$

05AB1E, 9 8 7 bytes

L`[Žˆrˆ

Try it online!

Explanation

L           # push range [1 ... input]
 `          # split as separate to stack
  [Ž        # loop until stack is empty
    ˆ       # add top of stack to global list
     r      # reverse stack
      ˆ     # add top of stack to global list
            # implicitly display global list
\$\endgroup\$
13
\$\begingroup\$

JavaScript (ES6), 49 45 bytes

Saved 4 bytes with help from @RickHitchcock

f=(n,k=1)=>n<k?[]:[n,k,k+1,n-1,...f(n-2,k+2)]

Demo

f=(n,k=1)=>n<k?[]:[n,k,k+1,n-1,...f(n-2,k+2)]

console.log(JSON.stringify(f(4)))
console.log(JSON.stringify(f(8)))
console.log(JSON.stringify(f(12)))
console.log(JSON.stringify(f(16)))
console.log(JSON.stringify(f(20)))


Non-recursive, 51 bytes

n=>[...Array(n)].map((_,i)=>[2*n-i,,++i][i&2]+1>>1)

Demo

let f =

n=>[...Array(n)].map((_,i)=>[2*n-i,,++i][i&2]+1>>1)

console.log(JSON.stringify(f(4)))
console.log(JSON.stringify(f(8)))
console.log(JSON.stringify(f(12)))
console.log(JSON.stringify(f(16)))
console.log(JSON.stringify(f(20)))

\$\endgroup\$
  • \$\begingroup\$ 47 bytes: f=(n,a=1)=>n<a+3?[]:[n,a,a+1,n-1,...f(n-2,a+2)] \$\endgroup\$ – Rick Hitchcock Aug 7 '17 at 13:32
  • 1
    \$\begingroup\$ @RickHitchcock n<a is actually enough, so that's 4 bytes saved. Thanks! \$\endgroup\$ – Arnauld Aug 7 '17 at 13:51
6
\$\begingroup\$

Python 2, 99 93 88 58 56 55 bytes

f=input()
for i in range(1,f/2,2):print-~f-i,i,i+1,f-i,

Try it online!

-6 bytes by removing unneeded indentation, thanks Oliver Ni

-5 bytes by changing the conditional, thanks Luis Mendo

-30 bytes by optimizing the print statements, thanks Arnold Palmer

-2 bytes by putting the loop on one line, thanks nedla2004

-1 byte by doing some wizardry, thanks Mr. Xcoder

\$\endgroup\$
  • \$\begingroup\$ Save bytes by using 1 space instead of 4. \$\endgroup\$ – Oliver Ni Aug 7 '17 at 0:20
  • \$\begingroup\$ Oh yeah, I always forget about that. Thanks. \$\endgroup\$ – LyricLy Aug 7 '17 at 0:21
  • 1
    \$\begingroup\$ -29 bytes using a lambda (although this might be different enough to warrant a separate answer). \$\endgroup\$ – notjagan Aug 7 '17 at 0:37
  • \$\begingroup\$ @notjagan Go ahead and post that yourself if you want. \$\endgroup\$ – LyricLy Aug 7 '17 at 0:45
  • \$\begingroup\$ 58 bytes by changing your print up just a bit. It now prints f-i+1,i,i+1,f-i in each loop instead of conditionally printing the last value. This also allowed removing the initial print f,. \$\endgroup\$ – Arnold Palmer Aug 7 '17 at 2:09
6
\$\begingroup\$

Python 2, 46 bytes

lambda n:map(range(1,n+1).pop,n/4*[-1,0,0,-1])

Try it online!

Generates the range [1..n] and pops from the front and back in the repeating pattern back, front, front, back, ...


Python 2, 49 bytes

f=lambda n,k=1:n/k*[0]and[n,k,k+1,n-1]+f(n-2,k+2)

Try it online!

Generates the first 4 elements, then recursively continues with the upper value n decreased by 2 and the lower value k increased by 2.


Python 2, 49 bytes

lambda n:[[n-i/2,i/2+1][-i%4/2]for i in range(n)]

Try it online!

Directly generates the i'th value of the list, using -i%4/2 as a Boolean for whether to take the lower or higher value.

\$\endgroup\$
6
\$\begingroup\$

Python 3, 68 63 62 bytes

−5 bytes thanks to @notjagan (removing spaces and using [*...] instead of list()).

−1 byte thanks to @ovs (*1 instead of [:]).

def f(n):r=[*range(1,n+1)];return[r.pop(k%4//2-1)for k in r*1]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ -5 bytes. \$\endgroup\$ – notjagan Aug 7 '17 at 1:26
  • 1
    \$\begingroup\$ You can use r*1 instead of r[:] for -1 byte` \$\endgroup\$ – ovs Aug 7 '17 at 5:42
5
\$\begingroup\$

MATL, 19 17 10 bytes

:t"0&)@o?P

Try it online!

Explanation

:          % Implicitly input n. Push range [1 2 ... n]
t          % Duplicate
"          % For each (that is, do n times)
  0&)      %   Push last element, and then subarray with remaining elements
  @        %   Push 1-based iteration index
  o?       %   Is it odd? If so
    P      %     Reverse subarray of remaining elements
           %   Implicit end
           % Implicit end
           % Implicitly display stack
\$\endgroup\$
5
\$\begingroup\$

Jelly,  12  11 bytes

Improved to 11 bytes, "Combinatorial Methods":

9Bṁ×ḶṚÆ¡‘Œ?

Try it online!

How?

This uses permutation calculations and the factorial number system:

9Bṁ×ḶṚÆ¡‘Œ? - Link n                        e.g. 16
9B          - nine in binary                     [1,0,0,1]
  ṁ         - mould like n                       [1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1]
    Ḷ       - lowered range(n)                   [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
   ×        - multiply                           [0,0,0,3,4,0,0,7,8,0,0,11,12,0,0,15]
     Ṛ      - reverse                            [15,0,0,12,11,0,0,8,7,0,0,4,3,0,0,0]
      Æ¡    - convert from factorial base        19621302981954 (=15*15!+12*12!+...+3*3!)
        ‘   - increment                          19621302981955 (we actually wanted 1*0! too)
         Œ? - shortest permutation of natural numbers [1,2,...] that would reside at that
            -   index in a sorted list of all permutations of those same numbers
            -                                    [16,1,2,15,14,3,4,13,12,5,6,11,10,7,8,9]

Unimproved 12 byter, "Knitting Patterns":

RṚ‘żRs2Z€FḊṁ

Try it online!

How?

This is the simple approach, it creates two strands, interleaves them and then trims the loose ends:

RṚ‘żRs2Z€FḊṁ - Link: n                      e.g. 8
R            - range(n)                          [1,2,3,4,5,6,7,8]
 Ṛ           - reverse                           [8,7,6,5,4,3,2,1]
  ‘          - increment                         [9,8,7,6,5,4,3,2]
    R        - range(n)                          [1,2,3,4,5,6,7,8]
   ż         - zip (interleave)                  [[9,1],[8,2],[7,3],[6,4],[5,5],[4,6],[3,7],[2,8]]
     s2      - split into chunks of length 2     [[[9,1],[8,2]],[[7,3],[6,4]],[[5,5],[4,6]],[[3,7],[2,8]]]
       Z€    - transpose €ach (cross-stitch?!)   [[[9,8],[1,2]],[[7,6],[3,4]],[[5,4],[5,6]],[[3,2],[7,8]]]
         F   - flatten                           [9,8,1,2,7,6,3,4,5,4,5,6,3,2,7,8]
          Ḋ  - dequeue (removes excess start)    [8,1,2,7,6,3,4,5,4,5,6,3,2,7,8]
           ṁ - mould like n (removes excess end) [8,1,2,7,6,3,4,5]
\$\endgroup\$
  • \$\begingroup\$ This is clever. +1 \$\endgroup\$ – Erik the Outgolfer Aug 7 '17 at 10:14
4
\$\begingroup\$

Octave, 43 36 bytes

A port of this answer in C (gcc) can be found here.

@(n)[n-(k=1:2:n/2)+1;k;k+1;n-k](:)';

Explanation

  1. k=1:2:n/2: Generates a linear sequence from 1 to n/2 in steps of 2. Note that this is immediately used in the next step.
  2. [n-k+1;k;k+1;n-k]: Creates a 4 row matrix such that the first row creates the sequence n, n-2, n-4... down to n-(n/2)+2, the second row is 1, 3, 5... up to n/2 - 1, the third row is the second row added by 1 and the fourth row is the first row added by 1.
  3. [n-k+1;k;k+1;n-k](:)': This stacks all of the columns of this matrix together from left to right to make a single column vector, and we transpose it to a row vector for easy display. Stacking the columns together this way precisely creates the sequence desired.

Note that this is an anonymous function, so you can assign it to a variable prior to using it, or you can use the built-in ans variable that gets created after creating the function.

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Hi, I think you can even shorten it by making it a anonymous function, so you don't have to call input. See this link: gnu.org/software/octave/doc/v4.0.3/… \$\endgroup\$ – Michthan Aug 7 '17 at 13:10
  • 1
    \$\begingroup\$ @Michthan True. I originally did it that way because the code was more than one statement. I took another crack at it so remove the call to input and I abused the syntax a bit more by storing the base incremental vector as I was creating the first row and taking the input n from the actual anonymous function input itself so I can now fit it into one statement. Thanks! \$\endgroup\$ – rayryeng - Reinstate Monica Aug 7 '17 at 14:50
3
\$\begingroup\$

R, 48 bytes (improved)

Thanks to @Giuseppe for -7 bytes!

n=scan();(x=order(1:n%%2))[order(-(n/2+.5-x)^2)]

The trick is that x=1:n;x[order(x%%2)] is equivalent to order(1:n%%2).

Try it online!

R, 55 bytes (original)

Golfed

n=scan();x=1:n;x=x[order(x%%2)];x[order(-(n/2+.5-x)^2)]

Ungolfed with comments

Read n from stdin.

n=scan()

Define x as sequence of pages from 1 to n.

x=1:n

Order pages so even pages are before uneven pages.

x=x[order(x%%2)]

Order pages in descending order with respect to the centre of the book computed by n/2+.5.

x[order(-(n/2+.5-x)^2)]

Example with 8 pages:

  • centre is 4.5;
  • pages 1 and 8 are the most distant from the centre, but 8 comes first because 8 is even;
  • pages 2 and 7 are the next most distant from the centre, but 2 comes first as 2 is even;
  • and so on.

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ nice, way better than my (stolen) solution \$\endgroup\$ – Giuseppe Aug 7 '17 at 21:34
  • 1
    \$\begingroup\$ 48 bytes! \$\endgroup\$ – Giuseppe Aug 7 '17 at 21:44
  • 1
    \$\begingroup\$ The trick was noticing that (1:n)[order(1:n%%2)] is the same as order(1:n%%2) \$\endgroup\$ – Giuseppe Aug 7 '17 at 21:45
2
\$\begingroup\$

Mathematica, 54 53 45 bytes

Join@@Range[#][[(-1)^k{k,-k}]]~Table~{k,#/2}&

Explanation

Join@@Range[#][[(-1)^k{k,-k}]]~Table~{k,#/2}&  (* Input: # *)
                              ~Table~{k,#/2}   (* Iterate from k=1 to #/2 *)
      Range[#][[            ]]                 (* From {1..#}, take... *)
                      {k,-k}                   (* k-th and negative k-th element *)
                                               (* negative k-th = k-th from the end *)
                (-1)^k                         (* Reversed for odd k *)
Join@@                                         (* Join the result *)
\$\endgroup\$
2
\$\begingroup\$

Python 2, 64 63 bytes

-1 byte thanks to ovs!

lambda n:sum([[i,i+1,n-i,n+~i]for i in range(1,n/2,2)],[n])[:n]

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ n-i-1 can be n+~i \$\endgroup\$ – ovs Aug 7 '17 at 5:40
2
\$\begingroup\$

Haskell, 42 bytes

n#a|n<a=[]|x<-n-2=n:a:a+1:n-1:x#(a+2)
(#1)

Try it online!

One byte longer:

Haskell, 43 bytes

f n=[1,3..div n 2]>>= \x->[n-x+1,x,x+1,n-x]
\$\endgroup\$
2
\$\begingroup\$

Java 8, 84 72 bytes

n->{for(int j=0;++j<n;System.out.printf("%d,%d,%d,%d,",n--,j++,j,n--));}

or

n->{for(int j=0;++j<n;System.out.print(n--+","+j+++","+j+","+n--+","));}

-12 bytes thanks to @TheLethalCoder's comment on the C# answer.

Old answer (84 bytes):

n->{int r[]=new int[n],i=1,N=n,J=1;for(r[0]=n;i<n;r[i]=-~i++%4<2?J++:--N);return r;}

Explanation:

Try it here.

n->{                  // Method with integer parameter and no return-type
  for(int j=0;++j<n;  //  Loop from 1 to `n` (exclusive)
    System.out.printf("%d,%d,%d,%d,",n--,j++,j,n--)
                      //   Print four numbers simultaneously
  );                  //  End of loop
}                     // End of method
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 47 + 1 (-n) = 48 bytes

$,=$";print$_--,$i+++1,$i+++1,$_--,''while$_>$i

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Managed to find a slightly different approach, but ended up with the exact same byte count! Try it online! \$\endgroup\$ – Dom Hastings Aug 7 '17 at 12:04
1
\$\begingroup\$

Swift 3, 74 bytes

func g(f:Int){for i in stride(from:1,to:f/2,by:2){print(f-i+1,i,i+1,f-i)}}

Try it online!

Swift 3, 60 bytes

{f in stride(from:1,to:f/2,by:2).map{(f-$0+1,$0,$0+1,f-$0)}}

For some reason, this does not work in any online environment I have tried so far. If you want to test it, put var g= in front of it, and call it with print(g(12)) in Xcode (Playgrounds).

Here is a picture after I've ran it in an Xcode playground, version 8.3.1 (Running Swift 3.1):

enter image description here

\$\endgroup\$
1
\$\begingroup\$

QBIC, 25 bytes

[1,:/2,2|?b-a+1,a,1+a,b-a

Although the input is %4, the actual rhythm is 2-based.

Explanation

[1,:/2,2|   FOR ( b=1; b <= <input>/2; b=b+2)               
?           PRINT
 b-a+1,     n
 a,         1
 1+a,       2
 b-a        n-1
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 66 bytes

A port of my Octave answer to C (gcc):

f(n,i){for(i=1;i<n/2;i+=2)printf("%d %d %d %d ",n-i+1,i,i+1,n-i);}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

cQuents, 21 bytes

=n::n-z+1,z+1,x-1,z-1

Try it online!

Explanation

                            Implicit input n
=n                          First item in the sequence is n
  ::                        Mode :: (Sequence 2): print sequence from 1 to n
                            Comma delimited items are rotated through
    n-z+1,                    n - previous + 1
          z+1,                previous + 1
              x-1,            third-previous - 1
                  z-1         previous - 1
\$\endgroup\$
1
\$\begingroup\$

R, 64 60 bytes

Devastatingly outgolfed by djhurio! His answer is quite elegant, go upvote it.

n=scan();matrix(c(n-(k=seq(1,n/2,2))+1,k,k+1,n-k),4,,T)[1:n]

A port of rayryeng's Octave answer.

Try it online!

original solution (64 bytes):

f=function(n,l=1:n)`if`(n,c(l[i<-c(n,1,2,n-1)],f(n-4,l[-i])),{})

Recursive function.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Bash + Perl + Groff + Psutils, 48 bytes

perl -nE'say".bp
"x--$_'|groff|psbook>/dev/null

Shows output on stderr. Output contains some trailing garbage.

Example of use:

$ echo 20 | perl -nE'say".bp
> "x--$_'|groff|psbook>/dev/null
[20] [1] [2] [19] [18] [3] [4] [17] [16] [5] [6] [15] [14] [7] [8] [13] [12] 
[9] [10] [11] Wrote 20 pages, 4787 bytes
\$\endgroup\$
0
\$\begingroup\$

Pyth, 21 20 bytes

sm[hK-QddhdK):1/Q2 2

Test Suite.

If outputting as a nested list is allowed:

Pyth, 20 19 bytes

m[hK-QddhdK):1/Q2 2

Test Suite.


Explanation

sm[hK-QddhdK):1/Q2 2  - Full program.

 m           :1/Q2 2  - Map over range(1,input()/2,2) with a variable d.
  [         )         - Construct a list with:
   hK-Qd                - Input - d + 1,
        d               - d,
         hd             - d + 1 and
           K            - Input - d.
s                     - Flattens the list and prints implicitly.
\$\endgroup\$
0
\$\begingroup\$

Ruby, 40 bytes

->n{1.step(n/2,2){|x|p n-x+1,x,x+1,n-x}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C#, 107 bytes

int[]F(int p){var a=new int[p];for(int i=0,q=1;q<p;a[i++]=p--){a[i++]=p--;a[i++]=q++;a[i++]=q++;}return a;}

Keep two counters, one starting at 1, one at p. In each loop iteration, write four elements and just increment or decrement counters after each entry. When the counters meet in the middle, stop.

int[] F(int p)
{
    var a = new int[p];
    for(int i = 0, q = 1; q < p; a[i++] = p--)
    {
        a[i++] = p--;
        a[i++] = q++;
        a[i++] = q++;
    }
    return a;
}
\$\endgroup\$
  • \$\begingroup\$ You can save a few bytes by placing the method in a delegate. Your code would then look like this: p=>{var a=new int[p];for(int i=0,q=1;q<p;a[i++]=p--){a[i++]=p--;a[i++]=q++;a[i++]=q++;}return a;};, with the System.Func<int, int[]> f = not having the included into the bytecount. Also you can add a link to TIO, which is very useful when trying to allow people to try your code out by themselves! \$\endgroup\$ – Ian H. Aug 7 '17 at 7:16
  • \$\begingroup\$ @IanH. When using a lambda the trailing semi colon can be omitted. \$\endgroup\$ – TheLethalCoder Aug 7 '17 at 10:04
  • \$\begingroup\$ Initialise q to 0 and pre increment at q<p -> ++q<p and then remove the second post increment to save a byte. Move the two trailing loop statements into the last stage of the for loop so you can remove the curly braces. \$\endgroup\$ – TheLethalCoder Aug 7 '17 at 10:09
  • 2
    \$\begingroup\$ If a trailing comma is allowed the following works for 71 bytes p=>{for(int q=0;++q<p;)System.Console.Write(p--+$",{q++},{q},{p--},");}. TIO. \$\endgroup\$ – TheLethalCoder Aug 7 '17 at 10:16
0
\$\begingroup\$

Haskell, 58 bytes

(x:y:r)#(a:b:s)=x:y:a:b:r#s
f n=take n$n:[1..]#[n-1,n-2..]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth, 27 24 23 bytes

-3 bytes by printing throughout instead of at the end.

-1 Thanks to Mr. Xcoder

V:1/Q2 2pjd[-QtNNhN-QNk

Try it online!

Or on the online Compiler/Executor

This is my first real program in Pyth, so there's probably better methods that I don't know about.

Explanation

V:1/Q2 2pjd[-QtNNhN-QNk
V:1/Q2 2                   # For N in range(1, Q/2, 2):
        pjd                # print " ".join(...),
           [-QtNNhN-QNk    # The list [n - (N-1), N, N + 1, n - N, ""] (n is input)
\$\endgroup\$
0
\$\begingroup\$

C++ (gcc), 89 84 68 bytes

As unnamed generic lambda. n is #pages (%4==0) and C is a reference parameter for the result, an empty container like vector<int> (only push_back is needed).

[](int n,auto&C){for(int i=0,j=0;i<n;C.push_back(++j%4<2?n--:++i));}

previous solution:

#define P C.push_back(
[](int n,auto&C){for(int i=0;i<n;P n--),P++i),P++i),P n--));}

Try it online!

Slightly ungolfed:

auto f=
[](int n,auto&C){
 for(int i=0,j=0;
     i<n;
     C.push_back(++j%4<2 ? n-- : ++i));
}

previous solution slightly ungolfed:

auto f=
[](int n, auto&C){
 for(
  int i=0;
  i<n;
   P n--),
   P++i),
   P++i),
   P n--)
 );
}
;

It was quite straightforward developed and there are sure some minor optimizations in the arithmetics.

  • Edit1: unification of the arithmetics saved 5 byte
  • Edit2: after the unification the 4 steps were combined

Usage:

std::vector<int> result;
f(n, result);

Print-Variant, 77 bytes out-dated

If you insist on printing the values, there is this solution:

[](int n,auto&o){for(int i=0;i<n;o<<n--<<' '<<++i<<' '<<++i<<' '<<n--<<' ');}

Where o is your desired std::ostream, like std::cout

Usage (if 2nd lambda was assigned to g):

g(n, std::cout);
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 79 bytes

(defun f(n &optional(k 1))(and(> n k)`(,n,k,(1+ k),(1- n),@(f(- n 2)(+ k 2)))))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Lua, 94 bytes

For this challenge I actually came up with 2 different methods that are both 94 bytes.

Method 1:

function f(n,i)i=i or 1 return n>i and('%s,%s,%s,%s,%s'):format(n,i,i+1,n-1,f(n-2,i+2))or''end

Commented Code:

function f(n,i)
  i=i or 1
  -- On the first iteration i will be nil so I'm setting it's value to 1 if it is.

  return n>i and ('%s,%s,%s,%s,%s'):format(n,i,i+1,n-1,f(n-2,i+2)) or ''
  -- Here i return a ternary statement
  -- If n>i is true, it will return a string using string.format() and part of this is recursion
  -- If it's false, it will just return an empty string
end

Method 2:

function f(n,i)i=i or 1 return n>i and n..','..i..','..i+1 ..','..n-1 ..','..f(n-2,i+2)or''end

This method is similar to the first method however I'm instead returning a concatenated string instead of string.format()

In both methods I have used the concept of n and i getting closer together

\$\endgroup\$
0
\$\begingroup\$

PHP, 51+1 bytes

while($i<$k=&$argn)echo$k--,_,++$i,_,++$i,_,$k--,_;

prints page numbers separated by underscore with a trailing delimiter.
Run as pipe with -nR or try it online.

\$\endgroup\$
0
\$\begingroup\$

J, 22 bytes

($,)_2|.`]\1+],@,.&i.-

Try it online!

Explanation

($,)_2|.`]\1+],@,.&i.-  Input: integer n
             ]          Identity
                     -  Negate
                  &i.   Form the ranges [0, 1, ..., n-1] and [n-1, ..., 1, 0]
                ,.      Interleave
              ,@        Flatten
           1+           Add 1
    _2    \             For each non-overlapping sublist of size 2
        `                 Cycle between these two operations
      |.                    Reverse for the first, third, ...
         ]                  Identity for the second, fourth, ...
  ,                     Flatten
 $                      Reshape to length n
\$\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.