【codewar-6kyu】PaginationHelper

##题目描述

Description:

For this exercise you will be strengthening your page-fu mastery. You will complete the PaginationHelper class, which is a utility class helpful for querying paging information related to an array.

The class is designed to take in an array of values and an integer indicating how many items will be allowed per each page. The types of values contained within the collection/array are not relevant.

The following are some examples of how this class is used:

 1 helper = PaginationHelper(['a','b','c','d','e','f'], 4)
 2 helper.page_count # should == 2
 3 helper.item_count # should == 6
 4 helper.page_item_count(0)  # should == 4
 5 helper.page_item_count(1) # last page - should == 2
 6 helper.page_item_count(2) # should == -1 since the page is invalid
 7 
 8 # page_ndex takes an item index and returns the page that it belongs on
 9 helper.page_index(5) # should == 1 (zero based index)
10 helper.page_index(2) # should == 0
11 helper.page_index(20) # should == -1
12 helper.page_index(-10) # should == -1 because negative indexes are invalid

##思路分析

进入6kyu 之后突然就吃力了些,感觉难起来的并不是编码,而是逻辑的完备性。

细心些,考虑各种非常规情况,超出处理阈值的情况,一定要——滴水不漏。

##代码解析

Python

 1 # TODO: complete this class
 2 
 3 class PaginationHelper:
 4 
 5   # The constructor takes in an array of items and a integer indicating
 6   # how many items fit within a single page
 7   def __init__(self, collection, items_per_page):
 8     self.collection = collection
 9     self.items_per_page = items_per_page
10       
11   
12   # returns the number of items within the entire collection
13   def item_count(self):
14     return len(self.collection)
15   
16   # returns the number of pages
17   def page_count(self):
18     if len(self.collection) % self.items_per_page == 0:
19       return len(self.collection) / self.items_per_page
20     else:
21       return len(self.collection) / self.items_per_page + 1
22     
23       
24   
25   # returns the number of items on the current page. page_index is zero based
26   # this method should return -1 for page_index values that are out of range
27   def page_item_count(self,page_index):
28     if page_index >= self.page_count():
29       return -1
30     elif page_index == self.page_count() - 1:
31       return len(self.collection) % self.items_per_page or self.items_per_page
32     else:
33       return self.items_per_page
34         
35       
36   
37   # determines what page an item is on. Zero based indexes.
38   # this method should return -1 for item_index values that are out of range
39   def page_index(self,item_index):
40     if item_index >= len(self.collection) or item_index < 0:
41       return -1
42     else:
43       return item_index / self.items_per_page
44       

 

转载于:https://www.cnblogs.com/pudding-ai/p/5824222.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值