簡體   English   中英

循環從不在遞歸函數內完成

[英]Loop Never Completes Within Recursive Function

我正在構建一個程序來將括號恢復為句子,使它們成為格式良好的公式(句子邏輯中的WFF)。 例如,

    - 句子a是WFF。
    - 句子a > b只有1種方法可以修復括號,使其成為WFF,即(a > b)
    - 句子a > b > c有2種方法可以修復括號以使其成為WFF - ((a > b) > c)(a > (b > c))
等等...

這個算法有一個迭代和遞歸元素

# returns number of connectives
def numConnectives(wff):
    count = 0
    for c in wff:
        if c == connectives:
            count += 1
    return count
def rec(wff):
    result = []
    ind = []                            # list storing indexes of connectives used
    if len(wff) == 1:
        return wff
    else:
        for i in range(numConnectives(wff)):
            opIndex = findConnective(wff, ind)          # index where the operator is at

            right   = createRight(wff, opIndex)     # right formula
                                                    # the first time it goes through, right is b>c
                                                    # then right is c
            left    = createLeft(wff, opIndex)      # left formula
                                                    # left is a
                                                    # then it is b
            return "(" + rec(left) + wff[opIndex] + rec(right) + ")"
 print(rec("a>b>c"))
 def rec(wff): result = [] ind = [] # list storing indexes of connectives used if len(wff) == 1: return wff else: for i in range(numConnectives(wff)): opIndex = findConnective(wff, ind) # index where the operator is at right = createRight(wff, opIndex) # right formula # the first time it goes through, right is b>c # then right is c left = createLeft(wff, opIndex) # left formula # left is a # then it is b return "(" + rec(left) + wff[opIndex] + rec(right) + ")" 
  print(rec("a>b>c")) 

我的輸出是(a>(b>c)) ,它應該是(a>(b>c)) AND ((a>b)>c) 發生這種情況是因為遞歸函數內部的循環從不選擇第二個運算符來執行遞歸調用。 當return語句在for循環之外時,輸出為((a>b)>c)

我如何使它功能遍歷所有操作符(也就是每個函數調用執行整個循環)

盡管rec() for循環的return是特定問題,但我認為整體問題是你使問題變得比它需要的更難。 你還在你的處理是不一致的connectives ,有時它的字符,集合range(len(connectives))有時它的單個字符, wff[i] == connectives[j] 這是我對代碼的簡化:

connectives = {'>'}

def findConnectives(wff):
    ''' returns index of wff '''

    if wff is None or len(wff) <= 1:
        yield -1  # it's an atomic
    else:
        for i, character in enumerate(wff):  # looping through all chars in wff
            if character in connectives:  # if the wff contains the connective
                yield i

def createLeft(wff, opIndex):

    ''' returns what's on left of operator '''

    return wff[:opIndex]

def createRight(wff, opIndex):

    ''' returns what's on right of operator '''

    return wff[opIndex + 1:]

def rec(wff):
    if len(wff) == 1:
        return [wff]

    result = []

    for opIndex in findConnectives(wff):
        if opIndex == -1:
            break

        left = createLeft(wff, opIndex) # left formula

        right = createRight(wff, opIndex)  # right formula

        for left_hand in rec(left):
            for right_hand in rec(right):
                result.append("(" + left_hand + wff[opIndex] + right_hand + ")")

    return result

print(rec("a>b>c"))

OUTPUT

% python3 test.py
['(a>(b>c))', '((a>b)>c)']
%

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM