【每日一题20220713】实现分页功能

:mage:‍在本练习中,您将加强对 page-fu 的掌握。您将完成 PaginationHelper 类,这是一个实用程序类,有助于查询与数组相关的分页信息。

该类旨在接收一组值和一个整数,该整数指示每页允许多少个项目。集合/数组中包含的值的类型不相关。

以下是如何使用此类的一些示例:

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

题目难度:简单
题目来源:https://www.codewars.com/kata/515bb423de843ea99400000a

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

import math
class PaginationHelper:

    def __init__(self, collection, items_per_page):
        self.collection=collection
        self.items_per_page=items_per_page

    def item_count(self):
        ##返回collection数组长度
        return len(self.collection)

    def page_count(self):
        ##返回collection的页码数
        return math.ceil(self.item_count()/self.items_per_page)

    def page_item_count(self, page_index):
        ##返回页码对应有的数据记录数,索引从0开始
        if 0<=page_index<self.page_count()-1:
            return self.items_per_page
        elif page_index==self.page_count()-1:
            return  self.item_count()%self.items_per_page
        else:
            return -1

    ##返回第item_index个数对应的页码,从item_index=0开始
    def page_index(self, item_index):
        if len(self.collection)>0:
            if 0<=item_index<=self.items_per_page:
                return 0
            elif self.items_per_page<item_index<=self.item_count()-1:
                return math.ceil(item_index/self.items_per_page)-1
            else:
                return -1
        else:
            return -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
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