You are not logged in. Please login at www.codechef.com to post your questions!

×

TLE . How can i optimize this better?

This is my code in python for the Enormous Input Test problem :

n,k=map(int, raw_input().split())
count=0
while n>0 :
    x=int(raw_input())
    if x%k==0 :
        count=count+1
    n-=1
print count

How can i optimize this in a better way or what am doing wrong? i'm facing this problem in several of my python codes.Is there any way i can overcome this TLE problem?

asked 22 Jul '14, 22:43

akarsh_hegde's gravatar image

1★akarsh_hegde
31113
accept rate: 0%

edited 22 Jul '14, 22:44


13

@akarsh_hedge try importing the sys module for faster input output , it has always helped me , hope it helps you too .

You would clearly see the time difference on using sys .

Happy Coding :)

link

answered 23 Jul '14, 01:42

its_pheonix's gravatar image

4★its_pheonix
2.3k62021
accept rate: 11%

import sys module and use sys.stdin.readline().strip() for raw_input() and use sys.stdout.write() for print. :)

link

answered 23 Jul '14, 00:25

vaibhav9518s's gravatar image

2★vaibhav9518s
462
accept rate: 0%

thank u . will try that right away! :)

(24 Jul '14, 18:48) akarsh_hegde1★

I agree with both @its_pheonix and @vaibhav9518s , importing sys will surely help you .

link

answered 23 Jul '14, 01:45

proxy_s's gravatar image

3★proxy_s
3113
accept rate: 0%

I am having same problem. I have tried both input() and sys.stdin.readline(). still TLE. This is also keeping me from earning full points on some competition problems as I believe my algorithms are often good save for i/o issues. This is frustrating as it seems as if Python submissions are being accepted they should be accommodated... otherwise wby accept them...

Here is my code for enormous input test:

import sys n, k = [int(i) for i in sys.stdin.readline().strip().split()] c = 0 for i in range(n): if int(sys.stdin.readline().strip()) % k == 0: c += 1 print(c)

link

answered 04 Jan '15, 12:52

tao_of_coding's gravatar image

3★tao_of_coding
12
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×727
×528

question asked: 22 Jul '14, 22:43

question was seen: 4,520 times

last updated: 04 Jan '15, 12:52