473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to use generators?

<Spoiler Alert - Anyone at < Level 24 in Python Challenge may not want to
read this post!>

I have never used generators before but I might have now found a use for
them. I have written a recursive function to solve a 640x640 maze but it
crashes, due to exceeding the stack. The only way around this I can
think of is to use Generator but I have no idea how to.

The function is as below:

def solve_maze(x,y):
if y <= 0:
success = 1
elif x <= 0 or x > 640 or y >= 640:
success = 0
elif maze_array[x][y] == 1:
success = 0
elif im.getpixel((x,y)) == (255, 255, 255, 255):
success = 0
else:
maze_array[x][y] = 1
if solve_maze(x,y-1) == 1:
success = 1
elif solve_maze(x+1,y) == 1:
success = 1
elif solve_maze(x-1,y) == 1:
success = 1
else:
success = solve_maze(x,y+1)

if success == 1:
print im.getpixel((x,y))

return success

#Main
wibble = solve_maze(x,y)
Nov 9 '05 #1
3 1355
Ian Vincent enlightened us with:
I have never used generators before but I might have now found a use
for them. I have written a recursive function to solve a 640x640
maze but it crashes, due to exceeding the stack. The only way
around this I can think of is to use Generator but I have no idea
how to.


A better way is to use a queue. I had the same problem with a similar
piece of code. The only thing why you're using a stack is to move to
the "next" point, without going back to a visited point.

The non-recursive solution is to mark all visited points as such, only
consider non-visited points, and then append the coordinates to a list
of points yet to visit. Then keep looping over your code until either
you found the solution to the maze or there are no points left to
visit.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Nov 9 '05 #2
On Wed, 9 Nov 2005, Sybren Stuvel wrote:
Ian Vincent enlightened us with:
I have never used generators before but I might have now found a use
for them. I have written a recursive function to solve a 640x640 maze
but it crashes, due to exceeding the stack. The only way around this I
can think of is to use Generator but I have no idea how to.


A better way is to use a queue. I had the same problem with a similar
piece of code. The only thing why you're using a stack is to move to
the "next" point, without going back to a visited point.


Exactly - using a queue means you'll do a breadth-first rather than a
depth-first search, which will involve much less depth of recursion. See:

http://cs.colgate.edu/faculty/neviso...00/Notes12.htm

For details.

An extended version of this exercise would be to implement an A* search:

http://en.wikipedia.org/wiki/A-star_search_algorithm

Which might be quicker than a blind breadth-first.

tom

--
Exceptions say, there was a problem. Someone must deal with it. If you
won't deal with it, I'll find someone who will.
Nov 9 '05 #3
Tom Anderson <tw**@urchin.earth.li> wrote in
news:Pi*******************************@urchin.eart h.li:

Exactly - using a queue means you'll do a breadth-first rather than a
depth-first search, which will involve much less depth of recursion.
See:


Thanks for the answers but found a easier (admittedly cheating) way around
the problem....run the code on my 64bit Linux system at home.
Nov 10 '05 #4

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

Similar topics

23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
3
by: Michael Sparks | last post by:
Hi, I'm posting a link to this since I hope it's of interest to people here :) I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework, and it's been published as a BBC...
13
by: Martin Sand Christensen | last post by:
Hi! First a bit of context. Yesterday I spent a lot of time debugging the following method in a rather slim database abstraction layer we've developed: ,---- | def selectColumn(self,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.