'How to implement List/Array pagination with a pivot (different starting points) and that is circular in Python?

I'm not sure how to start this question and also what does it mean that it is circular?



Solution 1:[1]

Circular means that when the pagination reaches the end of the list it loops back to the start again.

To give an example, dblist contains a list of IDs and you need to write a function called getPage which returns a certain page. The function takes arguments pivotID which is the first ID of the first page, pageSize which is the number of IDs in each page, and the pageNum which is the page you want to return. When the end of the list is reached it should loop back to the beginning of the list and continue until reaching the pivotID before stopping. I found the easiest way to do this was to rewrite dblist with the pivotID as the first element in the list.

dblist = [13, 5, 6, 54, 82, 39, 11, 69, 96, 32, 48, 18, 45, 77, 99]

def getPage(dbList, pivotID, pageSize, pageNum):
    startIndex = dbList.index(pivotID)
    dbList = dbList[startIndex:] + dbList[:startIndex]

    startPos = pageNum * pageSize
    endPos = startPos + pageSize

    if endPos < len(dbList):
        return dbList[startPos:endPos]
    elif startPos < len(dbList):
        return dbList[startPos:]
    else:
        return []

Choose some parameters for this function and print out the first 10 pages

print("PageNum Page")
for i in range(10):
    print("{}\t{}".format(i, getPage(dblist, 69, 2, i)))

PageNum Page
0   [69, 96]
1   [32, 48]
2   [18, 45]
3   [77, 99]
4   [13, 5]
5   [6, 54]
6   [82, 39]
7   [11]
8   []
9   []

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1