day 9函数作业
print('=======第1题=======')
def exchange_key_value(dict1):
dict0=dict([(value,key) for key,value in dict1.items()])
print(f'dict1={dict0}')
dict1={'a':1, 'b':2, 'c':3}
exchange_key_value(dict1)
print('=======第2题=======')
def join_alphabet(string):
new_str=''
for x in string:
if 'a'<=x<='z'or'A'<=x<='Z':
new_str+=x
print(new_str)
str1='12a&bc12d-+'
join_alphabet(str1)
print('=======第3题=======')
def capitalize(str_alp):
str0=str_alp[0]
new_str=''
if 'a'<=str0<='z':
x=chr(ord(str0)-32)
new_str=x+str_alp[1:]
else:
new_str=str_alp[:]
print(new_str)
capitalize('Abc')
print('=======第4题=======')
def endswith(str1,str2):
for i in range(-1,-len(str2)-1,-1):
if str1[i]!=str2[i]:
print(False)
break
else:
print(True)
endswith('abc231ab','cab')
print('=======第5题=======')
def isdigit(str1):
for x in str1:
if not '0'<=x<='9':
print(False)
break
else:
print(True)
isdigit('23函数')
print('=======第6题=======')
def upper_self(str1):
new_str=''
for x in str1:
if 'a'<=x<='z':
new_str+=chr(ord(x)-32)
else:
new_str+=x
print(new_str)
upper_self('abH23好rp1')
print('=======第7题=======')
def rjust(str0,n,char):
str1=n*char
list1=list(str1)
for i in range(-1,-len(str0)-1,-1):
list1[i]=str0[i]
str2=''.join(list1)
print(str2)
rjust('abc',7,'^')
print('=======第8题=======')
def index_self(list1,item):
length=len(list1)
list0=[]
for i in range(length):
if list1[i]==item:
list0.append(i)
if list0 == []:
print( -1)
else:
for x in list0:
print(x, end=',')
index_self(['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'],1)
print('=======第9题=======')
def len_self(seq):
count1=0
for x in seq:
count1+=1
print(count1)
len_self((1, 34, 'a', 45, 'bbb'))
print('=======第10题=======')
def max_self(seq):
if isinstance(seq,dict):
list1=list(seq)
max1=seq[list1[0]]
for x in list1:
if seq[x]>max1:
max1=seq[x]
print(max1)
elif isinstance(seq,str):
max2=seq[0]
for x in seq[1:]:
if ord(x)>ord(max2):
max2=x
print(max2)
else:
max3=seq[0]
for x in seq[1:]:
if x>max3:
max3=x
print(max3)
max_self('abcdpzasdz')
max_self([-7, -12, -1, -9] )
print('=======第11题=======')
def in_self(seq,item):
list0=list(seq)
for x in list0:
if x== item:
print(True)
break
else:
print(False)
in_self( (12, 90, 'abc'),'90')
print('=======第12题=======')
def replace_self(str1,old,new):
length1=len(str1)
length2=len(old)
new_str=''
i=0
while i<length1:
if str1[i:i+length2]==old:
new_str+=new
i+=length2
else:
new_str+=str1[i]
i+=1
print(new_str)
replace_self('how are you? and you?','you','me')