Print a booklet

39

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.

tisaconundrum

Posted 2017-08-06T23:38:58.020

Reputation: 1 097

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 :) – Shaggy – 2017-08-06T23:51:47.507

8

Welcome to PPCG and nice first challenge! Note that we recommend proposing new challenges in the sandbox before posting them.

– Oliver Ni – 2017-08-06T23:57:39.137

1Your input needs to be a multiple of 4 – tisaconundrum – 2017-08-06T23:57:59.430

Can I have a trailing comma in the output? – TheLethalCoder – 2017-08-07T10:14:34.853

1Would be nice (but maybe trivial) to support any value, filling with blank pages if needed (another challenge, maybe?) – Barranka – 2017-08-07T14:56:20.370

1Can we delimit the array with a space, hyphen, or other delimiter instead of a comma? – TehPers – 2017-08-07T20:01:57.223

Can we create a function to generate this sequence, or does the final result need to print to standard output? That is, are we allowed to create a function instead so that when you call the function and print its output, it will show the correct sequence? I see a lot of answers doing that already (including one of my answers), but it is not explicitly said we can in the objectives. – rayryeng - Reinstate Monica – 2017-08-08T13:06:16.930

Smallest byte wins and method to getting there can be used, as long as the output generates the correct numbers, whether delimited by spaces, commas, hyphens, or parenthesis. – tisaconundrum – 2017-08-08T20:40:39.130

Answers

8

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

Emigna

Posted 2017-08-06T23:38:58.020

Reputation: 50 798

13

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

Arnauld

Posted 2017-08-06T23:38:58.020

Reputation: 111 334

47 bytes: f=(n,a=1)=>n<a+3?[]:[n,a,a+1,n-1,...f(n-2,a+2)] – Rick Hitchcock – 2017-08-07T13:32:56.523

1@RickHitchcock n<a is actually enough, so that's 4 bytes saved. Thanks! – Arnauld – 2017-08-07T13:51:33.537

6

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

LyricLy

Posted 2017-08-06T23:38:58.020

Reputation: 3 313

Save bytes by using 1 space instead of 4. – Oliver Ni – 2017-08-07T00:20:11.840

Oh yeah, I always forget about that. Thanks. – LyricLy – 2017-08-07T00:21:02.287

1-29 bytes using a lambda (although this might be different enough to warrant a separate answer). – notjagan – 2017-08-07T00:37:24.063

@notjagan Go ahead and post that yourself if you want. – LyricLy – 2017-08-07T00:45:35.530

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,. – Arnold Palmer – 2017-08-07T02:09:03.133

You could move the whole for loop into one line. for i in range(1,f/2,2):print f-i+1,i,i+1,f-i, – nedla2004 – 2017-08-07T02:38:37.237

55 bytes, replace print f-i+1, with print-~f-i, in order to remove the whitespace – Mr. Xcoder – 2017-08-07T05:18:55.887

What I have done is no wizardry :)), let me explain. I used the bitwise operator ~. ~i is equivalent to -i-1. Now, I only negated ~i, such that I had -~i, which means -(-i-1) = i+1. The point is that you cannot remove the space in there unless you have a bracket or an operator, and the only appropriate prefix operator, in this case is ~, which lets you remove the space. – Mr. Xcoder – 2017-08-07T06:25:55.720

6

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!

Luis Mendo

Posted 2017-08-06T23:38:58.020

Reputation: 87 464

-5 bytes. – notjagan – 2017-08-07T01:26:33.693

1You can use r*1 instead of r[:] for -1 byte` – ovs – 2017-08-07T05:42:22.030

6

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.

xnor

Posted 2017-08-06T23:38:58.020

Reputation: 115 687

5

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

Luis Mendo

Posted 2017-08-06T23:38:58.020

Reputation: 87 464

5

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]

Jonathan Allan

Posted 2017-08-06T23:38:58.020

Reputation: 67 804

This is clever. +1 – Erik the Outgolfer – 2017-08-07T10:14:37.887

4

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!

rayryeng - Reinstate Monica

Posted 2017-08-06T23:38:58.020

Reputation: 1 521

1

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: https://www.gnu.org/software/octave/doc/v4.0.3/Anonymous-Functions.html#Anonymous-Functions

– Michthan – 2017-08-07T13:10:13.757

1@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! – rayryeng - Reinstate Monica – 2017-08-07T14:50:34.983

3

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!

djhurio

Posted 2017-08-06T23:38:58.020

Reputation: 1 113

1nice, way better than my (stolen) solution – Giuseppe – 2017-08-07T21:34:13.263

148 bytes! – Giuseppe – 2017-08-07T21:44:46.963

1The trick was noticing that (1:n)[order(1:n%%2)] is the same as order(1:n%%2) – Giuseppe – 2017-08-07T21:45:13.113

2

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

JungHwan Min

Posted 2017-08-06T23:38:58.020

Reputation: 13 290

2

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!

notjagan

Posted 2017-08-06T23:38:58.020

Reputation: 4 011

2n-i-1 can be n+~i – ovs – 2017-08-07T05:40:08.183

2

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

Kevin Cruijssen

Posted 2017-08-06T23:38:58.020

Reputation: 67 575

2

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]

nimi

Posted 2017-08-06T23:38:58.020

Reputation: 34 639

1

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!

Giuseppe

Posted 2017-08-06T23:38:58.020

Reputation: 21 077

First time someone's ever used an answer of mine as inspiration. Thanks :) – rayryeng - Reinstate Monica – 2017-08-07T16:12:20.077

1

It was hard to beat you, but I managed this with 55 byte answer (https://codegolf.stackexchange.com/a/138045/13849).

– djhurio – 2017-08-07T21:35:27.297

1

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

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

Try it online!

Xcali

Posted 2017-08-06T23:38:58.020

Reputation: 7 671

Managed to find a slightly different approach, but ended up with the exact same byte count! Try it online!

– Dom Hastings – 2017-08-07T12:04:04.270

1

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

Mr. Xcoder

Posted 2017-08-06T23:38:58.020

Reputation: 39 774

1

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

steenbergh

Posted 2017-08-06T23:38:58.020

Reputation: 7 772

1

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

ninjalj

Posted 2017-08-06T23:38:58.020

Reputation: 3 018

1

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!

rayryeng - Reinstate Monica

Posted 2017-08-06T23:38:58.020

Reputation: 1 521

1

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

Stephen

Posted 2017-08-06T23:38:58.020

Reputation: 12 293

0

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)

Arnold Palmer

Posted 2017-08-06T23:38:58.020

Reputation: 443

I found some improvements, and decided they deserved their own answer. – Mr. Xcoder – 2017-08-07T05:58:35.523

By the way, replace FN with V for -1 byte

– Mr. Xcoder – 2017-08-07T05:59:55.287

0

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.

Mr. Xcoder

Posted 2017-08-06T23:38:58.020

Reputation: 39 774

0

Ruby, 40 bytes

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

Try it online!

G B

Posted 2017-08-06T23:38:58.020

Reputation: 11 099

0

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

Hand-E-Food

Posted 2017-08-06T23:38:58.020

Reputation: 7 912

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! – Ian H. – 2017-08-07T07:16:51.877

@IanH. When using a lambda the trailing semi colon can be omitted. – TheLethalCoder – 2017-08-07T10:04:54.100

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. – TheLethalCoder – 2017-08-07T10:09:40.600

2

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.

– TheLethalCoder – 2017-08-07T10:16:12.173

0

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!

Laikoni

Posted 2017-08-06T23:38:58.020

Reputation: 23 676

0

Python 2, 50 bytes

A port of Arnauld's JS answer.

f=lambda n,k=1:n>k and[n,k,k+1,n-1]+f(n-2,k+2)or[]

Try it online!

totallyhuman

Posted 2017-08-06T23:38:58.020

Reputation: 15 378

2n-k-1 is n+~k. – Mr. Xcoder – 2017-08-07T08:20:57.323

0

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

Karl Napf

Posted 2017-08-06T23:38:58.020

Reputation: 4 131

0

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!

Renzo

Posted 2017-08-06T23:38:58.020

Reputation: 2 260

0

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

eniallator

Posted 2017-08-06T23:38:58.020

Reputation: 343

0

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.

Titus

Posted 2017-08-06T23:38:58.020

Reputation: 13 814

0

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

miles

Posted 2017-08-06T23:38:58.020

Reputation: 15 654