rekurencyjna konkatenacja nie działa

0

Dzień dobry.

Nie jestem biegły w pythonie. Mam funkcję:

def test(stri, i):
    stri += "a"
    if i > 0:
        test(stri, i - 1)
    return stri


print(test("abc", 10)) #zwraca 'abca', nie wiem czemu

Na chłopski rozum powinno mi to zwrócić abcaaaaaaaaaa a zwraca abca. Dlaczego? Skoro stri jest przekazywane przez referencję, to powinno chyba modyfikować w rekurencyjnych wywołaniach zmienną stri. Co jest nie tak?

0

To teraz właściwy problem:

def splitString_In(formula, separators, i): #["<=", "=="]
    formula = formula.split(separators[i - 1])
    if i > 1:
        for idx in range(len(formula)):
            return splitString_In(formula[idx], separators, i - 1)
    return formula


def splitString(formula, separators):
    if isinstance(formula, str) and len(separators) > 0:
        return splitString_In(formula, separators, len(separators))


print(splitString("1 + 2 < 4 = 1 + 3", ["<", "="])) #zwraca ['1 + 2 ', ' 4 '], zamiast [['1 + 2 '], [[' 4 '], [' 1 + 3']]]

Chcę opakować wzór w listy, rozdzielając każdy string przez operator porównania. Niestety nie działa. Mam wersję iteracyjną, która działa, ale dodawanie kolejnego operatora przyprawia mnie o zawrót głowy. Proszę o pomoc.

Tu wersja iteracyjna:

qcs = qcs.split("==")
for x1 in range(len(qcs)):
    qcs[x1] = qcs[x1].split("<=")
    for x2 in range(len(qcs[x1])):
        qcs[x1][x2] = qcs[x1][x2].split(">=")
        for x3 in range(len(qcs[x1][x2])):
            qcs[x1][x2][x3] = qcs[x1][x2][x3].split("!=")
            for x4 in range(len(qcs[x1][x2][x3])):
                qcs[x1][x2][x3][x4] = qcs[x1][x2][x3][x4].split("=")
                for x5 in range(len(qcs[x1][x2][x3][x4])):
                    qcs[x1][x2][x3][x4][x5] = qcs[x1][x2][x3][x4][x5].split("<")
                    for x6 in range(len(qcs[x1][x2][x3][x4][x5])):
                        qcs[x1][x2][x3][x4][x5][x6] = qcs[x1][x2][x3][x4][x5][x6].split(">")

Proszę o pomoc. Dzięki!

1

Matko przenajświętsza, co to jest?

qcs = qcs.split("==")
for x1 in range(len(qcs)):
    qcs[x1] = qcs[x1].split("<=")
    for x2 in range(len(qcs[x1])):
        qcs[x1][x2] = qcs[x1][x2].split(">=")
        for x3 in range(len(qcs[x1][x2])):
            qcs[x1][x2][x3] = qcs[x1][x2][x3].split("!=")
            for x4 in range(len(qcs[x1][x2][x3])):
                qcs[x1][x2][x3][x4] = qcs[x1][x2][x3][x4].split("=")
                for x5 in range(len(qcs[x1][x2][x3][x4])):
                    qcs[x1][x2][x3][x4][x5] = qcs[x1][x2][x3][x4][x5].split("<")
                    for x6 in range(len(qcs[x1][x2][x3][x4][x5])):
                        qcs[x1][x2][x3][x4][x5][x6] = qcs[x1][x2][x3][x4][x5][x6].split(">")
0

Krótko mówiąc -- zainteresuj się parsowaniem wyrażeń arytmetycznych. Google: arithmetic parsing in python -- albo coś takiego...

0

@Eldorad O.: Ładne

0

Popełniasz błąd powielany przez początkujących programistów

Przeczytaj książkę wujka Boba Czysty Kod
Kod jest bardzo nieczytelny.

0

Dziękuję! Poprawione!

def splitString_In(formula, separators, i): #["<=", "=="]
    formula = formula.split(separators[i - 1])
    if i > 1:
        for idx in range(len(formula)):
            formula[idx] = splitString_In(formula[idx], separators, i - 1)
    return formula


def splitString(formula, separators):
    if isinstance(formula, str) and len(separators) > 0:
        return splitString_In(formula, separators, len(separators))

Dzięki @Delor @szatkus
M.

1 użytkowników online, w tym zalogowanych: 0, gości: 1