[5 kyu] PaginationHelper

    科技2023-10-01  79

    [5 kyu] PaginationHelper

    文章目录

    [5 kyu] PaginationHelperQuestionSample TestsMy Answer (accepted)Suggested Answer

    Question

    Sample Tests

    My Answer (accepted)

    # TODO: complete this class 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): self.combine_page = list() i = 0 while i < self.item_count(): self.combine_page.append(self.collection[i:i+self.items_per_page]) i += self.items_per_page print(self.combine_page) return len(self.combine_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): return len(self.combine_page[page_index]) if page_index < len(self.combine_page) else -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): if item_index < 0 or (item_index + 1) > self.item_count(): return -1 else: for (index, item) in enumerate(self.combine_page): print(index,item) begin_ = index * self.items_per_page end_ = begin_ + len(item) - 1 print(begin_,end_) if item_index >= begin_ and item_index <= end_: return index

    Suggested Answer

    # TODO: complete this class 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): if len(self.collection) % self.items_per_page == 0: return len(self.collection) / self.items_per_page else: 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): if page_index >= self.page_count(): return -1 elif page_index == self.page_count() - 1: return len(self.collection) % self.items_per_page or 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): if item_index >= len(self.collection) or item_index < 0: return -1 else: return item_index / self.items_per_page
    Processed: 0.023, SQL: 8