Hone logo
Hone
Problems

Implementing Pagination for a Large Dataset

Pagination is a crucial technique for displaying large datasets in a user-friendly manner. Instead of overwhelming users with thousands of items at once, pagination breaks the data into smaller, manageable pages. This challenge asks you to implement a pagination function in Python that efficiently divides a list into pages based on a specified page number and page size.

Problem Description

You are tasked with creating a Python function called paginate that takes a list of items and divides it into pages. The function should accept three arguments:

  • data: A list of items to be paginated. This list can contain any type of data (numbers, strings, objects, etc.).
  • page_number: An integer representing the page number to retrieve (starting from 1).
  • page_size: An integer representing the number of items to display on each page.

The function should return a sublist containing only the items belonging to the requested page. If the requested page number is out of range (e.g., greater than the total number of pages or less than 1), the function should return an empty list.

Key Requirements:

  • The function must handle edge cases gracefully, such as empty input lists or invalid page numbers.
  • The page numbering should start from 1, not 0.
  • The function should be efficient, especially when dealing with large datasets.
  • The returned sublist should contain the correct items for the specified page.

Expected Behavior:

The function should calculate the starting and ending indices for the requested page based on the page_number and page_size. It should then return a slice of the input list corresponding to those indices.

Edge Cases to Consider:

  • Empty input list (data is empty).
  • page_number is less than 1.
  • page_number is greater than the total number of pages.
  • page_size is zero or negative.
  • page_size is larger than the total number of items.

Examples

Example 1:

Input: data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], page_number = 2, page_size = 3
Output: [4, 5, 6]
Explanation: The total number of pages is 10 // 3 = 3. Page 2 starts at index (2-1) * 3 = 3 and ends at index 2 * 3 = 6. Therefore, the items at indices 3, 4, and 5 (which are 4, 5, and 6) are returned.

Example 2:

Input: data = [1, 2, 3, 4, 5], page_number = 1, page_size = 2
Output: [1, 2]
Explanation: The total number of pages is 5 // 2 = 2. Page 1 starts at index (1-1) * 2 = 0 and ends at index 1 * 2 = 2. Therefore, the items at indices 0 and 1 (which are 1 and 2) are returned.

Example 3:

Input: data = [1, 2, 3], page_number = 4, page_size = 2
Output: []
Explanation: The total number of pages is 3 // 2 = 1. Since page_number (4) is greater than the total number of pages (1), an empty list is returned.

Constraints

  • data will be a list of any data type.
  • page_number will be an integer.
  • page_size will be an integer.
  • 1 <= len(data) <= 10000
  • 1 <= page_size <= 100
  • 1 <= page_number <= 1000 (though the actual number of pages will be limited by the length of data and page_size)
  • The function should execute in O(1) time complexity (excluding the time to create the slice).

Notes

Consider using list slicing to efficiently extract the desired page. Think about how to calculate the start and end indices for the slice based on the page_number and page_size. Remember to handle edge cases where the requested page is out of bounds. Calculating the total number of pages is helpful for determining if the requested page is valid.

Loading editor...
python