I have written a small function to generate an array of page numbers for displaying a pagination. The inputs to the function is an active page number p, and the max page number. The function will return an array of 5 possible page numbers available near the active page.
Example input, output:
pages(1,10): [1,2,3,4,5]
pages(10,10): [6,7,8,9,10]
pages(5,10): [3,4,5,6,7]
The function is:
def pages(p,max):
a=[p]
while len(a)<5 and not (a[0]==1 and a[-1]==max):
if a[0]>1:
a.insert(0,a[0]-1)
if a[-1]<max:
a.append(a[-1]+1)
return a
Is there anything to do to improve this code, or is there any other simpler methods to achieve this?