如果整数列表包含一系列严格递减的值,然后是一系列严格递增的值,则该整数列表被称为谷值。递减和递增序列的长度必须至少为2.递减序列的最后一个值是递增序列的第一个值。谷歌的python函数升序降序
-3
A
回答
0
def valley(list):
if(len(list)==0):
return(True)
if(len(list)==1):
return(False)
if(list[0]<list[1]):
return(False)
for i in range(0,len(list)-1):
if(list[i]<list[i+1]):
pos=i
break
if(list[i]==list[i+1]):
return(False)
else:
return(False)
for i in range(pos,len(list)-1):
if(list[i]>=list[i+1]):
return(False)
return(True)
0
def valley(l):
if len(l) < 4:
return False
else:
for i in range(0,l.index(min(l))):
if l[i] > l[i+1]:
i+=1
else: return False
for j in range(l.index(min(l)),len(l)-1):
if l[j] < l[j+1]:
j+=1
else: return False
if i==l.index(min(l)) and j==len(l)-1:
return True
else: return False
0
def valley(list):
if (len(list) < 3):
return False
ucount = 1
lcount = 1
for i in range(0, len(list) - 1):
if list[i] > list[i + 1]:
if lcount > 1:
return False
ucount = ucount + 1
if list[i] < list[i + 1]:
lcount = lcount + 1
if list[i] == list[i + 1]:
return False
if ucount >1 and lcount > 1:
return True
else:
return False
嗯,这代码对我的作品。
0
我认为一个更好的答案会包含上面的一些答案,以给出一个非常精简的代码,如下所示。
def valley(l):
if(len(l)<3):
return(False)
for i in range(0,len(l)-1):
if(l[i]<l[i+1]):
for i in range(i,len(l)-1):
if(l[i]>=l[i+1]):
return(False)
return(True)
elif(l[i]==l[i+1]):
return(False)
else:
return(False)
相关问题
- 1. 升序和降序在Python 3
- 2. 升序和降序的Java
- 3. Python:函数返回数字的升序
- 4. 如何升序降序?
- 5. mongodb降序升序索引
- 6. data.frame(NOT升序/降序)
- 7. 升序和降序排列
- 8. 订单整数在升序和降序
- 9. Python递归函数不降序
- 10. 在python中对气泡排序帮助 - 升序和降序
- 11. 排序字典python值降序,但按键升序
- 12. 谷歌图(JavaScript)的 - 难以上升/下降了近数
- 13. MYSQL按升序和降序排序
- 14. XSLT排序:升序和降序
- 15. 排序gridview列升序和降序
- 16. XQuery按升序和降序排序
- 17. 改变升序排序降序
- 18. golang排序切片升序或降序
- 19. Modx:getResources - 按ID升序/降序排序?
- 20. Django反向的升序/降序排列
- 21. 如何创建一个升序和降序数组的数组?
- 22. django order_by查询集,升序和降序
- 23. SQL按月升序和月份降序?
- 24. unicode图标升序和降序
- 25. 以升序和降序打印链表
- 26. Mysql排名,升序或降序
- 27. 按升序/降序排列vba excel
- 28. 根据列值升序和降序
- 29. PDO - 按价格升序或降序
- 30. 根据Y值升序或降序
你究竟想要什么?这是作业吗? – usamec