python_test_22

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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值