Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; ';-)': 0.03; 'else:': 0.03; 'subject:: [': 0.04; 'algorithm': 0.04; 'pop': 0.05; '(so': 0.07; 'column': 0.07; 'debugging': 0.07; '[1,': 0.09; 'executed': 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'random': 0.14; '...,': 0.16; 'columns': 0.16; 'first)': 0.16; 'jumps': 0.16; 'once.': 0.16; 'out)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'repetition': 0.16; 'repetitions': 0.16; 'script,': 0.16; 'tried:': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'subject:] ': 0.20; 'import': 0.22; 'header:User-Agent:1': 0.23; 'fine': 0.24; 'pass': 0.26; 'post': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'function': 0.29; 'rest': 0.29; 'array': 0.29; "doesn't": 0.30; 'besides': 0.30; "i'm": 0.30; 'work.': 0.31; '(my': 0.31; 'provided,': 0.31; 'position.': 0.33; 'maybe': 0.34; 'could': 0.34; 'problem': 0.35; 'board': 0.35; 'but': 0.35; 'there': 0.35; 'everyone.': 0.36; 'false': 0.36; 'next': 0.36; "i'll": 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'list': 0.37; 'project': 0.37; 'sometimes': 0.38; 'checks': 0.38; 'filled': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'previous': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'future': 0.60; 'number,': 0.60; 'solve': 0.60; 'tell': 0.60; 'forum': 0.61; 'numbers': 0.61; 'took': 0.61; 'received:173': 0.61; 'first': 0.61; 'myself': 0.63; 'today': 0.64; 'more': 0.64; 'different': 0.65; 'hours': 0.66; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: [newbie] Recursive algorithm - review Date: Fri, 03 Jan 2014 20:47:16 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: 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: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1388800051 news.xs4all.nl 2856 [2001:888:2000:d::a6]:45368 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63095 On 1/3/2014 7:16 PM, Wiktor wrote: > Hi, > it's my first post on this newsgroup so welcome everyone. :) > > I'm still learning Python (v3.3), and today I had idea to design (my first) > recursive function, that generates (filled out) board to 'Towers' Puzzle: > > http://www.chiark.greenend.org.uk/~sgtatham/puzzles/js/towers.html > > (so I could in future write algorithm to solve it ;-)) > > > I'm pretty proud of myself - that it works, and that took me only 4 hours > to debug. ;-) > But on Project Euler site sometimes I'm also proud, that I solved some > problem in 30-line script, and then on forum there's one lined solution... > > So maybe You might look at this script, and tell me if this can be more > pythonic. It's nothing urgent. I can wait - it works after all. ;-) > > > Idea is that function "generate()" 'finds' one number at a time (well, > besides first row), then checks if there are no repetitions in column > (because in row there cannot be by design - I pop out numbers from shuffled > list [1, 2, 3, ..., size] for every row.) > If no repetition - calls the same function to find next number, and so on. > If there is repetition at some point - recursion jumps back, and try > different number on previous position. > > > > import random > > > def check(towers, x=None): > if x: > c = x % len(towers) # check only column with > column = [] # value added on pos. x > for i in range(len(towers)): > column.append(towers[i][c]) > column = [x for x in column if x != 0] > # print(column) # debugging leftovers ;-) > return len(column) == len(set(column)) > else: > for c in range(len(towers)): # 'x' not provided, > column = [] # so check all columns > for i in range(len(towers)): > column.append(towers[i][c]) > column = [x for x in column if x != 0] > # print(column) > if len(column) != len(set(column)): > return False > return True > > > def generate(size=4, towers=None, row=None, x=0): > if not towers: # executed only once. > row = [a for a in range(1, size+1)] # Then I'll pass towers list > random.shuffle(row) # at every recursion > towers = [] > # not so pretty way to generate > for i in range(size): # matrix filled with 0's > towers.append([]) # I tried: towers = [[0]*size]*size > for j in range(size): # but this doesn't work. ;-) > towers[i].append(0) # I don't know how to do this with > # list comprehension (one inside [0]*size] is fine for one row towers = [[0]*size] for i in range(size)] should do what you want for a 2-d array instead of the above. I cannot look at the rest right now. -- Terry Jan Reedy