Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63095

Re: [newbie] Recursive algorithm - review

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 <python-python-list@m.gmane.org>
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 <tjreedy@udel.edu>
Subject Re: [newbie] Recursive algorithm - review
Date Fri, 03 Jan 2014 20:47:16 -0500
References <m2p28zow0k72.17kajsf7eon4w.dlg@40tude.net>
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 <m2p28zow0k72.17kajsf7eon4w.dlg@40tude.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4874.1388800051.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[newbie] Recursive algorithm - review Wiktor <look@signature.invalid> - 2014-01-04 01:16 +0100
  Re: [newbie] Recursive algorithm - review Terry Reedy <tjreedy@udel.edu> - 2014-01-03 20:47 -0500
    Re: [newbie] Recursive algorithm - review Wiktor <look@signature.invalid> - 2014-01-04 12:23 +0100
  Re: [newbie] Recursive algorithm - review Wiktor <look@signature.invalid> - 2014-01-04 20:07 +0100
    Re: [newbie] Recursive algorithm - review Wiktor <look@signature.invalid> - 2014-01-04 20:36 +0100
    Re: [newbie] Recursive algorithm - review Chris Angelico <rosuav@gmail.com> - 2014-01-05 10:11 +1100

csiph-web