I know it’s a very basic question but i am also a newbie in python environment. I am writing my first program (Data structure problem) where i need to read some input test cases.
Input:
The first line contains the number of test cases T. T test cases follow.
The first line for each case contains N, the number of elements to be sorted.
The next line contains N integers a[1],a[2]...,a[N].
Constraints:
1 <= T <= 5
1 <= N <= 100000
1 <= a[i] <= 1000000
Sample Input:
2
5
1 1 1 2 2
5
2 1 3 1 2
I wrote a following program to read the above input from a file but i am sure that this is not the best way to do it because it contains a lot of if-else loop and for loop which will really sucks at the large inputs.
sample = open('sample.txt')
first = sample.readline()
if len(first) > 5 or len(first) <1:
print "Not correct input";
else:
test = sample.readline
for x in range(0,len(first)):
second = sample.readline()
if len(second) >100000 or len(second) < 1:
print "wrong input";
else:
third = list()
for y in range(0, len(third)):
third.append(sample.readline()[:1])
method_test(third) #calling a method for each sample input
Please suggest me the best solution.
This should do it:
Edit: Added validity checks.