fork download
  1. class PaginationHelper:
  2.  
  3. def __init__(self, collection, items_per_page):
  4. '''The constructor takes in an array of items and a integer indicating
  5. how many items fit within a single page'''
  6. self.collection = collection
  7. '''Given array'''
  8. self.items_per_page = items_per_page
  9. '''Given number of items that should be on a single page'''
  10. self.result = [self.collection[i:i + self.items_per_page]
  11. for i in range(0, len(self.collection), self.items_per_page)]
  12. '''An array that orders our collection'''
  13.  
  14. def item_count(self):
  15. '''Returns the number of items within the entire collection'''
  16. return len(self.collection)
  17.  
  18. def page_count(self):
  19. '''Returns the number of pages'''
  20. return m.ceil(len(self.collection) / self.items_per_page)
  21.  
  22. def page_item_count(self, page_index):
  23. '''Returns the number of items on the current page. page_index is zero based.
  24. This method should return -1 for page_index values that are out of range.'''
  25. self.page_index = page_index
  26. try:
  27. return len(self.result[page_index])
  28. except IndexError:
  29. return -1
  30.  
  31. def page_index(self, item_index):
  32. '''Determines what page an item is on. Zero based indexes.
  33. This method should return -1 for item_index values that are out of range'''
  34. self.item_index = item_index
  35. if item_index not in range(len(self.collection)):
  36. return -1
  37. item = self.collection[item_index]
  38. for index, list_of_elements in enumerate(self.result):
  39. if item in list_of_elements:
  40. return index
  41. else:
  42. continue
  43.  
  44.  
  45. collection = [3, 1, 6, 'g', 5, [], (), 0, 'sdf']
  46. helper = PaginationHelper(collection, 2)
  47.  
  48. print(helper.collection)
  49. print(helper.result)
  50. print(helper.page_index(0))
Success #stdin #stdout 0.04s 8992KB
stdin
Standard input is empty
stdout
[3, 1, 6, 'g', 5, [], (), 0, 'sdf']
[[3, 1], [6, 'g'], [5, []], [(), 0], ['sdf']]
0