Re: Top level of a recursive function

2022-12-17 Thread Rob Cliffe via Python-list
On 14/12/2022 13:49, Stefan Ram wrote: I also found an example similar to what was discussed here in pypy's library file "...\Lib\_tkinter\__init__.py": |def _flatten(item): |def _flatten1(output, item, depth): |if depth > 1000: |raise ValueError("nesting too deep i

Re: Top level of a recursive function

2022-12-15 Thread Peter Otten
On 13/12/2022 15:46, Michael F. Stemper wrote: It's easy enough -- in fact necessary -- to handle the bottom level of a function differently than the levels above it. What about the case where you want to handle something differently in the top level than in lower levels? Is there any way to tell

Re: Top level of a recursive function

2022-12-13 Thread Michael F. Stemper
On 13/12/2022 09.03, Stefan Ram wrote: "Michael F. Stemper" writes: def fred(cf,toplevel=True): x = cf[0] if len(cf)>1: if toplevel: return x + fred(cf[1:],False) else: return "(" + x + fred(cf[1:],False) + ")" else: if toplevel: return x else:

Re: Top level of a recursive function

2022-12-13 Thread Chris Angelico
On Wed, 14 Dec 2022 at 05:01, Mats Wichmann wrote: > > On 12/13/22 10:36, Chris Angelico wrote: > > On Wed, 14 Dec 2022 at 03:35, Michael F. Stemper > > wrote: > >> > >> It's easy enough -- in fact necessary -- to handle the bottom > >> level of a function differently than the levels above it. Wh

Re: Top level of a recursive function

2022-12-13 Thread Mats Wichmann
On 12/13/22 10:36, Chris Angelico wrote: On Wed, 14 Dec 2022 at 03:35, Michael F. Stemper wrote: It's easy enough -- in fact necessary -- to handle the bottom level of a function differently than the levels above it. What about the case where you want to handle something differently in the top

Re: Top level of a recursive function

2022-12-13 Thread Chris Angelico
On Wed, 14 Dec 2022 at 03:35, Michael F. Stemper wrote: > > It's easy enough -- in fact necessary -- to handle the bottom > level of a function differently than the levels above it. What > about the case where you want to handle something differently > in the top level than in lower levels? Is the

RE: Top level of a recursive function

2022-12-13 Thread Schachner, Joseph (US)
: Tuesday, December 13, 2022 10:25 AM To: python-list@python.org Subject: Re: Top level of a recursive function Supersedes: r...@zedat.fu-berlin.de (Stefan Ram) writes: >def rest( s ): >return "(" + s[ 0 ] +( rest( s[1:] ) if len( s )> 1 else '' )+ ')' &

Top level of a recursive function

2022-12-13 Thread Michael F. Stemper
It's easy enough -- in fact necessary -- to handle the bottom level of a function differently than the levels above it. What about the case where you want to handle something differently in the top level than in lower levels? Is there any way to tell from within a function that it wasn't invoked b

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Gregory Ewing
Yawar Amin wrote: Cool ... but it looks like this can still potentially hit the max recursion limit? It depends on the nature of your data. If the data is a tree, it's very unlikely you'll reach the recursion limit unless the tree is massively unbalanced. If there's a chance of that, or if th

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Gregory Ewing
Roy Smith wrote: I will commonly put something like: import logging logger = logging.getLogger("logger-name-for-my-module") But that's not really a global variable, it's a global constant. There's nothing wrong with those, we use them all the time -- classes, functions, etc. If you were to re

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Chris Angelico
On Sun, Jan 18, 2015 at 4:30 AM, Albert van der Horst wrote: > The proper technique is make the global local to the normal subroutine, > then make the subroutine with those parameters you don't want to see > also local to that subroutine. > E.g. > > def fib(n): > ' return the n-th Fibonacci nu

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Albert van der Horst
In article <5e4ccec6-7a00-467d-8cf6-258ab0421...@googlegroups.com>, Tim wrote: >I have this type of situation and wonder if I should use a global >variable outside the recursive function instead of passing the updated >parameter through. > >I want to get a union of al

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Roy Smith
In article <54ba3654$0$13008$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > Good reasons for using global variables are few and far between. Just about > the only good reason for using global variables that I can think of is if > you have one or more settings/preference that get s

Re: recursive function: use a global or pass a parameter?

2015-01-17 Thread Steven D'Aprano
Tim wrote: > I have this type of situation and wonder if I should use a global variable > outside the recursive function instead of passing the updated parameter > through. To a first approximation, the answer to: "I have a X, should I use a global variable or a parameter?"

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Yawar Amin
On Friday, January 16, 2015 at 9:24:15 PM UTC-5, Yawar Amin wrote: > [...] > vals.extend(curr_obj.values()) Ah, I should mention that the above will do a breadth-first search. If we want to do a depth-first search we simply replace the above line with: vals.extendleft(curr_obj.values(

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Yawar Amin
On Friday, January 16, 2015 at 1:34:51 PM UTC-5, Peter Otten wrote: > [...] > I recommend that you use a generator: > > >>> def walk(obj): > ... if not hasattr(obj, "keys"): > ... return > ... if "things" in obj: > ... yield obj["things"] > ... for v in obj.values(): >

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Chris Angelico
On Sat, Jan 17, 2015 at 9:20 AM, Gregory Ewing wrote: > The only thing I would change is to wrap it all up > in a top-level function that takes care of creating > the result set and returning it. > > def walk(obj): > res = set() > _walk(obj, res) > return res > > def _walk(obj, res): > ...

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Gregory Ewing
Tim wrote: I have this type of situation and wonder if I should use a global variable outside the recursive function instead of passing the updated parameter through. No! Globals are evil, at least for that sort of thing. The way you're doing it is fine. The only thing I would change

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Tim
On Friday, January 16, 2015 at 1:34:51 PM UTC-5, Peter Otten wrote: >> Tim wrote: >> > Globals are generally bad as they make code non-reentrant; when two calls of > the function run simultaneously the data will be messed up. > > I recommend that you use a generator: > > >>> def walk(obj): > ..

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Peter Otten
Tim wrote: > I have this type of situation and wonder if I should use a global variable > outside the recursive function instead of passing the updated parameter > through. > > I want to get a union of all the values that any 'things' key may have, > even in a nes

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Rustom Mody
On Friday, January 16, 2015 at 11:26:46 PM UTC+5:30, Chris Angelico wrote: > On Sat, Jan 17, 2015 at 4:49 AM, Tim wrote: > > I want to get a union of all the values that any 'things' key may have, > > even in a nested dictionary (and I do not know beforehand how deep the > > nesting might go): >

Re: recursive function: use a global or pass a parameter?

2015-01-16 Thread Chris Angelico
On Sat, Jan 17, 2015 at 4:49 AM, Tim wrote: > I want to get a union of all the values that any 'things' key may have, even > in a nested dictionary (and I do not know beforehand how deep the nesting > might go): > > d = {'things':1, 'two':{'things':2}} > > def walk(obj, res): > if not hasatt

recursive function: use a global or pass a parameter?

2015-01-16 Thread Tim
I have this type of situation and wonder if I should use a global variable outside the recursive function instead of passing the updated parameter through. I want to get a union of all the values that any 'things' key may have, even in a nested dictionary (and I do not know befo

Re: Recursive function

2013-03-05 Thread Neil Cerutti
s to first write the simplest version of the function you can think of, the version that solves the most trivial form for the problem. For example, lets say I want to write a recursive function to reverse a string (you would never do this except as an exercise), I'd first write a version that ca

Re: Recursive function

2013-03-05 Thread Ana Dionísio
Yes, I simplified it a lot. I need to run it 24 times. What I don't really understand is how to put the final temperature (result) in it = 0 in temp_-1 in it =1 -- http://mail.python.org/mailman/listinfo/python-list

Re: Recursive function

2013-03-05 Thread Vlastimil Brom
2013/3/5 Ana Dionísio : > 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

Re: Recursive function

2013-03-05 Thread Dave Angel
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?

Recursive function

2013-03-05 Thread Ana Dionísio
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! -- http://mail.python.org/mai

Re: Hotshoting recursive function

2011-05-26 Thread Selvam
iling function. >> >> I would like to get some suggestions. >> > > The recursive call inside your function should call the undecorated > function, not the decorated function again. Decorator syntax is not > convenient anymore. > > Using t

Re: Hotshoting recursive function

2011-05-25 Thread Gabriel Genellina
tax is not convenient anymore. Using the same names as in the recipe example: # a recursive function def my_slow_function(n): ... return my_slow_function(n-1) my_profiled_slow_function = hotshotit(my_slow_function) my_profiled_slow_function(n) This works, in the sense that it does

Hotshoting recursive function

2011-05-22 Thread Selvam
Hi, I am using hotshot module to profile my python function. I used the details from ( http://code.activestate.com/recipes/576656-quick-python-profiling-with-hotshot/ ). The function I profile is a recursive one and I am getting the following error, "ProfilerError: profiler already active" I

Re: list parameter of a recursive function

2010-10-07 Thread TP
Seebs wrote: > On 2010-10-07, TP wrote: >> Diez B. Roggisch wrote: >>> A safer alternative for these cases is using tuples, because they are >>> immutable. > >> The problem with tuples is that it is not easy to modify them: > > This is probably the best post-and-response I've seen in the last c

Re: list parameter of a recursive function

2010-10-07 Thread Terry Reedy
On 10/6/2010 3:22 PM, TP wrote: Hi, I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) This sort of function is an exception. I

Re: list parameter of a recursive function

2010-10-07 Thread Diez B. Roggisch
TP writes: > Diez B. Roggisch wrote: > >> Back to your example: your solution is perfectly fine, although a bit >> costly and more error-prone if you happen to forget to create a copy. >> A safer alternative for these cases is using tuples, because they are >> immutable. > > Thanks Diez for your

Re: list parameter of a recursive function

2010-10-07 Thread Chris Rebert
On Wed, Oct 6, 2010 at 10:25 PM, TP wrote: > Diez B. Roggisch wrote: > >> Back to your example: your solution is perfectly fine, although a bit >> costly and more error-prone if you happen to forget to create a copy. >> A safer alternative for these cases is using tuples, because they are >> immut

Re: list parameter of a recursive function

2010-10-06 Thread Seebs
On 2010-10-07, TP wrote: > Diez B. Roggisch wrote: >> A safer alternative for these cases is using tuples, because they are >> immutable. > The problem with tuples is that it is not easy to modify them: This is probably the best post-and-response I've seen in the last couple of months. -s -- C

Re: list parameter of a recursive function

2010-10-06 Thread TP
Steven D'Aprano wrote: >> I think I prefer doing an explicit copy.copy, because it allows to >> remind the reader that it is very important to take care of that. > > I think a comment is better for that. It's better to explicitly say > something is important than to assume the reader will guess.

Re: list parameter of a recursive function

2010-10-06 Thread TP
Diez B. Roggisch wrote: > Back to your example: your solution is perfectly fine, although a bit > costly and more error-prone if you happen to forget to create a copy. > A safer alternative for these cases is using tuples, because they are > immutable. Thanks Diez for your explanation. The proble

Re: list parameter of a recursive function

2010-10-06 Thread Steven D'Aprano
On Wed, 06 Oct 2010 23:00:10 +0200, TP wrote: > I think I prefer doing an explicit copy.copy, because it allows to > remind the reader that it is very important to take care of that. I think a comment is better for that. It's better to explicitly say something is important than to assume the re

Re: list parameter of a recursive function

2010-10-06 Thread Diez B. Roggisch
TP writes: > Hi, > > I have a function f that calls itself recursively. It has a list as second > argument, with default argument equal to None (and not [], as indicated at: > http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) > > This is the outline of my function: > > def f ( a

Re: list parameter of a recursive function

2010-10-06 Thread TP
Chris Torek wrote: >>import copy from copy > > [from copy import copy, rather] Yes, sorry. > Note that if f() is *supposed* to be able to modify its second > parameter under some conditions, you would want to make the copy > not at the top of f() but rather further in, and in this case, > that

Re: list parameter of a recursive function

2010-10-06 Thread Chris Torek
In article TP wrote: >I have a function f that calls itself recursively. It has a list as second >argument, with default argument equal to None (and not [], as indicated at: >http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) > >This is the outline of my function: > >def f ( arg

list parameter of a recursive function

2010-10-06 Thread TP
Hi, I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) This is the outline of my function: def f ( argument, some_list = None ):

Re: it doesn't work ;) [class recursive function]

2010-09-17 Thread Ethan Furman
MRAB wrote: On 17/09/2010 17:55, Ethan Furman wrote: MRAB wrote: On 16/09/2010 00:23, Ethan Furman wrote: PS My apologies if this shows up twice, I haven't seen my other post yet and it's been 27 hours. That's probably because you sent it directly to me. That would explain it -- like I sa

Re: it doesn't work ;) [class recursive function]

2010-09-17 Thread MRAB
On 17/09/2010 17:55, Ethan Furman wrote: MRAB wrote: On 16/09/2010 00:23, Ethan Furman wrote: I need some fresh eyes, or better brains, or both! 'next_item' is a generator, but it's just calling itself and discarding the result. I think it should be yielding the results to its caller. That f

Re: it doesn't work ;) [class recursive function]

2010-09-15 Thread MRAB
On 16/09/2010 00:23, Ethan Furman wrote: I need some fresh eyes, or better brains, or both! The expected debugging output is a list of names in alphabetical order from each node (there are about 90 of them); what I am getting is this: --> dbf.tables.Index.from_file('', r'aad13658_last_name_for_

it doesn't work ;) [class recursive function]

2010-09-15 Thread Ethan Furman
I need some fresh eyes, or better brains, or both! The expected debugging output is a list of names in alphabetical order from each node (there are about 90 of them); what I am getting is this: --> dbf.tables.Index.from_file('', r'aad13658_last_name_for_state.idx') starting next_item call fo

Re: my recursive function call is wrong?

2009-08-16 Thread Chang Min Jeon
Dear Kev Thank you very much. I got it.:) 2009/8/16 Kev Dwyer > On Sun, 16 Aug 2009 16:57:41 +0900, Chang Min Jeon wrote: > > > Hello, > > You have placed recursive calls to the function in a number of different > locations; when len(macro) becomes zero control will return to the > calling func

Re: my recursive function call is wrong?

2009-08-16 Thread Kev Dwyer
On Sun, 16 Aug 2009 16:57:41 +0900, Chang Min Jeon wrote: Hello, You have placed recursive calls to the function in a number of different locations; when len(macro) becomes zero control will return to the calling function, but this calling function may have more code to execute, including fur

my recursive function call is wrong?

2009-08-16 Thread Chang Min Jeon
I'm trying to write program to translate define macro in 'C'. And start_parse has return condition that list's length is 0. At this time return statement invoke start_parse() function. I can't understand do that. I'm using Python 2.6.2 in Windows XP import re import sys comment = ''' #if defined

Re: Puzzling: local variable in recursive function made global?

2009-04-02 Thread Aahz
In article , andrew cooke wrote: > >sorry for the shouting, but someone asks this EVERY DAY AND I CAN'T TAKE >ANY MORE. Nobody's forcing you to respond. Nobody's forcing you to top-post, either. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "Debugging is twic

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread andrew cooke
Peter Otten wrote: > I would be surprised by the behaviour of Andrew's variant: > def append(item, items=None): > ... items = items or [] > ... items.append(item) > ... return items > ... a = [] append("x", a) > ['x'] ah that's a good point. andrew -- http://mail.

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Terry Reedy
If the order of node-entry into seen_nodes is never used (if particular, if 'node in seen_nodes' is its only usage), then seen_nodes could be a set and the 'in' operation would be O(1) instead of O(n). -- http://mail.python.org/mailman/listinfo/python-list

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Peter Otten
Daniel Oberski wrote: > Hi Peter, > >> Plus, it works as expected (read: modifies the argument) if you >> explicitly pass an empty list to the function... > > That is not so. The reason is given by Andrew Cooke in this thread. > > I would "expect" that when function calls lower in the recursion

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread MRAB
andrew cooke wrote: it's not global, it's mutable. you are passing THE SAME FRICKING OBJECT to is_terminal and appending values to it. instead, make a new copy: branch.next.is_terminal(list(seen_nodes)) sorry for the shouting, but someone asks this EVERY DAY AND I CAN'T TAKE ANY MORE. [s

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Daniel Oberski
Hi Peter, > Plus, it works as expected (read: modifies the argument) if you > explicitly pass an empty list to the function... That is not so. The reason is given by Andrew Cooke in this thread. I would "expect" that when function calls lower in the recursion hierarchy return, the object is not

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Daniel Oberski
Hi Diez, Great, this totally clears it up. Thank you! - daniel On Thu, 26 Mar 2009 17:50:20 +0100, Diez B. Roggisch wrote: > > That's not a local variable, that is a default argument. Which is in > fact only created once for each function, yes.> > http://effbot.org/pyfaq/why-are-default-value

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Daniel Oberski
Hi Andrew, > it's not global, it's mutable. you are passing THE SAME FRICKING OBJECT > to is_terminal and appending values to it. That, I understand. I already saw in the archives this confuses many people, e.g. the thread "Odd behavior regarding a list". I think this is understandable if you

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Peter Otten
andrew cooke wrote: > Diez B. Roggisch wrote: >> That's not a local variable, that is a default argument. Which is in >> fact only created once for each function, yes. >> >> http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm > > a nice way of handling this was posted here j

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread andrew cooke
Diez B. Roggisch wrote: > That's not a local variable, that is a default argument. Which is in > fact only created once for each function, yes. > > http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm a nice way of handling this was posted here just yesterday, which isn't in t

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread Diez B. Roggisch
Daniel Oberski schrieb: Hello all, I wrote this function to recurse through a tree structure of Nodes connected by Branches. I use a local variable seen_nodes to mark off Nodes already seen by the function (i.e. add them to a list). That's not a local variable, that is a default argument.

Re: Puzzling: local variable in recursive function made global?

2009-03-26 Thread andrew cooke
>> >>>> n1.add(b1) >>>> n2.add(b2) >>>> n2.add(b3) >>>> n4.add(b4) >>>> >>>> print n1.is_terminal() > 1 > >>>> # terminal graph with a cycle >>>> #-> b1 -> b2 -> b3 >>>

Puzzling: local variable in recursive function made global?

2009-03-26 Thread Daniel Oberski
ext = n5) >>> >>> n1.add(b1) >>> n2.add(b2) >>> n2.add(b3) >>> n4.add(b4) >>> >>> print n1.is_terminal() 1 >>> # terminal graph with a cycle >>> #-> b1 -> b2 -> b3 >>> # a -| >>> #

Re: Pass same parameter in Recursive function

2008-09-03 Thread Diez B. Roggisch
NOT tail-recursive 2. Yes, the arguments are immutable after I call the function the first time. I think the best description of the problem may be: How to keep some argument constant and accessable in one function (especially, recursive function). We know that we can use something like self.param

Re: Pass same parameter in Recursive function

2008-09-02 Thread Davy
s NOT tail-recursive 2. Yes, the arguments are immutable after I call the function the first time. I think the best description of the problem may be: How to keep some argument constant and accessable in one function (especially, recursive function). We know that we can use something like self.parameter to

Re: Pass same parameter in Recursive function

2008-09-02 Thread Chris Rebert
of this though. - Chris On Tue, Sep 2, 2008 at 8:20 PM, Davy <[EMAIL PROTECTED]> wrote: > Hi all, > > Sometimes I need to pass same parameter in recursive function. From my > point of view, the style is redundant, and I don't what to use some > global style like self.A, self.B, I

Pass same parameter in Recursive function

2008-09-02 Thread Davy
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any other choice? For example, def func(self, x, y, A, B, C): #x, y change in recursive call #A,

Re: Recursive function won't compile

2008-04-02 Thread Keith Thompson
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes: >> #include >> #include >> >> def RecursiveFact(n): >> if(n>1): >> return n*RecursiveFact(n-1) >> else: >> return 1 >> >> fact = RecursiveFact(31) >> print fact >> >> fact = "End of program" >> print fact >> >> >> ..but y

Re: Recursive function won't compile

2008-04-02 Thread George Sakkis
On Apr 2, 5:00 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] schrieb: > > > > > #include > > #include > > > def RecursiveFact(n): > > if(n>1): > > return n*RecursiveFact(n-1) > > else: > > return 1 > > > fact = RecursiveFact(31) > > print fact > > >

Re: Recursive function won't compile

2008-04-02 Thread Diez B. Roggisch
[EMAIL PROTECTED] schrieb: > #include > #include > > def RecursiveFact(n): > if(n>1): > return n*RecursiveFact(n-1) > else: > return 1 > > fact = RecursiveFact(31) > print fact > > fact = "End of program" > print fact > > > ..but yet it still gives the right answe

Re: Recursive function won't compile

2008-04-02 Thread dj3vande
In article <[EMAIL PROTECTED]>, <[EMAIL PROTECTED]> wrote: (Subject: Recursive function won't compile) >#include >#include > >def RecursiveFact(n): >..but yet it still gives the right answer. How is this possible? Possibly because it gives the right ans

Re: Recursive function won't compile

2008-04-02 Thread ajaksu
On Apr 2, 5:23 pm, [EMAIL PROTECTED] wrote: > #include > #include > > def RecursiveFact(n): >     if(n>1): >         return n*RecursiveFact(n-1) >     else: >         return 1 > > fact = RecursiveFact(31) > print fact The output is 822283865417792281772556288000 and is correct. But the "#incl

Re: Recursive function won't compile

2008-04-02 Thread Gary Herron
[EMAIL PROTECTED] wrote: > #include > #include > > def RecursiveFact(n): > if(n>1): > return n*RecursiveFact(n-1) > else: > return 1 > > fact = RecursiveFact(31) > print fact > > fact = "End of program" > print fact > > > ..but yet it still gives the right answer. How

Recursive function won't compile

2008-04-02 Thread bc1891
#include #include def RecursiveFact(n): if(n>1): return n*RecursiveFact(n-1) else: return 1 fact = RecursiveFact(31) print fact fact = "End of program" print fact ..but yet it still gives the right answer. How is this possible? -- http://mail.python.org/mailman/l

Re: python recursive function

2008-01-11 Thread Steven D'Aprano
On Fri, 11 Jan 2008 06:30:04 -0600, Nick Craig-Wood wrote: > HYRY <[EMAIL PROTECTED]> wrote: >> def bears (n): >> if n==42: >> return True ... >> return False > > Almost but you missed a case... Why are you people doing the OP's homework for him? And then DEBUGGING it as wel

RE: python recursive function

2008-01-11 Thread Reedick, Andrew
> -Original Message- > From: [EMAIL PROTECTED] [mailto:python- > [EMAIL PROTECTED] On Behalf Of Tom_chicollegeboy > Sent: Friday, January 11, 2008 3:30 AM > To: python-list@python.org > Subject: python recursive function > > Now, you are to write a program tha

Re: python recursive function

2008-01-11 Thread Fidtz
On 11 Jan, 08:30, Tom_chicollegeboy <[EMAIL PROTECTED]> wrote: > here is what I have to do: > > This question involves a game with teddy bears. The game starts when I > give you some bears. You then start giving me back some bears, but you > must follow these rules (where n is the number of bears t

Re: python recursive function

2008-01-11 Thread Nick Craig-Wood
HYRY <[EMAIL PROTECTED]> wrote: > def bears (n): > if n==42: > return True > if n%5==0: > if bears(n-42): > return True > if n%2==0: > if bears(n/2): > return True > if n%3==0 or n%4==0: > one = (n%10) > two

Re: python recursive function

2008-01-11 Thread Bruno Desthuilliers
Duncan Booth a écrit : > Bruno Desthuilliers <[EMAIL PROTECTED]> > wrote: > >> You want: >>return bears(n - 42) > > Actually, no he doesn't. He needs to explore all options when the first > attempt fails. Possibly - I didn't bother checking the algorithm correctness, just pointed

Re: python recursive function

2008-01-11 Thread HYRY
> def bears (n): > if n==42: > return True > if n%5==0: > bears(n-42) > if n%2==0: > bears(n/2) > if n%3==0 or n%4==0: > one = (n%10) > two = ((n%100)/10) > if one!=0 and two!=0: > bears(n-(one*two)) > return False > >

Re: python recursive function

2008-01-11 Thread cokofreedom
> Stylistically I prefer 'if not n % 5', looks neater. > As for your assignment, the hardest task will be creating an effective > method of ensuring you recurse through all possibilities. I was chatting to a friend about the 'if not n % 5' and while I am happy to use it saying that when 5 % 5 is F

Re: python recursive function

2008-01-11 Thread Chris
On Jan 11, 10:30 am, Tom_chicollegeboy <[EMAIL PROTECTED]> wrote: > here is what I have to do: > > This question involves a game with teddy bears. The game starts when I > give you some bears. You then start giving me back some bears, but you > must follow these rules (where n is the number of bear

Re: python recursive function

2008-01-11 Thread Duncan Booth
Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > You want: >return bears(n - 42) Actually, no he doesn't. He needs to explore all options when the first attempt fails. But I'm not going to write his homework for him. -- http://mail.python.org/mailman/listinfo/python-list

Re: python recursive function

2008-01-11 Thread cokofreedom
On Jan 11, 9:46 am, Gary Herron <[EMAIL PROTECTED]> wrote: > Tom_chicollegeboy wrote: > > here is what I have to do: > > > This question involves a game with teddy bears. The game starts when I > > give you some bears. You then start giving me back some bears, but you > > must follow these rules (w

Re: python recursive function

2008-01-11 Thread Bruno Desthuilliers
Gary Herron a écrit : > Tom_chicollegeboy wrote: >> here is what I have to do: >> >> This question involves a game with teddy bears. The game starts when I >> give you some bears. You then start giving me back some bears, but you >> must follow these rules (where n is the number of bears that you >

Re: python recursive function

2008-01-11 Thread Bruno Desthuilliers
Tom_chicollegeboy a écrit : (snip) > As you see my program must use recursion. It's conceptually easier to express using recursions - but every recursion-based algorithm can be rewritten to use iteration (and vice-versa). > I came up with this idea but I am not sure if its right or are there >

Re: python recursive function

2008-01-11 Thread Gary Herron
Tom_chicollegeboy wrote: > here is what I have to do: > > This question involves a game with teddy bears. The game starts when I > give you some bears. You then start giving me back some bears, but you > must follow these rules (where n is the number of bears that you > have): > This sounds very

python recursive function

2008-01-11 Thread Tom_chicollegeboy
here is what I have to do: This question involves a game with teddy bears. The game starts when I give you some bears. You then start giving me back some bears, but you must follow these rules (where n is the number of bears that you have): If n is even, then you may give back exactly n/2 bears.

Re: It is not possible to create a recursive function over a pyGTK treeStore

2007-06-29 Thread sebastien . abeille
ing all the files and folders so that > it would map the > file system hierarchy. > > I wrote a recursive function that would go through the file system > tree. > > My problem is that a sub-node of a gtk.TreeStore is not a > gtk.TreeStore, but > a gtk.TreeIter. And gtk.treeter

Re: It is not possible to create a recursive function over a pyGTK treeStore

2007-06-29 Thread Bruno Desthuilliers
it would map the > file system hierarchy. > > I wrote a recursive function that would go through the file system > tree. > > My problem is that a sub-node of a gtk.TreeStore is not a > gtk.TreeStore, but > a gtk.TreeIter. And gtk.treeter has almost no functions I would be >

It is not possible to create a recursive function over a pyGTK treeStore

2007-06-29 Thread sebastien . abeille
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing all the files and folders so that it would map the file system hierarchy. I wrote a recursive function

Re: recursive function

2007-01-09 Thread cesco
Hendrik van Rooyen wrote: > "cesco" <[EMAIL PROTECTED]> wrote: > > > > > Neil Cerutti wrote: > > > On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote: > > > > Hi, > > > > > > > > I have a dictionary of lists of tuples like in the following example: > > > > dict = {1: [(3, 4), (5, 8)], > > > >

Re: recursive function

2007-01-09 Thread Hendrik van Rooyen
"cesco" <[EMAIL PROTECTED]> wrote: > > Neil Cerutti wrote: > > On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote: > > > Hi, > > > > > > I have a dictionary of lists of tuples like in the following example: > > > dict = {1: [(3, 4), (5, 8)], > > > 2: [(5, 4), (21, 3), (19, 2)], > > >

Re: recursive function

2007-01-08 Thread Max Erickson
most inner tuple is > equal to N. > > For example, assuming N = 24, in this case it should return: > [((3, 4), (5, 4), (16, 1)), ((3, 4), (21, 3), (0, 2)), ((5, 8), > (19, 2), (0, 2))] > > A simple list comprehension would be enough if only I knew the > number of keys/lists bef

Re: recursive function

2007-01-08 Thread Tim Williams
On 8 Jan 2007 16:03:53 +0100, Neil Cerutti <[EMAIL PROTECTED]> wrote: > > len(dict.keys()). > Or len(dict) :) -- http://mail.python.org/mailman/listinfo/python-list

Re: recursive function

2007-01-08 Thread bearophileHUGS
First possible solution: def rloop(seqin, comb): # xcross product recipe 302478 by David Klaffenbach if seqin: for item in seqin[0]: newcomb = comb + [item] for item in rloop(seqin[1:], newcomb): yield item else: yield comb data

Re: recursive function

2007-01-08 Thread cesco
Neil Cerutti wrote: > On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote: > > Hi, > > > > I have a dictionary of lists of tuples like in the following example: > > dict = {1: [(3, 4), (5, 8)], > > 2: [(5, 4), (21, 3), (19, 2)], > > 3: [(16, 1), (0, 2), (1, 2), (3, 4)]] > > > > In this

Re: recursive function

2007-01-08 Thread Neil Cerutti
On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote: > Hi, > > I have a dictionary of lists of tuples like in the following example: > dict = {1: [(3, 4), (5, 8)], > 2: [(5, 4), (21, 3), (19, 2)], > 3: [(16, 1), (0, 2), (1, 2), (3, 4)]] > > In this case I have three lists inside the dict

recursive function

2007-01-08 Thread cesco
)), ((3, 4), (21, 3), (0, 2)), ((5, 8), (19, 2), (0, 2))] A simple list comprehension would be enough if only I knew the number of keys/lists beforehand but this is not the case. I guess I need a recursive function. Can anyone help? Thanks in advance Francesco -- http://mail.python.org/mailman

Re: Return returns nothing in recursive function

2006-10-17 Thread yellowalienbaby
Fredrik Lundh wrote: > Matthew Warren wrote: > > > I have the following code that implements a simple recursive tree like > > structure. > > > > The trouble is with the GetTreeBranch function, the print statement > > prints a valid value but the return immediatley afterward doesn't return > > anyt

  1   2   >