From 25e8e7cfb540fe4973f2d3a9c64a501cfd4e52e7 Mon Sep 17 00:00:00 2001 From: Thomas Hollenstein Date: Fri, 6 Mar 2026 14:48:47 +0100 Subject: [PATCH] Aufgabe 3 Codewars --- README.me | 1 + src/codewars/pagination_helper.py | 42 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/codewars/pagination_helper.py diff --git a/README.me b/README.me index c4d5c58..040eee6 100644 --- a/README.me +++ b/README.me @@ -1,3 +1,4 @@ Codewars Aufgabe 1 Snail: https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1 Aufgabe 2 Vigenère Cipher Helper: https://www.codewars.com/kata/52d1bd3694d26f8d6e0000d3 +Aufgabe 3 PaginationHelper: https://www.codewars.com/kata/515bb423de843ea99400000a diff --git a/src/codewars/pagination_helper.py b/src/codewars/pagination_helper.py new file mode 100644 index 0000000..b3f7f99 --- /dev/null +++ b/src/codewars/pagination_helper.py @@ -0,0 +1,42 @@ +import math + + +class PaginationHelper: + + # The constructor takes in an array of items and an integer indicating + # how many items fit within a single page + def __init__(self, collection, items_per_page): + self.collection = collection + self.items_per_page = items_per_page + + # returns the number of items within the entire collection + def item_count(self): + return len(self.collection) + + # returns the number of pages + def page_count(self): + item_count = self.item_count() + return math.ceil(item_count / self.items_per_page) + + # 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 + def page_item_count(self, page_index): + number_of_pages = self.page_count() + if page_index > number_of_pages - 1 or page_index < 0: + return -1 + + item_count = self.item_count() + page_count_without_round_up = item_count / self.items_per_page + + if page_count_without_round_up - page_index > 1: + return self.items_per_page + else: + return item_count - page_index * self.items_per_page + + # 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 + def page_index(self, item_index): + if item_index < 0 or item_index >= self.item_count(): + return -1 + + return item_index // self.items_per_page -- 2.30.2