【codewar-6kyu】PaginationHelper
2016-08-31 00:09
796 查看
##题目描述
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:
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_ndex 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
##思路分析
进入6kyu 之后突然就吃力了些,感觉难起来的并不是编码,而是逻辑的完备性。细心些,考虑各种非常规情况,超出处理阈值的情况,一定要——滴水不漏。
##代码解析
Python# 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
相关文章推荐
- 解读官方Android MediaPlayer API(1)
- Unity创建Assetbundle与加载
- LeetCode: Merge Two Sorted Lists
- Android网络状态判断
- maven配置远程仓库
- 鼠标指针移入移出改变图片透明度
- StringUtils方法全集
- loadrunner 参数化设置1!!!
- Oracle:SQL语句--对表的操作——添加主键约束
- Week1-0Introduction
- Oracle:SQL语句--对表的操作——添加主键约束
- 手动把web项目部署到tomcat
- CodeForces 711B Chris and Magic Square (暴力,水题)
- 《七周七Web开发框架》读后感
- java调.py文件指南
- 跨站点请求伪造
- 第一章——导言(2)
- poj 2559 Largest Rectangle in a Histogram 求面积最大的矩形(单调栈)
- javascript中BOM部分基础知识总结
- dotnet 你不得不晓得的一个编译开发工具