Aufgabe 3 Codewars #4

Merged
schmidmarco merged 1 commits from Aufgabe3 into master 2026-03-13 12:06:46 +01:00
2 changed files with 43 additions and 0 deletions

View File

@ -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

View File

@ -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