【问题标题】:Recursive function to return the line between ancestor and successor递归函数返回祖先和后继者之间的界线
【发布时间】:2019-12-04 03:13:30
【问题描述】:

我需要编写一个递归函数,如果存在,则返回祖先和后继者之间的界线。该函数将祖先、继任者和孩子及其父母的字典作为参数。我目前拥有的代码在某些情况下有效,但并非全部有效,所以它肯定还缺少一些东西。我目前尝试使用列表来添加人名,但我需要以某种方式将它们一一打印出来,结果最终。这就是我所拥有的和示例。前 4 个有效,但最后 3 个无效。在看第一个例子的时候,基本上它需要返回的是

Lucia
Adele
Sam
John
table = {'John': ('Sam', 'Mary'),
            'Mary': ('Konstantin', 'Marie'),
            'Rita': ('Sam', 'Mary'),
            'Simon': ('Sam', 'Mary'),
            'Marie': ('Carl', 'Liz'),
            'Sam': ('Joseph', 'Adele'),
            'Adele': ('Johannes', 'Lucia'),
            'Konstantin': ('Viktor', 'Jelena'),
            'Joseph': ('August', 'Emma'),
            'Viktor': ('Nikolai', 'Maria')}

def return_line(ancestor, successor, table, line = []):
    if successor in table:
        parents = table[successor]
        if ancestor in parents:
            if ancestor not in line:
                line.append(ancestor)
            if successor not in line:
                line.append(successor)         
        else:
            successor_1 = parents[0]
            successor_2 = parents[1]
            return_line(ancestor, successor_1, table)
            if line == []:
                return_line(ancestor, successor_2, table)
            else:
                ancestor = line[-1]
                return_line(ancestor, successor, table)            
    else:
        return
    if line != []:
        return line
    else:
        return("Doesn't exist")

print(return_line("Lucia", "John", table))
print(return_line("Marie", "Adele", table))
print(return_line("Maria", "Mary", table))
print(return_line("Emma", "Rita", table))

print(return_line("Nikolai", "John", table))
print(return_line("Jelena", "John", table))
print(return_line("Carl", "Mary", table))

【问题讨论】:

    标签: python recursion


    【解决方案1】:

    一个更简单的实现,无需传递可变参数:

    def line(ancestor, successor, table):
        if ancestor == successor:
            return [successor]
        if successor not in table:
            return  # no line exists
        for parent in table[successor]:
            anc_line = line(ancestor, parent, table)
            if anc_line:
                return anc_line + [successor] 
    
    line("Lucia", "John", table)
    # ['Lucia', 'Adele', 'Sam', 'John']
    line("Marie", "Adele", table)
    # None
    line("Maria", "Mary", table)
    # ['Maria', 'Viktor', 'Konstantin', 'Mary']
    line("Emma", "Rita", table)
    # ['Emma', 'Joseph', 'Sam', 'Rita']
    line("Nikolai", "John", table)
    # ['Nikolai', 'Viktor', 'Konstantin', 'Mary', 'John']
    line("Jelena", "John", table)
    # ['Jelena', 'Konstantin', 'Mary', 'John']
    line("Carl", "Mary", table)
    # ['Carl', 'Marie', 'Mary']
    

    基本情况:
    1.ancestor == successor:一人构成单例线
    2.successor not in table:没有这个人父母的更多信息,所以没有必要再去寻找了

    递归:
    1. 找到从祖先到任一父的线
    2.如果存在,追加手头的人

    【讨论】:

    • 干杯!但是我怎么能不返回列表,而是在新行上单独打印出名称,因为这是我实际需要使用此函数返回的内容?我只是试图用一个列表来解决它。
    • 如果该行不存在,它应该打印出“不存在”。
    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 2014-12-06
    • 2010-11-24
    • 2017-12-11
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多