Merge pull request 'Aufgabe 3 Codewars' (#4) from Aufgabe3 into master
Reviewed-on: #4 Code sieht soqweit solide aus. Kleine Anmerkung zu den Kommentaren: Wenn Du die anstelle mit # oberhalb der Funktion mit """ innerhalb der Funktion schreibst, kannst Du die für automatische Dokumentierungen (z.B. sphinx) und für help() nutzen und das ganz ohne Zusatzaufwand.
This commit is contained in:
commit
81fa3cae43
@ -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
|
||||
|
||||
42
src/codewars/pagination_helper.py
Normal file
42
src/codewars/pagination_helper.py
Normal 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
|
||||
Loading…
x
Reference in New Issue
Block a user