feat: function page_count and page_item_count finished

This commit is contained in:
Sandro Zimmermann 2026-03-09 20:14:59 +01:00
parent fbf13b3f3f
commit 7e43f0d42d

View File

@ -15,12 +15,22 @@ class PaginationHelper:
# returns the number of pages # returns the number of pages
def page_count(self): def page_count(self):
return self.items_per_page // len(self.collection) return int(len(self.collection) / self.items_per_page) + (
len(self.collection) % self.items_per_page > 0
)
# returns the number of items on the given page. page_index is zero based # returns the number of items on the given page. page_index is zero based
# this method should return -1 for page_index values that are out of range # this method should return -1 for page_index values that are out of range
def page_item_count(self, page_index): def page_item_count(self, page_index):
pass if page_index < 0 or self.page_count() - 1 < page_index:
return -1
if page_index == 0:
return len(self.collection[: self.items_per_page])
return self.collection[
self.items_per_page * page_index : self.items_per_page * page_index * 2
]
# determines what page an item at the given index is on. Zero based indexes. # determines what page an item at the given index is on. Zero based indexes.
# this method should return -1 for item_index values that are out of range # this method should return -1 for item_index values that are out of range
@ -28,7 +38,8 @@ class PaginationHelper:
pass pass
collection = ["a", "b", "c", "d", "e", "f"] collection = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
helper = PaginationHelper(collection, 4) helper = PaginationHelper(collection, 4)
print(helper.page_count()) print(helper.page_count())
print(helper.page_item_count(2))