摘要:题目引用:https://zhuanlan.zhihu.com/p/28328304
阅读全文
摘要:#! /usr/bin/python# coding=utf-8"""基于这篇文章的python实现http://blog.sae.sina.com.cn/archives/307"""import unittestdef pmt(s): """ PartialMatchTable """ prefix = [s[:i+1] for i in range(len(s)-1)] postfix = [s[i+1:] for i in range(len(s)-1)] inter
阅读全文
摘要:在高峰时间,实习生小飞常常会被电梯每层楼都停弄得很不耐烦,于是他想出了这样一个办法:由于楼层并不高,那么在繁忙的时间,每次电梯从一层往上走时,我们只允许电梯停在其中的某一层。所有乘客都从一楼上电梯,到达某层楼后,电梯听下来,所有乘客再从这里爬楼梯到自己的目的层。在一楼时,每个乘客选择自己的目的层,电梯则自动计算出应停的楼层。问:电梯停在哪一层楼,能够保证这次乘坐电梯的所有乘客爬楼梯的层数之和最少?#! /usr/bin/python# coding=utf-8import random,mathfrom itertools import groupbyfloor = 5def main():
阅读全文
摘要:在节假日的时候,书店一般都会做促销活动。由于《哈利波特》系列相当畅销,店长决定通过促销活动来回馈读者。在销售的《哈利波特》平装本系列中,一共有五卷,用编号0, 1, 2, 3, 4来表示。假设每一卷单独销售均需要8欧元。如果读者一次购买不同的两卷,就可以扣除5%的费用,三卷则更多。假设具体折扣的情况如下:本数折扣25%310%420%525%在一份订单中,根据购买的卷数以及本书,就会出现可以应用不同折扣规则的情况。但是,一本书只会应用一个折扣规则。比如,读者一共买了两本卷一,一本卷二。那么,可以享受到5%的折扣。另外一本卷一则不能享受折扣。如果有多种折扣,希望能够计算出的总额尽可能的低。要求根
阅读全文
摘要:问题描述: 有一摞烙饼,因为一只手端着盘子,所以只能用另外一只手来给烙饼排序,将烙饼由大到小排好序。这样就要求我们在给烙饼排序的时候总是将最上面的N个烙饼一起翻转。如果最下面的烙饼是最大的,那么只需要解决上面的N-1个烙饼,同理可以最后到解决两个烙饼的排序。#! /usr/bin/python# coding=utf-8import randomdef main(): arr = random.sample(xrange(100), 10) print arr n = [] while arr: arr = reverse(arr) n.in...
阅读全文
摘要:#! /usr/bin/python# coding=utf-8import mathdef main(x,y): #对减数取反 x, y = bin(x)[2:], bin(y)[2:] l = max([len(x),len(y)]) #最大长度 #对y取反 y = "1" * (l - len(y)) + "".join(['1' if i == '0' else '0' for i in y]) x,y = int(x,2), int(y,2) z = x + y + 1 z = bin(z) #拿
阅读全文
摘要:#!/usr/bin/python#coding=utf-8#http://docs.python.org/library/threading.htmlimport threading, time, randomfrom collections import dequeclass threadPool(object): def __init__(self, maxNum = 10): self.maxNum = maxNum self.deque = deque() def push(self, target, args = None): ...
阅读全文
摘要:#!/usr/bin/python#coding=utf-8#http://docs.python.org/library/collections.html#count对象 Only 2.7from collections import Counter#统计字母出现的次数Counter('hello world') Counter(['red', 'blue', 'red', 'green', 'blue', 'blue']) #小于等于0的会被忽略c = Counter(a=4,
阅读全文
摘要:#!/usr/bin/python#coding=utf-8#http://docs.python.org/library/string.htmlimport string#全部字母 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZprint string.ascii_letters print string.letters #根据地域来的#全部小写字母 abcdefghijklmnopqrstuvwxyzprint string.ascii_lowercaseprint string.lowercase#全部大写字母 ABCDEFGHI
阅读全文
摘要:ep.io是一个基于python的云托管服务商。创始人Andrew (python最流利web框架Django的主要贡献者),现在的技术层面的支持都是他在。目前是邀请质,你需要留个email给他,他过一段时间就会帮你开通了。现在让我们来看看如何在ep.io上如何写一个Hello world站点。因为不是很喜欢Django那种很重的框架,所以选用Flask.你一步是确保你的机器安装了下面的组件sudo apt-get gitsudo apt-get python-pipsudo apt-get openssh-client安装epio的管理组件pip install epio之后再來就是 dep
阅读全文
摘要:#!/usr/bin/python#coding=utf-8import osimport marshal,cPickle"""marshal只能序列化有限的类型而cPickle能够序列化自定义的类型"""class Foo: def __init__(self, name): self.name = name def __str__(self): return self.nameo=range(0,10)L = Foo("Goodspeed")cls = [marshal,cPickle]for c in cls
阅读全文
摘要:#! /usr/bin/python# coding=utf-8import calendar"""返回的某个月的日历返回类型是字符串型"""cal = calendar.month(2011, 11)"""返回一年的日历"""cal = calendar.calendar(2011)cal = calendar.HTMLCalendar(calendar.MONDAY)"""打印出一个月日历"""cal.forma
阅读全文
摘要:#! /usr/bin/python# coding=utf-8from datetime import datetime, tzinfo,timedelta"""tzinfo是关于时区信息的类tzinfo是一个抽象类,所以不能直接被实例化"""class UTC(tzinfo): """UTC""" def __init__(self,offset = 0): self._offset = offset def utcoffset(self, dt): return tim
阅读全文
摘要:#! /usr/bin/python# coding=utf-8from datetime import datetime,date,time"""date类型顾名思义就是只表示日期,而time只表示time"""today = date.today()attrs = [("year","年"),( 'month',"月"),( 'day',"日")]for k,v in attrs: "today.%s = %s
阅读全文
摘要:#! /usr/bin/python# coding=utf-8from datetime import datetime,timedelta"""timedelta代表两个datetime之间的时间差"""now = datetime.now()past = past = datetime(2010,11,12,13,14,15,16)timespan = now - past#这会得到一个负数past - nowattrs = [("days","日"),( 'seconds'
阅读全文
摘要:#! /usr/bin/python# coding=utf-8import datetime"""datetime的功能强大能支持0001年到9999年""""""当前时间返回的是一个datetime类型now方法有个参数tz,设置时区类型。如果没有和方法today的效果一样"""now = datetime.datetime.now()#UTC时间datetime.datetime.utcnow()attrs = [("year","年&qu
阅读全文
摘要:#! /usr/bin/python# coding=utf-8import timefrom datetime import datetime"""表示日常所用时间的类,是用C实现的内嵌类。功能比较简单,但效率高。表示的时间范围有限1970年1月1日到2038年1月19日。""""""当前时间 返回的一个float型,以一个固定时间epoch(1970年1月1日0时起经过的秒数)因为time终究是以float型来表示的,所以对于timespan的问题,基本就成了数字问题。""&quo
阅读全文
摘要:# -*- coding: UTF-8 -*-#!/usr/bin/env pythondef pairwise(iterable): """ 交叉形成字典 """ itnext = iter(iterable).next while True: yield itnext(),itnext()def distinct(seq): """ 对序列进行去重 """ return dict.fromkeys(L).keys() if __name__ == '__main_
阅读全文