python 核心编程 第十一章
来源:互联网 发布:酒神淘宝店地址 编辑:程序博客网 时间:2024/06/16 13:44
#!/usr/bin/env python第4题
def max2(x,y):
return x if x>y else y
def min2(x,y):
return x if x<y else y
print max2(8,4)
print min2(8,4)
def my_max(anyList):
try: #Be sure the list isn't empty
a = anyList[0]
except Exception , docs:
print str(docs)
else:
for L in anyList:
a = a if a>L else L
return a
def my_min(anyList):
try:
a = anyList[0]
except Exception , docs:
print str(docs)
else:
for L in anyList:
a = a if a<L else L
return a
print my_max("123456765")
print my_min("bcdaoe")
第5题#!/usr/bin/env python
def changeTime(H,M):
timeH = H + float(M)/float(60)
timeM = H*60 + M
return [timeH,timeM]
print changeTime(3,10)
第6题#!/usr/bin/env python
def GD(G,pt = 0.1):
return G*pt
print GD(100)
print GD(100,0.3)
第7题#!/usr/bin/env python
def printf(strings,*lists):
LEN = len(strings)
listKey = ['d','f','s']
nuEle = 0
nuEle = strings.count("%f") + strings.count("%d") + strings.count("%s")
# for ch in strings:
# if ch == "%" and strings.index(ch) != LEN-1: #The Code print strings.index(ch) aways show the index of firs"%"
# nextCh = strings[strings.index(ch)+1] #I don,t know what happen...
# if nextCh in listKey:
# nuEle += 1
if nuEle != len(lists[0]):
print "Wrong Element."
return
resultString = ""
i = 0
iterStr = 0
while True:
if strings[iterStr] == "%":
try:
nextChs = strings[iterStr+1] #Be sure not out of range
except Exception:
resultString += strings[iterStr]
break
nextChs = strings[iterStr+1]
resultString += str(lists[0][i])
iterStr += 2
i += 1
else:
resultString += strings[iterStr]
iterStr += 1
if iterStr == LEN:
break
print resultString
printf("abc%fabc%d",[3.1,6])
printf("aa%d%s%d",[100,"gogogogogo",20])
printf("asd%d%",[11])
第8题#!/sur/bin/env python
def poly(X,Y):
return X,Y
list_1 = [1,2,3,4,5]
list_2 = ['a','b','c','d','e']
print map(poly,list_1,list_2)
print zip(list_1,list_2)
第9题#!/usr/bin/env python
def checkYear(Y):
if (Y%4 == 0 and Y%100 != 0) or (Y%400 == 0 and Y%100 == 0):
return True
else:
return False
listYear = [1000,2000,2600,1996,2001,1990,1900,1906]
print filter(checkYear,listYear)
第11题#!/sur/bin/env python
def avgerage(X,Y):
return X+Y
db = [1,2,3,5,7,9,10]
print "%.2f"%(float(reduce(avgerage,db))/float(len(db)))
第12题#!/usr/bin/env python
import sys
import os
def clearSpace(chs):
newChar = ""
if chs == " ":
return newChar
else:
return chs
newText = ""
fileName = open((sys.argv)[1],"r+")
for line in fileName:
newText += str(map(clearSpace,line))
newText += os.linesep
fileName.close()
fileName = open((sys.argv)[1],"w+")
fileName.write(newText)
fileName.close()
第13题#!/usr/bin/env python
import time
def counts(LEN):
for i in range(0,LEN):
pass
def timeit(fun_1,Len):
print"%s"%(time.clock())
fun_1(Len)
print"%s"%(time.clock())
print"Please Input Any Integer To Test:",
L = int(raw_input())
timeit(counts,L)
第16题#!/sur/bin/env python
import time
High = 10
SUM = 0
SUM_L = 0
def mult(X,Y):
return X*Y
for i in range(1,High+1): #b
SUM += reduce(mult,[x for x in range(1,i+1)])
print SUM
MULT = lambda x,y:x*y
for i in range(1,High+1): #c
SUM_L += reduce(MULT,[x for x in range(1,i+1)])
print SUM_L
#!/usr/bin/env python
from operator import add,sub,imul,ifloordiv
from random import randint,choice
ops = {'+':add,'-':sub,'*':imul,'/':ifloordiv}
fakeResult = randint(0,100)
ele = [randint(1,10) for i in range(2)]
op = choice("+-*/")
print"%d %s %d = %d"%(ele[0],op,ele[1],fakeResult)
realResult = (ops[op])(ele[0],ele[1])
print "Do You Think It's Right.(Y/N)",
jk = raw_input()
if fakeResult == realResult:
if jk == 'Y':
print"You Right."
else:
print "You Wrong"
else:
if jk == 'N':
print"You Right."
else:
print "You Wrong"
- Python核心编程 第十一章
- python 核心编程 第十一章
- python核心编程--第十一章
- python 核心编程第十一章 13题
- Python核心编程第十一章笔记
- python核心编程第十一章练习
- 《Python核心编程》第十一章:函数和函数式编程
- Python核心编程(第十一章)--函数和函数式编程
- Windows核心编程 第十一章 线程池的使用
- 第十一章 CrateDemo核心代码
- Python核心编程学习笔记-Python核心编程第七章
- python核心编程第六章
- Python 核心编程第十八章
- Python核心编程 第三章
- Python核心编程 第四章
- Python核心编程 第六章
- Python核心编程 第七章
- Python核心编程 第八章
- 第五章
- 第六章
- 第七章
- Python 核心编程 第八章 练习题
- python 核心编程 第十章
- python 核心编程 第十一章
- Leetcode 494. Target Sum Add to List
- python 核心编程 第九章
- python 调用 C/C++ 编程
- C Primer Plus 第十一章 编程练习 1-15题
- C Primer Plus 第十二章 编程练习 1-8题
- C Primer Plus 第十四章 编程练习 1-11题
- C Primer Plus 第十六章 编程练习 1-7题
- C++类成员变量是为什么选择private?