代码似乎并没有在函数中的if语句之后运行

2024-04-26 21:34:58 发布

您现在位置: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

Tags: testselfheredeffunctionmergecallsort
1条回答
网友
1楼 · 发布于 2024-04-26 21:34:58

"but nothing nothing seems to run after the print "here" part"

你是基于没有其他指纹的事实吗?如果是这样,那是因为你必须打印你改变的变量。另外,没有函数返回您在函数中处理过的任何内容,而sort会对meetings变量进行变异,它无法知道何时停止调用自己,当尝试索引到meetings变量中的空列表时,它最终会抛出一个错误。甚至你对印刷品的使用也令人困惑。您首先使用print("call function"),然后使用print meetings,然后混合使用python2&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一直用一个空列表和一个更高的索引号来调用自己

  • 合并会议总是返回一个空列表

您需要测试索引是否大于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)

现在,虽然这会停止递归调用,但它仍然没有完全排序,它会根据两个元素彼此的位置对它们进行排序,但需要多次传递才能实现正确的排序。 例如:

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

因为这是一项任务,所以我让你来决定。你知道吗

相关问题 更多 >