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


Groups > comp.lang.python > #40539

Re: Recursive function

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!feeder3.eweka.nl!81.171.88.16.MISMATCH!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder5.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'example:': 0.03; 'calculates': 0.09; 'terminate.': 0.09; 'termination': 0.09; 'def': 0.10; 'stack': 0.15; 'iteration': 0.16; 'iteration.': 0.16; 'itertools': 0.16; 'loops': 0.16; 'lot!': 0.16; 'simplified': 0.16; 'still,': 0.16; 'temp': 0.16; 'wrote:': 0.17; '(or': 0.18; 'otherwise,': 0.20; 'parameters': 0.20; 'import': 0.21; 'runs': 0.22; 'for?': 0.23; 'script': 0.24; 'specifically': 0.24; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'implies': 0.29; 'way?': 0.29; 'case,': 0.29; 'print': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'but': 0.36; 'too': 0.36; 'uses': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'more': 0.63; 'hello!': 0.65; 'received:74.208': 0.71; '10:32': 0.84; 'recursion,': 0.84; 'room).': 0.84; 'temperature': 0.84; 'temperature,': 0.84; 'ana': 0.91
Date Tue, 05 Mar 2013 11:02:03 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: Recursive function
References <9ff73ed4-24cd-4a61-bb54-a67dd4a96ed0@r9g2000vbh.googlegroups.com>
In-Reply-To <9ff73ed4-24cd-4a61-bb54-a67dd4a96ed0@r9g2000vbh.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
X-Provags-ID V02:K0:lcR0Bjk3HLs47MlI6IHA/nag4z+HCBvIie8E29YeGtt MGaXlkYjfgBGZ6I8rRTJJwht3DNNvF22+5XudwWnQAZn0N+sZC 1ZAJotVRIOR7lsmPjC/HpZD+z5wY68pwrCA3CV/9TmEiM6naXi ehkCDiqX0i6lptkbi+kuYanNUrfIAw+4SAHM97XGCPNTgYwL8X MJojBbaX8mwDct8tlr8YSAt+QCn1tylFQqF6jeDSqNtn8Ahujx x42YPGxwkYOsE7NtmcTChWVA0AOAxwf2/4it5caHZFnxnpG+7W SCVOsnvMZx2GQWhWh0FyVEJNE0mh1VH3mWc4p97u7YwKbzapw= =
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2891.1362499338.2939.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362499338 news.xs4all.nl 6967 [2001:888:2000:d::a6]:57411
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:40539

Show key headers only | View raw


On 03/05/2013 10:32 AM, Ana Dionísio wrote:
> Hello!
>
> I have to make a script that calculates temperature, but one of the
> parameters is the temperature in the iteration before, for example:
> temp = (temp_-1)+1
>
> it = 0
> temp = 3
>
> it = 1
> temp = 3+1
>
> it = 2
> temp = 4+1
>
> How can I do this in a simple way?
>
> Thanks a lot!
>

Recursion only works when you have a termination condition.  Otherwise, 
it just loops endlessly (or more specifically until the stack runs out 
of room).  Still, you don't have a problem that even implies recursion, 
just iteration.

For your simple case, there's a simple formula:

def  find_temp(it):
     return it+3

Methinks you have simplified the problem too far to make any sense.

import itertools
temp = 3
for it in itertools.count():
     temp += 1
     print it, temp

Warning:  that loop will never terminate.

Is this what you were looking for?  It uses recursion, and I used <=0 
for the termination condition.

def find_temp2(it):
     if it <=0:  return 3
     return find_temp(it-1)


-- 
DaveA

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


Thread

Recursive function Ana Dionísio <anadionisio257@gmail.com> - 2013-03-05 07:32 -0800
  Re: Recursive function Dave Angel <davea@davea.name> - 2013-03-05 11:02 -0500
    Re: Recursive function Ana Dionísio <anadionisio257@gmail.com> - 2013-03-05 08:15 -0800
    Re: Recursive function Ana Dionísio <anadionisio257@gmail.com> - 2013-03-05 08:15 -0800
      Re: Recursive function Neil Cerutti <neilc@norwich.edu> - 2013-03-05 19:05 +0000
  Re: Recursive function Vlastimil Brom <vlastimil.brom@gmail.com> - 2013-03-05 17:06 +0100

csiph-web