This is a follow-up of: Rearrange page numbers for printing a book
- Total pages of a book:
- Pages per booklet:
int(divisible by 4 evenly)
Rearrange the pages in each booklet to follow a specific format, [n, f, f+1, n-1], where n is the last page of the booklet, and f is the first.
Print a list/tuple of the rearranged integers
If pages is not a multiple of 4 the last booklet (list that get processed by
arrange_booklet()) always has errors at the end.
int pages will generally be a large number (50~500). I wrote the program because my printer software doesn't allow me to just print the pages in this order. I have to manually type every integer, comma-separated. Which would obviously get tedious very quickly.
from math import ceil # f is the first page (smallest input integer), n is the last (largest) def arrange_booklet(f, n): '''Creates a list by using the page # order formula.''' _ = list() while n > f: _.extend([n, f, f+1, n-1]) f += 2 n -= 2 return _ def make_book(): '''Combines all the lists into one and returns the result.''' pages = list(range(1, int(input("Enter a number of pages: "))+1)) while True: len_booklet = int(input( "Enter booklet length, must be a multiple of four: ")) if len_booklet % 4 == 0: break book = list() # permanent list, to be returned _ = list() # temporary list, to populate permanent list for page in pages: _.append(page) if page % len_booklet == 0: _ = arrange_booklet(_, _[-1]) book.extend(_) _.clear() # the last booklet, if pages % 4 != 0, is still in the temp list and broke # out of the loop it has to be handled separately below, or it will never # reach the book if _: _ = arrange_booklet(_, _[-1]) book.extend(_) print(book) if __name__ == '__main__': make_book()