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


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.

  • \$\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


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!

  • 2
    \$\begingroup\$ n-k-1 is n+~k. \$\endgroup\$ – Mr. Xcoder Aug 7 '17 at 8:20

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.