helper = PaginationHelper(['a','b','c','d','e','f'], 4)
helper.page_count() # should == 2
helper.item_count() # should == 6
helper.page_item_count(0) # should == 4
helper.page_item_count(1) # last page - should == 2
helper.page_item_count(2) # should == -1 since the page is invalid
# page_index takes an item index and returns the page that it belongs on
helper.page_index(5) # should == 1 (zero based index)
helper.page_index(2) # should == 0
helper.page_index(20) # should == -1
helper.page_index(-10) # should == -1 because negative indexes are invalid
class PaginationHelper:
# The constructor takes in an array of items and a integer indicating
# how many items fit within a single page
def __init__(self, collection, items_per_page):
pass
# returns the number of items within the entire collection
def item_count(self):
pass
# returns the number of pages
def page_count(self):
pass
# returns the number of items on the current 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):
pass
# determines what page an item 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):
pass
collection = range(1,25)
helper = PaginationHelper(collection, 10)
assert helper.page_count() == 3
assert helper.page_item_count(1) == 10
assert helper.page_item_count(2) == 4
assert helper.page_item_count(3) == -1
assert helper.page_index(0) == 0
assert helper.page_index(23) == 2
assert helper.page_index(24) == -1
assert helper.item_count() == 24
helper = PaginationHelper([], 10)
assert helper.page_index(0) == -1
class PaginationHelper:
# The constructor takes in an array of items and a 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):
return len(self.collection)//self.items_per_page + 1
# returns the number of items on the current 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):
page_num = self.page_count()-1
item_num = self.item_count()
if page_index > page_num:
return -1
elif page_index == page_num:
return item_num-page_num*self.items_per_page
else:
return self.items_per_page
# determines what page an item 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):
item_num = self.item_count()-1
if item_index > item_num or item_index < 0:
return -1
else:
return item_index//self.items_per_page
collection = range(1,25)
helper = PaginationHelper(collection, 10)
assert helper.page_count() == 3
assert helper.page_item_count(1) == 10
assert helper.page_item_count(2) == 4
assert helper.page_item_count(3) == -1
assert helper.page_index(0) == 0
assert helper.page_index(23) == 2
assert helper.page_index(24) == -1
assert helper.item_count() == 24
helper = PaginationHelper([], 10)
assert helper.page_index(0) == -1
class PaginationHelper:
# The constructor takes in an array of items and a 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):
x, y = divmod(len(self.collection), self.items_per_page)
if y == 0:return x
else: return x + 1
# returns the number of items on the current 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):
page_num = page_index + 1
if page_num > self.page_count(): return -1
else:
remainder_items = len(self.collection) - page_index * self.items_per_page
if remainder_items >= self.items_per_page: return self.items_per_page
else: return remainder_items
# determines what page an item 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 > len(self.collection) - 1: return -1
else:
x, y = divmod(item_index, self.items_per_page)
if y == 0: return item_index
else: return x
collection = range(1, 25)
helper = PaginationHelper(collection, 10)
assert helper.page_count() == 3
assert helper.page_item_count(1) == 10
assert helper.page_item_count(2) == 4
assert helper.page_item_count(3) == -1
assert helper.page_index(0) == 0
assert helper.page_index(23) == 2
assert helper.page_index(24) == -1
assert helper.item_count() == 24
helper = PaginationHelper([], 10)
assert helper.page_index(0) == -1
class PaginationHelper:
# The constructor takes in an array of items and a 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):
import math
return math.ceil(self.item_count()/self.items_per_page)
# returns the number of items on the current 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):
if page_index<0 or page_index>=self.page_count():
return -1
elif page_index<self.page_count()-1:
return self.items_per_page
else:
return self.item_count()%self.items_per_page
# determines what page an item 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
else:
return item_index//self.items_per_page
class PaginationHelper:
# The constructor takes in an array of items and a integer indicating
# how many items fit within a single page
def __init__(self, collection, items_per_page):
self.collection = collection
self.item_pre_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):
num = self.item_count()
if num % self.item_pre_page == 0:
return num / self.item_pre_page
return num // self.item_pre_page + 1
# returns the number of items on the current 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):
page = self.page_count()
if page_index < 0 or page_index >= page:
return -1
if page_index == page-1:
return self.item_count() % self.item_pre_page
return self.item_pre_page
# determines what page an item 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):
num = self.item_count()
if item_index < 0 or item_index >= num:
return -1
return (item_index+1)//self.item_pre_page
collection = range(1,25)
helper = PaginationHelper(collection, 10)
assert helper.page_count() == 3
assert helper.page_item_count(1) == 10
assert helper.page_item_count(2) == 4
assert helper.page_item_count(3) == -1
assert helper.page_index(0) == 0
assert helper.page_index(23) == 2
assert helper.page_index(24) == -1
assert helper.item_count() == 24
helper = PaginationHelper([], 10)
assert helper.page_index(0) == -1
class PaginationHelper:
# The constructor takes in an array of items and a 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):
return self.item_count()//self.items_per_page if self.item_count()%self.items_per_page==0 else self.item_count()//self.items_per_page+1
# returns the number of items on the current 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):
if self.item_count()%self.items_per_page==0:
page_list=[self.items_per_page for _ in range(self.page_count())]
else:
page_list=[self.items_per_page for _ in range(self.page_count()-1)]+[self.item_count()%self.items_per_page]
try:
return page_list[page_index]
except:
return -1
# determines what page an item 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):
return item_index//self.items_per_page if 0<=item_index<self.item_count() else -1
collection = range(1,25)
helper = PaginationHelper(collection, 10)
assert helper.page_count() == 3
assert helper.page_item_count(1) == 10
assert helper.page_item_count(2) == 4
assert helper.page_item_count(3) == -1
assert helper.page_index(0) == 0
assert helper.page_index(23) == 2
assert helper.page_index(24) == -1
assert helper.item_count() == 24
helper = PaginationHelper([], 10)
assert helper.page_index(0) == -1