← 返回 coinbase 的题目列表Pagination with Next and Previous
类型:online_judge
Implement a pagination system that supports forward and backward pagination. The function paginate(data, page_size, current_page, direction) should accept a list of data, page size, current page number, and direction ('forward' or 'backward') as parameters and return the segment of data for the next page.
Input
data: A list of any data type
page_size: An integer specifying the number of items per page
current_page: An integer, the current page number (starts from 1)
direction: A string, either 'forward' or 'backward', indicating the pagination direction
Output
Return the data segment for the specified page and direction. If out of bounds, return an empty list.
Example
paginate(['a', 'b', 'c', 'd', 'e', 'f'], 2, 1, 'forward')
# Output ['c', 'd']
paginate(['a', 'b', 'c', 'd', 'e', 'f'], 2, 2, 'backward')
# Output ['a', 'b']
paginate(['a', 'b', 'c', 'd'], 3, 2, 'forward')
# Output []
Assume pages will never contain more than a million items.
Example
Input
['a', 'b', 'c', 'd', 'e', 'f']
2
1
'forward'