6-1.成员操作符in即可判断一个字符或者是一个字符串是不是在另一个字符串中
6-2.
import string
import keyword
alphas=string.letters+'_'
nums=string.digits
def check():
input=raw_input("identifier to test?")
if input[0] not in alphas:
return False
else:
if len(input)>1:
for x in input[1:]:
if x not in alphas+nums:
return False
if input in keyword.kwlist:
return False
else:
return True
else:
return True
if __name__ =="__main__":
res=check()
if res:
print "valid identifier!"
else:
print "invalid identifier!"
6-3.
a.输入字符串并且排序。这里用个冒泡排序实现
def mysort():
alist=[]
while True:
try:
num=raw_input("input a num : ")
except(EOFError,KeyboardInterrupt,):
break
alist.append(int(num))
length=len(alist)
for i in range(1,length):
for j in range(0,length-i):
if alist[j]<alist[j+1]:
alist[j],alist[j+1]=alist[j+1],alist[j]
print alist
if __name__=="__main__":
mysort()
6-4
def testscore(score):
if 90<score<100:
return 'A'
elif 80<score<89:
return 'B'
elif 70<score<79:
return 'C'
elif 60<score<69:
return 'D'
else:
return 'F'
def getscore(scorelist):
score=[]
sum=0.0
for x in scorelist:
sum+=float(x)
score.append(testscore(x))
print "avg:%s\nthe score:%s"%(sum/5,score)
def main():
scorelist=[]
for i in range(1,6):
num=int(raw_input("input a num : "))
scorelist.append(num)
getscore(scorelist)
if __name__=='__main__':
main()
6-5
a,b,c这三题不理解题目的意思。
d.回文字符串
def sysmmetry(tr):
return tr+tr[-1:0:-1]+tr[0]
if __name__=="__main__":
tr=raw_input("input a str : ")
print sysmmetry(tr)
6-6
def substr(str):
length=len(str)
for i in range(0,length):
if str[i]!=' ':
break
for j in range(length-1,0,-1):
if str[j]!=' ':
break
return str[i:j+1]
def main():
input=raw_input("input a str : ")
str=substr(input)
for (index,value) in enumerate(str):
print "%s:%s"%(index,value)
if __name__=="__main__":
main()
6-7
(a)这段代码要做的是输入一个数字n。在1...n中找到不是n因数的数字。
(b)对于任何的偶数它都不能处理是因为循环中的i=i+1应该放在else分支里面
(c)
# -*- coding: utf-8 -*-
#接收输入
num_str=raw_input("enter a number : ")
#转换为数字
num_num=int(num_str)
#构造列表
fac=range(1,num_num+1)
print "before:%s"%fac
i=0
while i<len(fac):
#如果当前列表中的元素是输入数字的因数
if num_num%fac[i]==0:
del fac[i]
else:
#不是因数时向后遍历
i=i+1
print "after:%s"%fac
6-8
numstr=['zero','one','two','three','four','five','six','seven','eight','nine']
def numstr():
num=int(raw_input("enter a number : "))
res=''
numlist=[]
while num:
digit=num%10
num=num/10
numlist.append(digit)
length=len(numlist)
if length==1:
print "%s"%numstr[numlist[0]]
elif length==2:
print "%s-%s"%(numstr[numlist[1]],numstr[numlist[0]])
elif length==3:
print "%s hundred and %s-%s"%(numstr[numlist[2]],numstr[numlist[1]],numstr[numlist[0]])
else:
print "the input num is bigger than 1000 or less than 0"
6-9
def hour_minute(totalminutes):
hour=totalminutes/60
minute=totalminutes%60
print "%s hours and %s minutes"%(hour,minute)
def main():
totalminutes=int(raw_input("enter total minutes: "))
hour_minute(totalminutes)
if __name__=="__main__":
main()
6-10
主要是考察ord()和chr()这两个函数
def str_change(strs):
length=len(strs)
res=''
for i in range(0,length):
str_int=ord(strs[i])
if 65<=str_int<65+26:
res=res+chr(str_int+32)
elif 97<=str_int<97+26:
res=res+chr(str_int-32)
else:
res=res+strs[i]
print res
def main():
strs=raw_input("input a str : ")
str_change(strs)
if __name__ == "__main__":
main()
6-11转换
def num_ip(num):
alist=[]
while num:
alist.append(num%256)
num=num/256
print "ip : %d.%d.%d.%d"%(alist[3],alist[2],alist[1],alist[0])
def ip_num(ipstr):
num=0
i=0
j=0
length=len(ipstr)
while i<length and j<length:
if ipstr[j]!='.':
j=j+1
else:
num=num*256+int(ipstr[i:j])
j=j+1
i=j
num=num*256+int(ipstr[i:j])
return num
def main():
ipstr=raw_input("input a ipstr : ")
num=ip_num(ipstr)
print "ip to num : %d"%num
num_ip(num)
if __name__=="__main__":
main()
6-12
def findchr(string,char):
length=len(string)
for i in range(0,length):
if string[i]==char:
return i
return -1
def rfindchr(string,char):
length=len(string)
for i in range(length-1,-1,-1):
if string[i]==char:
return i
return -1
def substr(string,origchar,newchar):
res=''
length=len(string)
for i in range(0,length):
if string[i]==origchar:
res=res+newchar
else:
res=res+string[i]
print "after : %s"%res
def main():
longstr=raw_input("enter a long str : ")
char=raw_input("enter a char : ")
print "the %s first at %d\nthe %s last at %d"%(char,findchr(longstr,char),char,rfindchr(longstr,char))
newchar=raw_input("enter an new char : ")
substr(longstr,char,newchar)
if __name__=="__main__":
main()