如果整數列表包含一系列嚴格遞減的值,然後是一系列嚴格遞增的值,則該整數列表被稱爲谷值。遞減和遞增序列的長度必須至少爲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