Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 基于python的NPTL数据结构_Python 3.x - Fatal编程技术网

Python 3.x 基于python的NPTL数据结构

Python 3.x 基于python的NPTL数据结构,python-3.x,Python 3.x,如果整数列表由严格递减的值序列和严格递增的值序列组成,则称其为谷值列表。递减和递增序列的长度必须至少为2。递减序列的最后一个值是递增序列的第一个值 编写一个Python函数valley(l),该函数接受一个整数列表,如果l是valley,则返回True,否则返回False 下面是一些例子来说明函数应该如何工作 valley([3,2,1,2,3]) 真的 假的 假的 def valley(列表): 如果(len(list)list[i+1]和cond1==False和cond2==False:

如果整数列表由严格递减的值序列和严格递增的值序列组成,则称其为谷值列表。递减和递增序列的长度必须至少为2。递减序列的最后一个值是递增序列的第一个值

编写一个Python函数
valley(l)
,该函数接受一个整数列表,如果l是valley,则返回True,否则返回False

下面是一些例子来说明函数应该如何工作

valley([3,2,1,2,3])
真的

假的

假的

def valley(列表): 如果(len(list)list[i+1]和cond1==False和cond2==False: cond1=真 elif list[i]>list[i+1]和cond1==True和cond2==False: 持续 elif列表[i]
def valley(l):
方向=[]
i=0

如果(l[i]欢迎来到StackOverflow。请阅读并遵循发布指南:我可以问什么类型的问题,以及如何提问:。记住,我们需要最少、完整、可验证的示例:。完成后,您可以单击
编辑
,编辑您的问题,以便我们提供帮助。一些解释会有所帮助
valley([3,2,1])
valley([3,3,2,1,2])
def valley(list):
  if(len(list)<4):
    return False
  cond1=False
  cond2=False
  cnt=1
  for i in range(0,len(list)-1):
    if list[i]>list[i+1] and cond1==False and cond2==False:
      cond1=True
    elif list[i]>list[i+1] and cond1==True and cond2==False:
      continue
    elif list[i]<list[i+1] and cond1==False:
      return False
    elif list[i]==list[i+1]:
      return False
    elif list[i]<list[i+1] and cond1==True and cond2==False:
      cond2=True
  return cond1 and cond2 
def valley(l):
    direction = []

    i = 0
    if(l[i]<l[i+1]):
        direction.append(1) # 1 means direction is upwards
    else:
        direction.append(-1) # -1 means direction is downwards

    i = i+1

    while(i<len(l)-1):
        if(direction[-1] == 1) and (l[i]>l[i+1]):
            direction.append(-1)
        elif(direction[-1] == -1) and (l[i] < l[i+1]):
            direction.append(1)
        i = i+1

    if(len(direction)==2) and (sum(direction) == 0) and (direction[0] == -1):
        return True
    else:
        return False