Home > Software engineering >  How to solve array problem to check if element is greater, less than or equal its neighbor
How to solve array problem to check if element is greater, less than or equal its neighbor

Time:11-10

I'm still learning more about programming and have a problem with my code.

I have an array

data = [5,4,4,4,4,3,3,8] the expected result should be P,n,n,n,n,n,v,p. but I'm getting this p,n,n,n,p,n,n,p

#data = [2,1,4,5,5,5,4] expected result p,v,n,n,n,p,v (my code works for this. but the code must be able to solve the two)

I want to assign them to Peak(P) and Valley(V).

where the peak is the high number and Valley is the lower number.

.

def get_Ps_values(data):
    dt=[]
    details=[]
    n = len(data)
    #Handling first element in the data
    #We check if first element is less or equal to the next element
    if (data[0] < data[1]):
        #if there is, we append the element to the list
        dt.append(data[0])
        details.append('V')
        
    elif (data[0] > data[1]):
        dt.append(data[0])
        details.append('P')
        
    else:
        #this is checking if the first element is a P
        dt.append(data[1])
        details.append('N')
   #To handle other elements in the data that are not first and last element
    for i in range(1, n-1):
            if data[i] == data[i-1]==data[i 1]:
                dt.append(data[i])
                details.append('N')
            elif data[i] < data[i-1]<data[i 1]:
                dt.append(data[i])
                details.append('V')
            elif i 1 > n-1 and data[i 1] > data[i-1]:

                dt.append(data[i])
                details.append('P')
            elif (i == 1 or data[i-1] == data[i]) and (i == n-1 or data[i] > data[i 1]):  # Found peak
                dt.append(data[i])
                details.append('P')
            elif i-1 > n-1 and data[i 1] < data[i-1]:

                dt.append(data[i])
                details.append('V')
            else:
                dt.append(data[i])
                details.append('N')
    
    #Handling last element in the data
    #We check if the last element is greater or equal to the element before it.
    if (data[-1] > data[-2]):
        #if there is, we append the element to the list
        dt.append(data[-1])
        details.append('P')
        
    elif(data[-1] < data[-2]):
        #if there is, we append the element to the list
        dt.append(data[-1])
        details.append('N')
    elif(data[-1] >= data[-2]):
        #if there is, we append the element to the list
        dt.append(data[-1])
        details.append('V')
   
        #this is checking if the last element is a V
        dt.append(data[-1])
        details.append('N')

    return dt, details

Thanks

CodePudding user response:

Assuming:

  • that a peak is the last point of a stretch of identical values if it is strictly higher than the previous and next stretches (or of only one neighbor on the ends)
  • that is valley is the same for a strictly lower value compared to the neighbor(s)
  • all other points being "n"

You can use itertools.groupby to compare the stretches to the previous and next ones:

from itertools import groupby

def pvn(data):
    out = []
    pos = 0
    prev = None
    for k, g in groupby(data):
        g = list(g)
        pos = pos   len(g)
        next_ = data[pos] if pos < len(data) else None
        if (prev is None or k > prev) and (next_ is None or k > next_):
            out.extend(['n']*(len(g)-1) ['p'])
        elif (prev is None or k < prev) and (next_ is None or k < next_):
            out.extend(['n']*(len(g)-1) ['v'])
        else:
            out.extend(['n']*len(g))
        prev = k
    return out

pvn([5, 4, 4, 4, 4, 3, 3, 8])
# ['p', 'n', 'n', 'n', 'n', 'n', 'v', 'p']

pvn([2, 1, 4, 5, 5, 5, 4])
# ['p', 'v', 'n', 'n', 'n', 'p', 'v']
  • Related