Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'anyway.': 0.05; 'remaining': 0.07; 'grid': 0.09; 'subject:How': 0.10; 'cc:addr :python-list': 0.11; 'def': 0.12; 'innermost': 0.16; 'iteration': 0.16; 'loops': 0.16; 'pairs': 0.16; 'subject:make': 0.16; 'version".': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'meant': 0.20; 'cc:addr:python.org': 0.22; 'sorry,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'message-id:@mail.gmail.com': 0.30; 'keys': 0.31; 'probably': 0.32; 'becomes': 0.33; 'test': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'false': 0.36; 'should': 0.36; 'e.g.': 0.38; 'previous': 0.38; 'structure': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'remove': 0.60; 'referred': 0.60; 'skip:z 20': 0.60; 'success': 0.61; "you're": 0.61; 'july': 0.63; 'skip:+ 10': 0.65; 'subject:this': 0.83; 'oscar': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=Zs93quVUZKF6mnC2qFoDCshcOyDmfeQXSWs3kGH3XUg=; b=fFWKnA5vNDxvrkrhJuINwQcNkmVoJOor20HmjxhVFBrVQujGyi6pWpNblWeXs90SKL Q04caHk9JZbghDyEWZsFYItzM3LbUbE9SGhfQKEK24v1lDpIt/CY0jMvs2YbAjAqbVpY ovZZRZMatQ4dzyu/prm6nSuargo6DHZDsgtD4HTZ3jbQ8hG2fSdLpYG5rXwEfzogIxwv x8fVqgtjoWY8tUuQ+4lex7qr9e3Un1hdv+WqFoTPfPue/j98Yk5Qs9GGSZ31nLGMKemZ 4QRyJChxQ+ez+ZkIabPbjvC92DQJCtn1wZ8jjhfNCAz0abmyj9+KQ1VP7+YNeEdTgOkW /buw== X-Received: by 10.58.207.103 with SMTP id lv7mr7154404vec.33.1373038743287; Fri, 05 Jul 2013 08:39:03 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Oscar Benjamin Date: Fri, 5 Jul 2013 16:38:43 +0100 Subject: Re: How to make this faster To: Helmut Jarausch Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 85 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373038751 news.xs4all.nl 15894 [2001:888:2000:d::a6]:60843 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49996 On 5 July 2013 16:17, Helmut Jarausch wrote: > > I've tried the following version > > def find_good_cell() : > Best= None > minPoss= 10 > for r,c in Grid : > if Grid[(r,c)] > 0 : continue Sorry, I think what I meant was that you should have a structure called e.g. Remaining which is the set of all (r, c) pairs that you want to loop over here. Then there's no need to check on each iteration whether or not Grid[(r, c)] > 0. When I said "sparse" I meant that you don't need to set keys in Grid unless you actually have a value there so the test "Grid[(r, c)] > 0" would look like "(r, c) in Grid". Remaining is the set of all (r, c) pairs not in Grid that you update incrementally with .add() and .remove(). Then this for r,c in Grid : if Grid[(r,c)] > 0 : continue becomes for r, c in Remaining: > Sq_No= (r//3)*3+c//3 > Possibilities= 9-len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) > if ( Possibilities < minPoss ) : > minPoss= Possibilities > Best= (r,c) > > if minPoss == 0 : Best=(-1,-1) > return Best > > All_digits= set((1,2,3,4,5,6,7,8,9)) All_digits= set(range(1, 10)) or All_digits = {1,2,3,4,5,6,7,8,9} > > def Solve(R_Cells) : > if R_Cells == 0 : > print("\n\n++++++++++ S o l u t i o n ++++++++++\n") > Print_Grid() > return True > > r,c= find_good_cell() > if r < 0 : return False > Sq_No= (r//3)*3+c//3 > > for d in All_digits - (Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) : > # put d into Grid > Grid[(r,c)]= d > Row_Digits[r].add(d) > Col_Digits[c].add(d) > Sqr_Digits[Sq_No].add(d) > > Success= Solve(R_Cells-1) > > # remove d again > Grid[(r,c)]= 0 > Row_Digits[r].remove(d) > Col_Digits[c].remove(d) > Sqr_Digits[Sq_No].remove(d) > > if Success : > Zuege.append((d,r,c)) > return True > > return False > > which turns out to be as fast as the previous "dictionary only version". > Probably, set.remove is a bit slow No it's not and you're not using it in your innermost loops anyway. Probably the loop I referred to isn't your bottleneck. Oscar