Question

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 :)

Was it helpful?

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']

OTHER TIPS

I was working on the same problem and here is the solution for raw_input():

num_testcases = int(raw_input())
assert 1 <= num_testcases <= 5
for testcase in range(num_testcases):
    num_elems = int(raw_input())
    assert 1 <= num_elems <= 100000
    elems = map(int, raw_input().split())
    assert all(map(lambda x: 1 <= x <= 1000000, elems))
    insertion_sort(elems)

Hope it will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top