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

时间:2018-06-03 21:00:29

标签: python

我正在关注一些在线课程并且我有这个功能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

1 个答案:

答案 0 :(得分:3)

  

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

你是基于没有其他东西打印的事实吗?如果是这样,因为你必须打印你改变的变量。此外,你的函数没有返回你在函数中工作的任何东西,而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递归调用自身,直到它超过递归限制,没有什么可以告诉它停止调用自己

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

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

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