Book Pagination in Python

Suppose we have a list of strings called book, if we page an index (0-indexed) into the book, and page_size, we have to find the list of words on that page. If the page is out of index then simply return an empty list.

So, if the input is like book = ["hello", "world", "programming", "language", "python", "c++", "java"] page = 1 page_size = 3, then the output will be ['language', 'python', 'c++']

To solve this, we will follow these steps −

  • l:= page*page_size

  • return elements of book from index l to l+page_size - 1

Let us see the following implementation to get better understanding −


 Live Demo

class Solution:
   def solve(self, book, page, page_size):
      return book[l:l+page_size]
ob = Solution()
book = ["hello", "world", "programming", "language", "python", "c++",
page = 1
page_size = 3
print(ob.solve(book, page, page_size))


["hello", "world", "programming", "language", "python", "c++", "java"], 1, 3


['language', 'python', 'c++']

Updated on: 02-Sep-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started