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

Julia复现Python首个重复值查找嵌套循环代码问题排查

复现Python逻辑到Julia时的运行异常排查

我尝试查找列表或数组中首次出现的重复值,已编写可正常运行的Python版本代码,在将对应逻辑复现到Julia时无法得到预期结果,请求协助排查。

正常运行的Python实现代码

def firstDuplicateValue(array):
    # Write your code here.
    index = float('inf')
    for x, i in enumerate(array):
        for j, k in enumerate(array):
            if i == k and x != j and j > x:
                if j < index:
                    index = j
    if index == float('inf'):
        return -1
    else:
        return array[index]

运行异常的Julia版本实现

对应逻辑编写的Julia代码如下,运行后无法得到预期结果:

function firstDuplicateValue(array)
    index = Inf
    for (ind_1, value_1) in enumerate(array)
        for (ind_2, value_2) in enumerate(array)
            if value_1 == value_2 && ind_2 > ind_1  
                if ind_2 < index
                    index = ind_2
                end
            end
        end
    end
    if index == Inf
        return -1
    else 
        return array[index]
    end
end

说明:我知晓该需求存在时间复杂度更优的实现方案,本次尝试仅为学习Julia基础语法,不做算法优化讨论。

问题最终结论

提问者后续补充:代码本身逻辑可正常运行,出现异常的原因是向函数传入了错误输入,属于个人笔误。


内容的提问来源于stack exchange,提问作者Harneet.Lamba

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 16:15:45