INTEST in Python

I have done with several submissions for enormous input test in Python2.7
I tried with,
import psyco
psyco.full()
also.
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.
    CodeChef: Practical coding for everyone
    and I feel
    CodeChef: Practical coding for everyone
    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()

array=map(int,sys.stdin.readlines()) 

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.

5 Likes

Thanks back2basics. Nice suggestion!
It worked.
http://www.codechef.com/viewsolution/1496057

I don’t get it, if you simply run this CodeChef: Practical coding for everyone 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

Hello,

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 Mastering Python 3 I/O (Version 2) 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 CodeChef: Practical coding for everyone
Please help asap.