Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; '(python': 0.05; 'tests,': 0.07; 'try:': 0.07; 'brace': 0.09; 'imply': 0.09; 'input,': 0.09; 'notation': 0.09; 'finished': 0.15; '4:59': 0.16; 'exceed.': 0.16; 'expression.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'pentium': 0.16; 'reason.': 0.16; 'seconds.': 0.16; 'stdin': 0.16; 'subject:Regular': 0.16; 'subject:expression': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'trying': 0.21; 'subject:problem': 0.22; 'sets': 0.23; 'this:': 0.23; "i've": 0.23; 'idea': 0.24; 'tried': 0.25; 'least': 0.25; 'header:In- Reply-To:1': 0.25; 'wrote': 0.26; 'am,': 0.27; '2.6': 0.27; 'message-id:@mail.gmail.com': 0.27; 'regular': 0.27; 'lines': 0.28; 'chris': 0.28; 'run': 0.28; 'source': 0.29; 'maybe': 0.29; 'query': 0.30; 'seconds': 0.30; 'code': 0.31; 'could': 0.32; 'getting': 0.33; '11,': 0.33; 'doubt': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'something': 0.35; 'really': 0.36; 'but': 0.36; 'test': 0.36; 'does': 0.37; 'why': 0.37; 'drop': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'sure': 0.38; 'description': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'end': 0.40; 'your': 0.60; 'most': 0.61; 'solve': 0.62; 'maximum': 0.63; 'limit': 0.65; 'results': 0.65; 'taking': 0.65; '100': 0.78; '2013': 0.84; 'limit?': 0.84; 'windows)': 0.84; 'faster.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=MJ91t65HqJ7HDEmeSzUVli+YgviGkHd5b5xGnYvR1BY=; b=RY3WiRkyBcCSXAbErnsBehTuB8ESlx1gEogV+ykanEpXJP4NubG72650VUyyuSOHJQ 3k9VXBTLRo+ulMzhP0a0DJSuf5TfObfpwUiU8afK0hGv9zLlpcJx/Kpqj4l41BWJf/Zv fKjK7Nd7PflHumLuJs3O+SGLzKWMkNCSDDZ2vBoGRuCcL2lcmBBXpgAYtMkLQmp45l7k eJg8iv63VQjenqaxZCekS3RH/VYpJgDuDT0fxAeHmApfngqMuKUQUQpSp6IN4Wb2fxF5 O9wKRR/cE/K+j1Fp5qZwpy1i8eksOdv/5U9i7w2dL+YhU1l9qQsoVW+SbRHNSNBuOnk0 Q8rA== MIME-Version: 1.0 X-Received: by 10.52.22.194 with SMTP id g2mr3270639vdf.91.1362938924699; Sun, 10 Mar 2013 11:08:44 -0700 (PDT) In-Reply-To: References: Date: Mon, 11 Mar 2013 05:08:44 +1100 Subject: Re: Regular expression problem From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362938932 news.xs4all.nl 6879 [2001:888:2000:d::a6]:50703 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41028 On Mon, Mar 11, 2013 at 4:59 AM, Chris Angelico wrote: > On Mon, Mar 11, 2013 at 4:42 AM, mukesh tiwari > wrote: >> I am trying to solve this problem[1] using regular expression. I wrote this code but I am getting time limit exceed. Could some one please tell me how to make this code run faster. > > What is the time limit? I just tried it (Python 2.6 under Windows) and > it finished in a humanly-immeasurable amount of time. Are you sure > that STDIN (eg raw_input()) is where your test data is coming from? Oops, reading comprehension fail. Time limit is 3s on a Pentium III. I've no idea how long your code will take on that hardware, but I doubt that it's taking three seconds. So my query regarding source of test data still stands. Can you put together an uber-simple test program that just echoes the lines of input, to make sure it really is coming off stdin? The problem description certainly does seem to imply stdin, but I can't see why your code would take three seconds unless it's stalling for some reason. Though perhaps on a P3 with the maximum 100 tests, maybe that could take a while... Something to try: Since you're using re.search(), see if you can drop the complemented sets at the beginning [^~!@#$%^&*()<>?,.]* and end [^~!@#$%^&*()<>?,.a-zA-Z0-9]* - they're going to be slow to process. Also, you can simplify this: [a-zA-Z0-9][a-zA-Z0-9._][a-zA-Z0-9._][a-zA-Z0-9._][a-zA-Z0-9._][a-zA-Z0-9._]* to this: [a-zA-Z0-9][a-zA-Z0-9._]{4,} The brace notation means "at least 4, at most infinity". Try those out and see if you still get the results you want. ChrisA