472,353 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Make a generator from a recursive function

This was my answer to the thread "new in programing":

def do_something(*args):
print args

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
do_deeply(first+1, depth+1, lim, False, *args)
if first <= depth:
do_deeply(first+1, depth, lim, True, *args + (first,))
elif doit:
do_something(*args)

do_deeply(first=1, depth=3, lim=4)

I thought it was a good answer, but I think better would be a generator. Is
there a straightforward way to make such a function a generator, or does it
require a not using a recursive function? I think "cheating" would be to
generate the list and make an iterable from it.

Note: this is not the same as "cross" from the "N-uples from list of lists" thread.

James
Dec 10 '05 #1
2 1678
James Stroud <js*****@mbi.ucla.edu> wrote:
...
This was my answer to the thread "new in programing":

def do_something(*args):
print args

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
do_deeply(first+1, depth+1, lim, False, *args)
if first <= depth:
do_deeply(first+1, depth, lim, True, *args + (first,))
elif doit:
do_something(*args)

do_deeply(first=1, depth=3, lim=4)

I thought it was a good answer, but I think better would be a generator. Is
there a straightforward way to make such a function a generator, or does it


I'm not entirely sure what you mean, but I will guess it's something not
too different from...:

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
for x in do_deeply(first+1, depth+1, lim, False, *args):
yield x
if first <= depth:
for x in do_deeply(first+1, depth, lim, True, *args + (first,)):
yield x
elif doit:
yield args

to be used with

for x in do_deeply(first=1, depth=3, lim=4):
do_something(*x)
Did I guess right...?
Alex
Dec 10 '05 #2
Alex Martelli wrote:
James Stroud <js*****@mbi.ucla.edu> wrote:
...
This was my answer to the thread "new in programing":

def do_something(*args):
print args

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
do_deeply(first+1, depth+1, lim, False, *args)
if first <= depth:
do_deeply(first+1, depth, lim, True, *args + (first,))
elif doit:
do_something(*args)

do_deeply(first=1, depth=3, lim=4)

I thought it was a good answer, but I think better would be a generator. Is
there a straightforward way to make such a function a generator, or does it

I'm not entirely sure what you mean, but I will guess it's something not
too different from...:

def do_deeply(first, depth, lim, doit=True, *args):
if depth < lim:
for x in do_deeply(first+1, depth+1, lim, False, *args):
yield x
if first <= depth:
for x in do_deeply(first+1, depth, lim, True, *args + (first,)):
yield x
elif doit:
yield args

to be used with

for x in do_deeply(first=1, depth=3, lim=4):
do_something(*x)
Did I guess right...?
Alex


Yes, that's what I was thinking. Thank you.
Dec 10 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: MetalOne | last post by:
I am trying to write a generator function that yields the index position of each set bit in a mask. e.g. for x in bitIndexGenerator(0x16): #10110...
8
by: Paul Chiusano | last post by:
I've been playing around with generators and have run into a difficulty. Suppose I've defined a Node class like so: class Node: def...
2
by: Johannes Ahl mann | last post by:
hi, i am in the process of profiling an application and noticed how much time my depth first generator for walking a tree data structure took. ...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function...
7
by: aurora | last post by:
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have...
5
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
18
by: John Salerno | last post by:
Ok, I wrote this all by myself, which explains why it doesn't work. It is meant to take a number and generate the next number that follows...
6
by: Mike C# | last post by:
Hi all, Can anyone recommend a good and *easy to use* lexer and parser generator? Preferably one that was written specifically for VC++ and not...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.