Ex62:
#!/usr/bin/env python
import string,keyword
alphas=string.letters+'_'
nums=string.digits
myinput=raw_input('Identifier to test? ')
if keyword.iskeyword(myinput):
print 'ok as a keyword'
elif myinput[0] not in alphas:
print 'invalid input:first symbol must be alphabetic'
else:
for char in myinput[1:]:
if char not in alphas+nums:
print "symbols error"
break
else:
print "ok"
Ex63
#!/usr/bin/env python
def insert_sort(num_list):
for i in range(1,len(num_list)):
key,j=num_list[i],i-1
if key<num_list[i-1]:
continue
while j>=0 and key>num_list[j]:
j-=1
num_list.remove(key)
num_list.insert(j+1,key)
return num_list
def dict_sort(num_list):
num_list= [str(x) for x in num_list]
return insert_sort(num_list)
print insert_sort([1,2,7,4,5])
print dict_sort([11,2,7,4,5])
Ex66:
#!/usr/bin/env python
def strip(word):
if word[0]!=' ' and word[-1]!=' ':
return word
if word[0]==' ':
word=word[1:]
if word[-1]==' ':
word=word[:-1]
return strip(word)
print strip(" abc ")
Ex67:
#!/usr/bin/env python
num_str=raw_input("Enter a number:")
num_num=int(num_str)
fac_list=range(1,num_num+1)
new_list=[]
print 'BEFORE:',fac_list
for num in fac_list:
if num_num%num!=0:
new_list.append(num)
print 'AFTER:',new_list
Ex68:
#!/usr/bin/env python
def change_to_eng(num):
u1={'0':'',
'1':'one',
'2':'two',
'3':'three',
'4':'four',
'5':'five',
'6':'six',
'7':'seven',
'8':'eight',
'9':'nine' }
u2={'10':'ten',
'11':'eleven',
'12':'twelve',
'13':'thirteen',
'14':'fourteen',
'15':'fifteen',
'16':'sixteen',
'17':'seventeen',
'18':'eighteen',
'19':'nineteen'}
u3={'2':'twenty',
'3':'thirty',
'4':'forty',
'5':'fifty',
'6':'sixty',
'7':'seventy',
'8':'eighty',
'9':'ninety'}
if num[0]=='0':
return change_to_eng(num[1:])
if 4<=len(num)<=6:
return change_to_eng(num[:-3])+' thousand '+change_to_eng(num[-3:])
elif len(num)==3:
return change_to_eng(num[0])+' handred '+change_to_eng(num[1:])
elif len(num)==2:
if 10<=int(num)<=19:
return u2[num]
else:
return u3[num[0]]+' '+u1[num[1]]
#return num
elif len(num)==1:
return u1[num]
else:
return 'wrong number'
print change_to_eng('12341')
Ex69:
#!/usr/bin/env python
def change_to_hour(minute):
t=divmod(minute,60)
print 'hour: ',t[0]
print 'minute: ',t[1]
change_to_hour(1000)
Ex611:
#!/usr/bin/env python
def int_to_ip(num_int):
t=[]
for i in range(4):
num_int,v=divmod(num_int,256)
t.insert(i,v)
return str(t[3])+'.'+str(t[2])+'.'+str(t[1])+'.'+str(t[0])
def ip_to_int(ip):
num=0
t=ip.split('.')
t.reverse()
for i,n in enumerate(t):
num+= pow(256,i)*int(n)
return num
x=ip_to_int('8.8.8.8')
print int_to_ip(x)
Ex612:
#!/usr/bin/env python
def findstr(string,char):
for i,c in enumerate(string):
if c==char:
return i
return -1
def rfindstr(string,char):
m=len(string)
for i in range(1,m+1):
if string[-i]==char:
return m-i+1
return -1
def substr(string,origchar,newchar):
i=findstr(string,origchar)
if i==-1:
return -1
elif i==len(string)-1:
return string[:-1]+newchar
else:
return string[:i]+newchar+string[i+1:]
print substr('abadef','f','h')
Ex614:
#!/usr/bin/env python
from random import choice
class Rochambeau(object):
def __init__(self,ch=choice(['stone','scissor','cloth'])):
if ch not in ('stone','scissor','cloth'):
raise TypeError,'you must choose stone,scissor or cloth'
self.ch=ch
def __gt__(self,other):
if (self.ch=='stone' and other.ch=='scissor')\
or (self.ch=='scissor' and other.ch=='cloth')\
or (self.ch=='cloth' and other.ch=='stone'):
return True
else:
return False
def __lt__(self,other):
if (self.ch=='scissor' and other.ch=='stone')\
or (self.ch=='cloth' and other.ch=='scissor')\
or (self.ch=='stone' and other.ch=='cloth'):
return True
else:
return False
def __eq__(self,other):
if self.ch==other.ch:
return True
else:
return False
def main():
user_input=raw_input('chose stone,scissor or cloth: ')
com=Rochambeau()
user=Rochambeau(user_input)
if com>user:
print 'you lose'
elif com==user:
print 'draw'
elif com<user:
print 'you win'
if __name__=='__main__':
main()
Ex615:
#!/usr/bin/env python
from datetime import date
def date_reg(string):
year,mon,day=string.split('/')
return date(int(year),int(mon),int(day))
delta1=date_reg('2013/6/16')-date_reg('1983/07/05')
print delta1.days
delta2=date.today()-date_reg('1983/07/05')
print delta2.days
next_birthday=date_reg('1983/07/05').replace(year=date.today().timetuple()[0])
if next_birthday<date.today():
next_birthday=next_birthday.replace(year=next_birthday.timetuple()[0]+1)
delta3=next_birthday-date.today()
print delta3.days
Ex617:
#!/usr/bin/env python
import copy
def myPop(ls):
ls.pop()
return ls
l=[1,2,3,4,5]
t=copy.deepcopy(l)
print myPop(t)
print l
Ex619:
#!/usr/bin/env python
def output(mlist,col,v=False):
row=(len(mlist)+col-1)/col
R=[[0 for x in range(col)] for y in range(row)]
i=0
if not v:
for x in range(row):
for y in range(col):
if i>=len(mlist):
R[x].remove(0)
continue
R[x][y]=mlist[i]
i+=1
elif v:
flag=0
last_row_c=len(mlist)%col
for y in range(col):
if y>last_row_c-1 and last_row_c!=0 and flag==0:
R[row-1].remove(0)
row-=1
flag=1
for x in range(row):
R[x][y]=mlist[i]
i+=1
for i,x in enumerate(R):
for j,y in enumerate(R[i]):
if j<len(x)-1:
print str(y)+' ',
else:
print str(y)+'\n',
output([1,2,3,4,5,6,7,8,9],2,True)
转载于:https://blog.51cto.com/thuhak/1223432