473,561 Members | 3,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Make a generator from a recursive function

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

def do_something(*a rgs):
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(*a rgs)

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 1722
James Stroud <js*****@mbi.uc la.edu> wrote:
...
This was my answer to the thread "new in programing":

def do_something(*a rgs):
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(*a rgs)

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.uc la.edu> wrote:
...
This was my answer to the thread "new in programing":

def do_something(*a rgs):
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(*a rgs)

do_deeply(fir st=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
1921
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 print x --> 1 2 4 This is what I have, but it does not work. Changing yield to print, shows that the recursion works correctly.
8
2295
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 __init__(self, data=None, left=None, right=None): self.children = self.children.append(left) self.children.append(right) self.data = data
2
1283
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. i wrote the same thing as a recursive function producing an array, and this non-generator version is nearly 10 times faster! any ideas why this is, what i did wrong... for some reason the...
2
2877
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 calls. Recursive function 1 takes input and displays a sequence of spaces; recursive function 2 uses input to display ascending sequence of digits; likewise, recursive function 3 uses input to display...
7
2646
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 some concern on its performance. Let's take the inorder traversal from http://www.python.org/peps/pep-0255.html as an example. def inorder(t): if t: for x in inorder(t.left):
5
2239
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
1760
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 according to the Morris sequence. It works for a single number, but what I'd like it to do is either: 1. repeat indefinitely and have the number of times controlled elsewhere in the program (e.g., use...
6
2729
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 mangled through 20 different platforms. I've had it up to here (funny hand gesture) with trying to compile the bullet-riddled code that GNU Flex and Bison keep spitting out for even the simplest of...
3
4222
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 the inner function seems to contain a circular reference, which means it is only collected by the mark-and-sweep collector, not by reference counting. Here is some code that demonstrates it: ===...
0
7568
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8085
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7614
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3598
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1180
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
892
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.