You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python数组峰谷判定代码问题求助:多测试用例不兼容

数组元素峰/谷/无判定逻辑代码修正问题

需求说明

实现数组元素的峰(Peak/P)、谷(Valley/V)及无判定(N)逻辑:

  • 峰(P):元素为局部较高数值
  • 谷(V):元素为局部较低数值
  • 无判定(N):不符合峰/谷条件的元素

测试用例

  • 测试用例1:
    • 输入:data = [5,4,4,4,4,3,3,8]
    • 预期结果:['P','N','N','N','N','N','V','P']
    • 当前代码输出:['P','N','N','N','P','N','N','P'](不符合预期)
  • 测试用例2:
    • 输入:data = [2,1,4,5,5,5,4]
    • 预期结果:['P','V','N','N','N','P','V']
    • 当前代码输出:符合预期

现有实现代码

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

修正方案

问题分析

  1. 首尾元素处理逻辑错误:首元素相等时错误添加了data[1]而非data[0];尾元素条件顺序混乱且存在重复append操作。
  2. 中间元素判断逻辑失效:部分条件(如i+1 > n-1)在循环中永远不成立,连续相等元素的判定逻辑错误,导致测试用例1中索引4的元素被误判为P。

修正后的代码

def get_Ps_values(data):
    dt = []
    details = []
    n = len(data)
    
    # 处理空数组或单元素数组的边界情况
    if n == 0:
        return dt, details
    if n == 1:
        dt.append(data[0])
        details.append('N')
        return dt, details
    
    # 处理第一个元素
    first_val = data[0]
    next_val = data[1]
    dt.append(first_val)
    if first_val > next_val:
        details.append('P')
    elif first_val < next_val:
        details.append('V')
    else:
        details.append('N')
    
    # 处理中间元素
    for i in range(1, n-1):
        current = data[i]
        dt.append(current)
        
        # 寻找左边第一个不等于当前值的元素
        left_idx = i - 1
        while left_idx >= 0 and data[left_idx] == current:
            left_idx -= 1
        left_val = data[left_idx] if left_idx >= 0 else None
        
        # 寻找右边第一个不等于当前值的元素
        right_idx = i + 1
        while right_idx < n and data[right_idx] == current:
            right_idx += 1
        right_val = data[right_idx] if right_idx < n else None
        
        # 判断峰/谷/无判定
        is_peak = False
        is_valley = False
        if left_val is not None and right_val is not None:
            is_peak = current > left_val and current > right_val
            is_valley = current < left_val and current < right_val
        elif left_val is not None:
            is_peak = current > left_val
            is_valley = current < left_val
        elif right_val is not None:
            is_peak = current > right_val
            is_valley = current < right_val
        
        if is_peak:
            details.append('P')
        elif is_valley:
            details.append('V')
        else:
            details.append('N')
    
    # 处理最后一个元素
    last_val = data[-1]
    prev_val = data[-2]
    dt.append(last_val)
    if last_val > prev_val:
        details.append('P')
    elif last_val < prev_val:
        details.append('V')
    else:
        details.append('N')
    
    return dt, details

关键修改说明

  1. 边界情况处理:新增空数组和单元素数组的处理逻辑,提升代码健壮性。
  2. 首尾元素逻辑修正:
    • 首元素:直接与下一个元素比较,相等时标记为N,修正了原代码错误添加元素的问题。
    • 尾元素:明确判断规则,删除重复append操作,修正条件顺序。
  3. 中间元素逻辑重构:
    • 通过寻找左右第一个不同元素的方式,准确判断连续相等元素的状态,解决测试用例1的误判问题。
    • 删除无效条件,简化判断逻辑,确保峰/谷的判定基于局部相对高低。

内容的提问来源于stack exchange,提问作者Samuel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 00:05:25