Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:09:18+00:00 2026-06-13T18:09:18+00:00

I know it’s a very basic question but i am also a newbie in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:09:20+00:00Added an answer on June 13, 2026 at 6:09 pm

    This should do it:

    with open('sample.txt') as sample:
        num_testcases = int(sample.readline())
        assert 1 <= num_testcases <= 5
        for testcase in range(num_testcases):
            num_elems = int(sample.readline())
            assert 1 <= num_elems <= 10000
            elems = map(int, sample.readline().split())
            assert len(elems) == num_elems
            assert all(1 <= elem <= 100000 for elem in elems)
            method_test(elems)
    

    Edit: Added validity checks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a very basic question but sometimes it happens that you
I know this is a very simple question for you experts,but please forgive me.Im
I know this is a pretty basic question, but I don't know how to
Know this might be rather basic, but I been trying to figure out how
I know that this sort of question has been asked here before, but still
i know this is a stupid question but i d'ont know how to do
I know this is an oft asked question, but I've tried some of the
I know this is kind of easy question but i cant seem to find
I know this is a stupid question, but I've looked for 45 mins now
I know very little about Agile but I wonder if there is any difference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.