INTEST in Python

I have done with several submissions for enormous input test in Python2.7
I tried with,
import psyco
Here are some approches I used,

  1. I taken all input at once,and then find final answer.
  2. Take a no at once and then calculate answer after each input.
    I am wondering if someone have solved this problem in Python ,if yes how or what can be efficient way.
    I am python programmer not interested in other languages right now.
    here are solutions i tried.,tripples
    and I feel
    this would be efficient.

i’ve done it in a similar way here.
hope it helps. :slight_smile:

PS: you should use raw_input() instead of input(). see Python doc to understand why.

1 Like

I don’t think you’re program will pass if you take the input one by one.
Instead use the function map()


Now array will have all of the input into one list. But hey, we don’t want the first line to be mapped into the array… so read the first line separately.

n,k = map(int,sys.stdin.readline().split(" "))        

Then use a for loop to iterate over the elements of array and print the answer!

Note : sys.stdin.readlines() is used to read multiple lines till EOF while sys.stdin.readline() can read only a single line.


Thanks back2basics. Nice suggestion!
It worked.

I don’t get it, if you simply run this as Python 3 code it will give a TLE, but if you, just change the print(ans) to print ans and submit it as Python2 solution it is accepted… this is not done… o.O


I am doing the same problem using Python 3.2.3.
Since I don’t find any method to read multiple lines from standard input in one go, I need some way to get it done.

Can you suggest something efficient?

Following is the code I submitted:
My Solution: Exceeding Time Limit

Python 3 completely rewrote the I/O system, requiring more precision in handling I/O data. See for an overview.

I am trying to learn handling inputs in python, when number of test cases is not given. Copied this exact solution of yours but got a TLE, though your’s is AC. I don’t understand this?
My submission is here
Please help asap.