32.使用不定长参数定义一个函数max_min,接受的参数类型是数值,最终返回这些数中的最大值和最小值
def max_min(*args):
max_num = args[0]
min_num = args[0]
for num in args:
if num > max_num:
max_num = num
if num < min_num:
min_num = num
return max_num,min_num
m_max,m_in = max_min(1,3,5,6,23,75,12)
print("最大值为%d" % m_max)
print("最小值为%d" % max_min)
33.定义函数findall,要求返回符合要求的所有位置的起始下标,如字符串"helloworldhellopythonhelloc++hellojava",
需要找出里面所有的"hello"的位置,返回的格式是一个元组,即:(0,10,21,29)
def findall(all_str,sub_str):
index_list = []
start = 0
while True:
index = all_str.find(sub_str,start)
if index == -1:
break
index_list.append(index)
start = index + len(sub_str)
return tuple(index_list)
all_str = "helloworldhellopythonhelloc++hellojava"
sub_str = "hello"
ret = findall(all_str,sub_str)
print(ret)