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


Groups > comp.lang.python > #42203

Re: Sudoku

Date 2013-03-28 19:28 -0400
From Dave Angel <davea@davea.name>
Subject Re: Sudoku
References <e45b8b75-ff89-4ee1-9ada-bfda3d06d05d@googlegroups.com> <9dla2a-kql.ln1@satorlaser.homedns.org> <5ce10a13-be58-4548-85df-e1d865d3304e@googlegroups.com> <mailman.3857.1364445382.2939.python-list@python.org> <f160e316-e2f6-4314-827e-9d48007f7f70@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3924.1364513332.2939.python-list@python.org> (permalink)

Show all headers | View raw


On 03/28/2013 06:11 PM, Eric Parry wrote:
> On Thursday, March 28, 2013 3:06:02 PM UTC+10:30, Dave Angel wrote:


    <SNIP>
>>
>>
>> Are you familiar with recursion?  Notice the last line in the function
>> r() calls the function r() inside a for loop.
>>
>> So when r() returns, you're back inside the next level up of the
>> function, and doing a "backtrack."
>>
>> When the function succeeds, it will be many levels of recursion deep.
>> For example, if the original pattern had 30 nonzero items in it, or 51
>> zeroes, you'll be 51 levels of recursion when you discover a solution.
>>
>> If you don't already understand recursion at all, then say so, and one
>> or more of us will try to explain it in more depth.
>>
       <SNIP>
>
> Thank you for that explanation.
> No, I do not understand recursion. It is missing from my Python manual. I would be pleased to receive further explanation from anyone.
> Eric.
>

Recursion is not limited to Python.  It's a general programming term, 
and indeed applies in other situations as well.  Suppose you wanted to 
explain what the -r switch meant in the cp command.  "r" stands for 
recursion.

(example is from Linux.  But if you're more familiar with Windows, it's 
the /s switch in the DIR command.)

The cp command copies all the matching files in one directory to another 
one.  If the -r switch is specified, it then does the same thing to each 
subdirectory.

Notice we did NOT have to specify sub-subdirectories, since they're 
recursively implied by the first description.


Closer to the current problem, suppose you defined factorial in the 
following way:  factorial(0) is 1, by definition.  And for all n>0, 
factorial(n) is n*factorial(n-1).

So to directly translate this definition to code, you write a function 
factorial() which takes an integer and returns an integer.  If the 
parameter is zero, return one.  If the parameter is bigger than zero, 
then the function calls itself with a smaller integer, building up the 
answer as needed  (untested).

def  factorial(n):
     if n==0:
         return 1
     val = n *factorial(n-1)
     return val




-- 
DaveA

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


Thread

Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-26 22:44 -0700
  Re: Sudoku Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-03-27 08:58 +0100
    Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-27 20:00 -0700
      Re: Sudoku Dave Angel <davea@davea.name> - 2013-03-28 00:36 -0400
        Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-28 15:11 -0700
          Re: Sudoku Dave Angel <davea@davea.name> - 2013-03-28 19:28 -0400
            Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-28 22:07 -0700
            Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-28 22:07 -0700
          Re: Sudoku Chris Angelico <rosuav@gmail.com> - 2013-03-29 09:45 +1100
            Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-29 14:47 -0700
              Re: Sudoku Dave Angel <davea@davea.name> - 2013-03-29 18:11 -0400
                Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-30 15:06 -0700
                Re: Sudoku Dave Angel <davea@davea.name> - 2013-03-30 19:15 -0400
                Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-31 15:03 -0700
                Re: Sudoku Dave Angel <davea@davea.name> - 2013-03-31 18:34 -0400
                Re: Sudoku Arnaud Delobelle <arnodel@gmail.com> - 2013-03-31 23:59 +0100
                Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-31 15:03 -0700
                Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-31 15:27 -0700
                Re: Sudoku Chris Angelico <rosuav@gmail.com> - 2013-04-01 09:35 +1100
                Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-31 20:58 -0700
                Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-31 15:27 -0700
                Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-30 15:06 -0700
            Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-29 14:47 -0700
        Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-28 15:11 -0700
  Re: Sudoku Damien Wyart <damien.wyart@free.fr> - 2013-03-27 09:49 +0100
  Re: Sudoku Dave Angel <davea@davea.name> - 2013-03-27 05:38 -0400
  Re: Sudoku Eric Parry <joan4eric@gmail.com> - 2013-03-27 19:49 -0700

csiph-web