繁体   English   中英

在函数内部的if语句之后,代码似乎没有运行

[英]code doesn't seem to run after if statement inside function

我正在关注一些在线课程并且我有这个功能sort但是在print "here"部分后似乎没有任何东西运行:

import unittest


def sort(meetings, indx):
    print("call function")
    print meetings
    firstfirst = meetings[indx][0]
    firstsecond = meetings[indx][1]
    secondfirst = meetings[indx+1][0]
    secondsecond = meetings[indx+1][1]

    first = meetings[indx]
    second = meetings[indx+1]

    print firstfirst
    print secondfirst

    if firstfirst > secondfirst:
        meetings[indx] = second
        meetings[indx+1] = first
    print "here"
    indx = index + 1
    print "meetings: "
    sort(meetings[indx:len(meetings)-1], indx)

def merge_ranges(meetings):

    # Merge meeting range

    sort(meetings, 0)

    return []


# Tests



class Test(unittest.TestCase):

    def test_meetings_overlap(self):
        actual = merge_ranges([(1, 3), (2, 4)])
        expected = [(1, 4)]
        self.assertEqual(actual, expected)

    def test_meetings_touch(self):
        actual = merge_ranges([(5, 6), (6, 8)])
        expected = [(5, 8)]
        self.assertEqual(actual, expected)

    def test_meeting_contains_other_meeting(self):
        actual = merge_ranges([(1, 8), (2, 5)])
        expected = [(1, 8)]
        self.assertEqual(actual, expected)

    def test_meetings_stay_separate(self):
        actual = merge_ranges([(1, 3), (4, 8)])
        expected = [(1, 3), (4, 8)]
        self.assertEqual(actual, expected)

    def test_multiple_merged_meetings(self):
        actual = merge_ranges([(1, 4), (2, 5), (5, 8)])
        expected = [(1, 8)]
        self.assertEqual(actual, expected)

    def test_meetings_not_sorted(self):
        actual = merge_ranges([(5, 8), (1, 4), (6, 8)])
        expected = [(1, 4), (5, 8)]
        self.assertEqual(actual, expected)

    def test_sample_input(self):
        actual = merge_ranges([(0, 1), (3, 5), (4, 8), (10, 12), (9, 10)])
        expected = [(0, 1), (3, 8), (9, 12)]
        self.assertEqual(actual, expected)


unittest.main(verbosity=2)

输出显示了这个,并且仅针对测试用例(我没有包括)抛出错误,因为这些是预期的...

call function
[(1, 8), (2, 5)]
1
2
here
call function
[(5, 8), (1, 4), (6, 8)]
5
1
here
call function
[(1, 3), (2, 4)]
1
2
here
call function
[(1, 3), (4, 8)]
1
4
here
call function
[(5, 6), (6, 8)]
5
6
here
call function
[(1, 4), (2, 5), (5, 8)]
1
2
here
call function
[(0, 1), (3, 5), (4, 8), (10, 12), (9, 10)]
0
3
here

“但是在印刷品”这里“部分之后似乎没有任何东西可以运行

你是基于没有其他东西打印的事实吗? 如果是这样,因为你必须打印你改变的变量。 此外,你的函数没有返回你在函数中工作的任何东西,而sort变异会议变量,它无法知道何时停止调用自身,它只会在尝试索引到一个空列表时最终抛出错误在会议变量中。 即使你使用印刷品也令人困惑。 您使用print("call function") ,然后print meetings然后混合python 2和3打印语法。

但是,让我们来看看你问题的核心。

def sort(meetings, indx):
    print("call function")
    print meetings
    # eventually meetings will be an empty list and meetings[indx] 
    # will throw an IndexError
    firstfirst = meetings[indx][0]
    firstsecond = meetings[indx][1]
    secondfirst = meetings[indx+1][0]
    secondsecond = meetings[indx+1][1]

    first = meetings[indx]
    second = meetings[indx+1]

    print firstfirst
    print secondfirst

    if firstfirst > secondfirst:
        meetings[indx] = second
        meetings[indx+1] = first
    # "here" is printed
    print "here"  
    # you alter the indx variable but do not print it
    indx = index + 1  
    # "meetings:" is printed but nothing else is printed below it
    print "meetings: "  
    # sort calls itself without any condition to stop calling itself 
    # and which will eventually have the indx variable exceed the 
    # meetings length in the call:
    #     meetings[indx:len(meetings)-1]
    sort(meetings[indx:len(meetings)-1], indx)  
    # nothing is returned here and sort does not mutate the object in 
    # any way that I could see that would cause sort to stop 
    # calling itself

def merge_ranges(meetings):

    # Merge meeting range

    sort(meetings, 0)

    return []  # <- this empty list is always returned no matter what
  • sort不返回任何东西,如果你只是改变一些东西,这不是一个大问题
  • sort以递归方式调用自身,直到它超过递归限制,没有任何东西可以告诉它停止调用自身

让我们假设会议就是这个列表

meetings = [(0, 1), (3, 5)]
meetings[5:] # ==> [] will always return an empty list when indx exceed meetings length

这意味着sort继续使用空列表和更高的索引号调用自身

  • merge_meetings始终返回一个空列表

您需要测试索引大于len(meetings)

建议:假设python 3

def sort(meetings, indx):
    print("call function")
    print(meetings)
    first = meetings[indx]
    second = meetings[indx+1]
    firstfirst = first[0]
    firstsecond = first[1]
    secondfirst = second[0]
    secondsecond = second[1]

    print(firstfirst)
    print(secondfirst)

    if firstfirst > secondfirst:
        meetings[indx] = second
        meetings[indx+1] = first
    indx = index + 1
    print("meetings: ", meetings)
    if len(meetings) - 1 > indx:
        sort(meetings[indx:], indx)

现在,虽然它负责停止递归调用它仍然没有完全排序,它相对于它们的位置相互排序2个元素,但它需要几次传递才能实现正确的排序。 例如:

In [1]: a = [(5,3), (0,2), (4,1), (1,1)]
In [2]: sort(a, 0)
call function
[(0, 2), (5, 3), (4, 1), (1, 1)]
0
5
meetings:  [(0, 2), (5, 3), (4, 1), (1, 1)]
call function
[(5, 3), (4, 1), (1, 1)]
4
1
meetings:  [(5, 3), (1, 1), (4, 1)]

In [3]: a
Out[3]: [(0, 2), (5, 3), (4, 1), (1, 1)]

我会把这件事告诉你,因为这是一项任务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM