[英]Python 3 Run-Time Error, Kattis "Putovanje"
Kattis 拒絕了四次提交“Putovanje”的 Python 3 腳本的嘗試,原因僅是“運行時錯誤”。 它在 VSCode 中與我內置的三個測試用例以及來自 Kattis 本身的示例用例一起工作得很好。 然而,這可能意味着 Kattis 使用了一組更狡猾的輸入。
在線資源將我引導到以下解決方案:“......不應該有零的返回值......”(我已經通過或者從富有成效的“答案”函數中刪除返回行,或通過分配調用 main 中的變量(即函數“解決方案”))或“...使用 try/except 塊以避免在 EOF 之后請求輸入...”(我已將其包含在我的代碼的最新版本中,這里)。 然后,我使用 Python Tutor 將更多測試用例的代碼可視化,例如 10, 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10 以確保所有循環都能完成。 順便說一句,列表方法 Fruits.remove() 的使用讓我有點困擾,因為每次連續刪除時列表長度都會縮短,但到目前為止,我的所有測試用例都沒有超出列表的容量。
我在這里錯過了什么,凱蒂斯一直在追趕?
注意:我的測試函數僅通過終端調用,通過“python putovanje.py test”——否則該函數將被忽略。
這是 Kattis 描述: https ://open.kattis.com/problems/putovanje
這是我的代碼:
# Module requests
import sys
def answer(fruits, C, N):
loop = int(0)
# marker is the desired end result
count = int(0)
sum = int(0)
marker = int(0)
it = int(0)
#loops over successive indices
for iterator2 in range(len(fruits)):
#loops over available indices
for iterator in range(len(fruits)):
if ((sum + fruits[iterator] <= C)):
sum = sum + fruits[iterator]
count+=1
# stores largest count
if (count > marker):
marker = count
count = int(0)
sum = int(0)
loop+=1
if (loop == N):
break
#advances iterator
fruits.remove(fruits[it])
# print and return maximum fruits count
print(marker)
return marker
def solution():
C = int(0)
N = int(0)
i = int(0)
elem = int(0)
# Read in total number of fruits (list length)
N = int(input())
# Read in total amount of weight (list length)
C = int(input())
fruits = []
# Read in fruit weights to list by append loop
for i in range(N):
try:
elem=int(input())
except EOFError: #stackoverflow.com
break
fruits.append(elem)
# computes desired result
marker = answer(fruits, C, N)
def test():
#local test cases
berries = [1,2,4,2,1]
mushrooms = [1, 1000, 1]
nuts = [10,9,8,7,6,5,4,3,2,1]
assert answer(berries, 5, 5) == 3
assert answer(mushrooms, 1000, 3) == 2
assert answer(nuts, 10, 10) == 4
print("All test cases passed.")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == 'test':
test()
else:
solution()
處理您的輸入時出現問題。 它一次讀取完整的輸入行,因此您必須拆分該行並將其分配給變量。
N, C = map(int, input().split())
fruits = [int(x) for x in input().split()]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.