如何将这些流程图GOTO语句转换为Python?

2024-04-27 04:01:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些流程图,它们代表了寻找一个时间序列的波峰和波谷的算法

我试图将它们转换为Python,但我不知道如何修复“GOTO”的问题

def minzo(trough,peak, i,x,r,p,sp):
    f=-1
    i=i+1
    if i == (len(ts) - 1):
        endzo1 = endzo()
    else:
        if x[i] < trough:
            trough=x[i]
        else:
            if x[i]-trough>=r:
                p=p+1
                sp.append(trough)
                peak=ts[i]
                maxzo1=maxzo()
    return trough,peak,i,x,r,p,sp

x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
sp = []
r = 1
trough = x[0]
peak = x[0]
i = -1

while i<len(x):
    i = i + 1
    if i == (len(x)-1):
        GOTO ENDZO???
    else:
        if x[i]>peak:
            peak=x[i]
            if peak-trough>=r:
                sp.append(trough)
                GOTO MAXZO

        else:
            if x[i]<trough:
                trough=x[i]
                if peak-trough>=r:
                    sp.append(peak)
                    GOTO MINZO

流程图如下:

开始
Start

GOTOs
GOTO'S


Tags: 算法lenifdef时间代表序列else
1条回答
网友
1楼 · 发布于 2024-04-27 04:01:47

这是正确的吗

def endzo(x, trough, peak, i, p, f, l, r, s, n):
    n = p + 1
    if abs(f) == 1:
        if f == 1:
            s[n] = peak
        else:
            s[n] = trough
    else:
        s[n] = (trough + peak) / 2

    return x, trough, peak, i, p, f, l, r, s, n


def maxzo(x, trough, peak, i, p, f, l, r, s, n):
    f = f + 1
    while i < len(x):
        i = i + 1
        if i == (len(x) - 1):
            x, trough, peak, i, p, f, l, r, s, n = endzo(x, trough, peak, i, p, f, l, r, s, n)
        else:
            if x[i] > peak:
                peak = x[i]
            else:
                if peak - x[i] >= r:
                    p = p + 1
                    s[p] = peak
                    trough = x[i]
                    x, trough, peak, i, p, f, l, r, s, n = minzo(x, trough, peak, i, p, f, l, r, s, n)
        return x, trough, peak, i, p, f, l, r, s, n


def minzo(x, trough, peak, i, p, f, l, r, s, n):
    f = -1
    while i < len(x):
        i = i + 1
        if i == (len(x) - 1):
            x, trough, peak, i, p, f, l, r, s, n = endzo(x, trough, peak, i, p, f, l, r, s, n)
        else:
            if x[i] < trough:
                trough = x[i]
            else:
                if x[i] - trough >= r:
                    p = p + 1
                    s[p] = trough
                    peak = x[i]
                    x, trough, peak, i, p, f, l, r, s, n = maxzo(x, trough, peak, i, p, f, l, r, s, n)
        return x, trough, peak, i, p, f, l, r, s, n

x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
trough = x[0]
peak = x[0]
i = -1
p = 0
f = 0
l = len(x) - 1
r = 1
s = []
n = 0

while i <= l:
    i = i + 1
    if i == l:
        x, trough, peak, i, p, f, l, r, s, n = endzo(x, trough, peak, i, p, f, l, r, s, n)
    else:
        if x[i] > peak:
            peak = x[i]
            if peak - trough >= r:
                s[p] = trough
                x, trough, peak, i, p, f, l, r, s, n = maxzo(x, trough, peak, i, p, f, l, r, s, n)

        else:
            if x[i] < trough:
                trough = x[i]
                if peak - trough >= r:
                    s[p] = peak
                    x, trough, peak, i, p, f, l, r, s, n = minzo(x, trough, peak, i, p, f, l, r, s, n)

print(s)

相关问题 更多 >