Search code examples
pythonraw-input

Unable to access raw_input() data - Learning Python on Interviewstreet


I'm attempting to move over to Python after having worked in Matlab for the past few months. This is probably a simple question, but I haven't been able to find it in searching StackOverflow or Google yet.

The InterviewStreet problem I'm working on is pretty simple. It's an insertion sort question, checking how many times different numbers need to be switched in lists. They give us a raw_input() in the following format:

2

5

1 1 1 2 2

5

2 1 3 1 2

The first line is how many lists I need to sort, all subsequent even lines are the quantity of numbers in a list I need to sort, and the following odd lines are the actual lists that need sorting.

I am stuck waaay at the beginning. The best I've done is

STDOUT = raw_input()

print STDOUT

which for some reason only gives the first integer in all test cases (for this one, it's 2).

This is probably obvious to you already, but how do I access the rest of the numbers with raw_input()?

Thanks!

P.S. Please don't help me with the rest of the problem, I actually want to try and solve it myself :)


Solution

  • You need to call raw_input() once for each line, like so:

    N = int(raw_input())    # first line is number of lists
    
    for i in xrange(N):
        num_in_list = int(raw_input())  # read the number of items in list N
        list = raw_input().split()      # read list N
    
        print num_in_list, list         # print list N
    

    Should output the following:

    5 ['1', '1', '1', '2', '2']
    5 ['2', '1', '3', '1', '2']