假设我们有一个名为book的字符串列表,如果我们在书中分页索引(0索引),并且分页为page_size,则必须在该页面上找到单词列表。如果页面索引不足,则只需返回一个空列表。
因此,如果输入就像book = [“ hello”,“ world”,“ programming”,“ language”,“ python”,“ c ++”,“ java”] page = 1 page_size = 3,则输出将是['language','python','c ++']
为了解决这个问题,我们将遵循以下步骤-
l:=页面* page_size
将书的元素从索引l返回到l + page_size-1
让我们看下面的实现以更好地理解-
class Solution: def solve(self, book, page, page_size): l=page*page_size return book[l:l+page_size] ob = Solution()book = ["hello", "world", "programming", "language", "python", "c++", "java"] page = 1 page_size = 3 print(ob.solve(book, page, page_size))
["hello", "world", "programming", "language", "python", "c++", "java"], 1, 3
输出结果
['language', 'python', 'c++']