user5228835
user5228835

Reputation: 9

run time error for stock maximize ... hackerrank

I am getting a correct answer on my compiler, but I am getting a run time error on hacker rank. Solution for stock maximize problem. I am new at python, therefore I am having difficulty in removing error.Inputs are of this form

1       //no of test cases
3      //no of stocks
5 2 3 //cost of stocks

I think error is in taking input as 5 3 2 (continuous). If I am taking as

5
3
2 

(one on each line)then it is working fine. How can I fix this problem?

t=int(input())
list=[]
while t>0:

    n=int(input())

    list.clear() 
    for i in range(0,n):
        list.append(int(input()))
    sum=0
    print('hello')
    max=list[n-1]
    for i in range(n-2,-1,-1):
        if(list[i]<max):
          sum=sum+(max-list[i])
        else:
            max=list[i]
    print(sum)

    t=t-1

Upvotes: 0

Views: 390

Answers (2)

wildthing81
wildthing81

Reputation: 375

You can read the //cost of stocks like so in Python3

stockcost=[int(k) for k in input().split()]

This will create a list of stock prices

Upvotes: 1

Tan&#233; Tachyon
Tan&#233; Tachyon

Reputation: 1112

Instead of trying to read in three lines of stock costs when there is actually only one line of three space-separated costs, you need to read in that one line and split it into a list of integers, for example like this (since it looks like you're using Python 3):

stocks = list(map(int, input().split(" ")))

With your example, this would give you the list [5, 3, 2].

Upvotes: 0

Related Questions