元组的风暴之最长连续递增序列
小美:前几天老师布置了一道题目:求最长连续递增子序列的长度。我感觉题目并不难,很快就做出来了。老师说我的代码有错误,可我没发现错误啊,我测试了好几组数据都是正确的。
阿福:有这种事情?你把代码发给我看看。
题目1:
求最长连续递增子序列的长度。例如,在元组(1,9,2,5,7,3,4,6,8,0)中最长连续递增子序列为(3,4,6,8),其长度为4。
函数功能:求最长连续递增子序列的长度
函数名:def sub_num(a: tuple) -> int
参数表:a -- 元组。
返回值:返回最长连续递增子序列的长度。
示例:输入a=(1,9,2,5,7,3,4,6,8,0),返回4
代码1:
def sub_num(a: tuple) -> int:
if not a: #a是空元组
return 0
max_len, c = 1,1
for i in range(1, len(a)):
if a[i]> a[i-1]:
c += 1
else:
if c> max_len:
max_len = c
c = 1
return max_len
阿福:让我仔细瞧