Kattis has rejected four attempts to submit a Python 3 script for "Putovanje" with only "Run Time Error" as the reason. It works just fine in VSCode with the three test cases I built into it, as well as the sample cases from Kattis itself. However, this may mean Kattis uses a sneakier set of inputs.
Online resources direct me to the following solutions: "...shouldn't have a return value of zero..." (which I've corrected by alternatively removing the return line from the fruitful "answer" function, or by assigning the call to a variable in the main (ie function "solution")) or "...use a try/except block to avoid requesting inputs after EOF..." (I've included it in the newest versions of my code, here). I then used Python Tutor to visualize the code on a few more test cases, such as 10, 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10 to ensure all the loops go to completion. BTW, the use of the list method fruits.remove() bothers me a bit, since the list length shortens upon each successive removal, but so far all my test cases have not exceeded the capacity of the list.
What am I missing here that Kattis keeps catching?
Note: My test function is called only through the terminal, via "python putovanje.py test" -- this function is ignored otherwise.
Here is the Kattis description: https://open.kattis.com/problems/putovanje
And here is my code:
# 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()
There is a problem handling your input. It's reading a complete input line at once, so you have to split that line and assign it to the variables.
N, C = map(int, input().split())
fruits = [int(x) for x in input().split()]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.