簡體   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