Reputation: 331
So I'm trying to solve the following problem from the UVa online judge: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2864
I've written the following code in Python:
t = int(input())
for i in range(t):
highs = 0
lows = 0
walls = int(input())
heights = [0]*50
for h in range(walls):
heights[h] = (int(input()))
for j in range(1, walls):
if (heights[j] < heights[j - 1]):
highs += 1
elif (heights[j] > heights[j - 1]):
lows += 1
print("Case %d: %d %d" % (i + 1, highs, lows))
exit(0)
Every time I try my code with different test cases I get the expected output; it works perfectly fine in my side but when I submit it I keep on getting a Runtime Error. I'm desperate now as I have tried a million things and nothing works. Please, help.
Upvotes: 0
Views: 2368
Reputation: 228
As suggested the Runtime Error is in this part of your code:
for h in range(walls):
heights[h] = (int(input()))
that you can change for
heights = [int(i) for i in input().split()]
and omit the line
heights = [0]*50
Another thing that I found is that you are counting in a contrary manner lows and highs. You should do:
if (heights[j] < heights[j - 1]):
lows += 1
elif (heights[j] > heights[j - 1]):
highs += 1
That should work. Hope you get the solution Accepted ;)
Upvotes: 0
Reputation: 56694
I think the error is here:
for h in range(walls):
heights[h] = (int(input()))
input()
reads a line, thenint()
tries to convert the line to an integer. But "1 4 2 2 3 5 3 4"
cannot be converted to an integer, and if you read 8 lines you will probably run out of input.
Instead, try
heights = [int(i) for i in input().split()]
which should return [1, 4, 2, 2, 3, 5, 3, 4]
.
Upvotes: 1