alexwatever
alexwatever

Reputation: 594

Calculating pages in PDF based on product count

I'm creating a web app that converts a html product catalogue into a PDF. I'm trying to calculate the number of pages based on the quantity of products, passing the function the product total and returning a integer for the page count.

The product count changes on different pages due to banners being displayed.

To give a more visual example of the catalogues page counts:

1 page: [8]  
2 pages: [8][12]  
3 pages: [8][16][12]  
4 pages: [8][14][14][12]  
5 pages: [8][14][14][16][12]  
6 pages: [8][14][14][14][14][12]  

I just can't wrap my head around this, my math and algorithm skills are not up to scratch. If anyone can shed some light on the direction I should head, I'd really appreciate it.

Upvotes: 1

Views: 112

Answers (1)

Abid Hasan
Abid Hasan

Reputation: 658

Logically, you would place the first page first, then last page, then check for an odd number total and then add middles.

def ass(num):
    #Check for negative number
    if num <= 0: return
    #This list will be returned, start with 8, since it always has 8
    ret = [8]

    #If only on number, then return
    if num == 1: return ret
    #add 14s in the middle; even need total-2, odd number needs total-3
    to_add = num - 2 if num % 2 == 0 else num - 3

    for i in range(to_add):
        ret.append(14)

    #check for second to last if odd, we need 16 appended
    if num % 2 != 0:
        ret.append(16)
    #Final 12
    ret.append(12)

    return ret

Upvotes: 2

Related Questions