I have a problem with the following specification:
Input:
First line contains an integer N , the number of element in the given sequnce.
Then follows N integers A1, A2…. An, Ai is ith element of the given sequence. These numbers may be either space separated or newline separated.
How can I handle an input like that?
I tried the following,but it only works for space separated elements.When used with newline separated elements the grader throws “time exceeded”
import sys
counter=0
A=[]
for line in sys.stdin:
if counter!=0:
A+=[int(Ai) for Ai in line.split()]
else:
N=int(line)
counter+=1
Assuming that you’re reading from a file (
sys.stdinis also a file):Hope this helps