Azhar Uddin Sheikh
Azhar Uddin Sheikh

Reputation: 671

How to create a pagination function with a given list

I have to create a pagination function asking for 2 arguments, an array and a page_number.

I was trying to solve for any no of item's in a list I am stuck into some problem.

Here is my code:

def paginator(array,page_number):
    if 0 < page_number < len(array):
        obj_per_page = len(array)//page_number
        objects = len(array)
        count = 0
        start = 0
        while objects > 1:
            count += 1
            objects -= len(array)//page_number
            object_available = array[start:obj_per_page]
            print(f'this is page {count} have object {object_available}')
            start = obj_per_page 
            obj_per_page += len(array)//page_number
    
    elif len(array) <= page_number:
        for i in array:
            print(f'thi is page {i} have object [{i}]')
 
    else:
        print(array)
 
 
list = [1,2,3,4,5,6]
page_number = 3
 
paginator(list,page_number)

output

this is page 1 have object [1, 2]
this is page 2 have object [3, 4]
this is page 3 have object [5, 6]

`this code provide expected output only when

if len(list) is divisible by page_number if page_number is 0 or 1 if page_number is greater and equal to len(list)`

Bugs

let's say len(list) is 9 and page_number are either of (2,4,5,6,7,8) the remainder of 9 % (2,4,5,6,7,8) is (1,1,4,3,2,1) ,so the number of item's are missing equal to remainder

what i want

if len(list) is 9 and page_number is 4 and I want output something like

this is page 1 have object [1, 2, 3]
this is page 2 have object [4, 5]
this is page 3 have object [6, 7]
this is page 4 have object [8, 9]

Help me if you guys have other solution share it I try to explain my problem sorry for bad grammer

Upvotes: 2

Views: 3494

Answers (2)

arshovon
arshovon

Reputation: 13651

I have updated the array indices to append reminder objects at first page.

def paginator(objects, total_pages):
    total_objects = len(objects)
    if 0 < total_pages < total_objects:
        obj_per_page = total_objects // total_pages
        reminder_objects = total_objects - (obj_per_page * total_pages)
        first_page = True
        i = 0
        page_number = 1
        while total_objects > 0:
            current_page_total_objects = obj_per_page
            if first_page and reminder_objects > 0:
                current_page_total_objects = obj_per_page + reminder_objects
            print(f'this is page {page_number} have object {objects[i:i+current_page_total_objects]}')
            page_number += 1
            first_page = False
            i += current_page_total_objects
            total_objects -= current_page_total_objects

    elif total_objects <= total_pages:
        for i in objects:
            print(f'this is page {i} have object [{i}]')
    else:
        print(objects)
 
 
ar = [1,2,3,4,5,6,7,8,9]
page_number = 4

Output:

this is page 1 have object [1, 2, 3]
this is page 2 have object [4, 5]
this is page 3 have object [6, 7]
this is page 4 have object [8, 9]

N.B.: I have renamed few variables to be more meaningful.

Upvotes: 2

Andrej Kesely
Andrej Kesely

Reputation: 195408

def paginator(array, page_number):
    n, remainder = page_number, len(array) % page_number
    curr, idx, old_idx = 1, 0, 0
    while n + remainder != 0:
        idx += len(array) // page_number + bool(remainder)
        print(f"this is page {curr} have object {array[old_idx:idx]}")
        n -= 1
        remainder = max(remainder - 1, 0)
        curr += 1
        old_idx = idx


lst = list(range(1, 10))
page_number = 4

paginator(lst, page_number)

Prints:

this is page 1 have object [1, 2, 3]
this is page 2 have object [4, 5]
this is page 3 have object [6, 7]
this is page 4 have object [8, 9]

Upvotes: 2

Related Questions