function : divmod() Function to combine the result of divisor and remainder operation , Returns a tuple containing quotient and remainder (a//b, a%b)
grammar :divmod(a, b)
give an example 1. Calculate slave parameters N Start , Every number and parameter decreasing downward A The result value of the divisor and remainder of . The code is as follows :
def getDivmod(A, N):
while N >= 0:
sn = str(N)
sa = str(A)
result = ['divmod({0}, {1}):'.format(sn, sa), str(divmod(N, A))]
print(''.join(result))
N = N -1
call getDivmod() Function and pass in parameter data , The code is as follows :
getDivmod(3, 10)
The operation results are as follows :
divmod(10, 3):(3, 1)
divmod(9, 3):(3, 0)
divmod(8, 3):(2, 2)
divmod(7, 3):(2, 1)
divmod(6, 3):(2, 0)
divmod(5, 3):(1, 2)
divmod(4, 3):(1, 1)
divmod(3, 3):(1, 0)
divmod(2, 3):(0, 2)
divmod(1, 3):(0, 1)
divmod(0, 3):(0, 0)
2. Simulation Implementation of data paging calculation method . The code is as follows :
def getSegment(curIndex, getSize):
abc = ['a','b', 'c', 'd', 'e', 'f', 'g','h','i', 'j', 'k', 'l', 'm','n','o', 'p', 'q', 'r', 's', 't','u','v', 'w', 'x', 'y', 'z']
startGet = (curIndex-1)*getSize
getData = abc[startGet:curIndex*getSize]
totalIndexTuple = divmod(len(abc), getSize)
totalIndex = totalIndexTuple[0] + (1 if totalIndexTuple[1] > 0 else 0)
return (getData, totalIndex)
call getSegment() Function and pass the current page number and the total number of entries per page , Return the data result and the total number of pages , The code is as follows :
result = getSegment(2, 5)
print(result[0])
print(result[1])
Output results :
['f', 'g', 'h', 'i', 'j']
6