Program passes all other test cases except the last one. How to fix last test case?

时间:2019-01-15 18:26:54

标签: python python-3.x

I need to find the maximum recurrence in a number and I seem to have figured out all other test cases besides testcase on line 31 in test_sweep.

I have tried to rewrite this but I am fairly certain my code should work.

def max_run(l: list) -> list:
    if len(l) <= 0:
        return 0
    if len(l) == 1:
        return 1

    bal = 0
    maxbal = 0
    compare_item = l[0]
    for item in l:
        if item == compare_item:
            bal = bal + 1
        else:
            compare_item = item
            if bal >= maxbal:
                maxbal = bal
                bal = 1
    return maxbal

class TestMaxRun(unittest.TestCase):
    def test_run(self):
        before = [1, 1, 3, 3, 3, 5]
        saved = before.copy()
        self.assertEqual(sweep.max_run(before), 3)
        self.assertEqual(before, saved)
        self.assertEqual(sweep.max_run([]), 0)
        self.assertEqual(sweep.max_run([42]), 1)
        self.assertEqual(sweep.max_run([1, 2, 3]), 1)
        self.assertEqual(sweep.max_run([3, 3, 3, 2, 3]), 3)
        self.assertEqual(sweep.max_run([1, 2, 2, 3]), 2)
        self.assertEqual(sweep.max_run([3, 4, 5, 5, 5]), 3)

Should pass all errors. max_run fails on line 31.

1 个答案:

答案 0 :(得分:0)

您的问题是,您没有正确处理输入结尾处的长序列。在最后一个测试用例中,三个5的序列将被忽略,因为您无需检查它是否比之前的最长序列(长度为1)更长。

您需要在readarray num1 < file1 | awk -F'[ ]' '{print $1}' while read -r $num1; do grep "$num1" file2 echo "$num1" | awk -F':' '{print $1,$5}' done < file3.txt 之前的max_run循环外重复此代码:

return maxbal