2017-08-12 10 views
-3

厳密に減少する値のシーケンスに続いて厳密に増加する値のシーケンスで構成される場合、整数のリストは谷と言われます。減少および増加する配列は、少なくとも2の長さでなければならない。減少する配列の最後の値は、増加する配列の最初の値である。昇順降順の谷のためのpython関数

+0

正確に何をしたいですか?これは宿題ですか? – usamec

答えて

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) 
関連する問題