Как я могу преобразовать эти операторы GOTO потоковой диаграммы в Python? - PullRequest
0 голосов
/ 23 апреля 2020

У меня есть некоторые блок-схемы, представляющие алгоритм поиска пиков и впадин временного ряда.

Я пытался преобразовать их в 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

1 Ответ

0 голосов
/ 23 апреля 2020

Это правильно?

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)
...