Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How to use generators?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ian Vincent  
View profile  
 More options Nov 9 2005, 6:18 am
Newsgroups: comp.lang.python
From: Ian Vincent <i_vinc...@hotmail.com>
Date: 9 Nov 2005 11:18:12 GMT
Local: Wed, Nov 9 2005 6:18 am
Subject: 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)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sybren Stuvel  
View profile  
 More options Nov 9 2005, 10:01 am
Newsgroups: comp.lang.python
From: Sybren Stuvel <sybren...@YOURthirdtower.com.imagination>
Date: Wed, 9 Nov 2005 16:01:30 +0100
Local: Wed, Nov 9 2005 10:01 am
Subject: Re: How to use generators?
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Anderson  
View profile  
 More options Nov 9 2005, 1:58 pm
Newsgroups: comp.lang.python
From: Tom Anderson <t...@urchin.earth.li>
Date: Wed, 9 Nov 2005 18:58:45 +0000
Local: Wed, Nov 9 2005 1:58 pm
Subject: Re: How to use generators?

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/nevison.pub/web102/web102S00/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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Vincent  
View profile  
 More options Nov 10 2005, 3:33 am
Newsgroups: comp.lang.python
From: Ian Vincent <i_vinc...@hotmail.com>
Date: 10 Nov 2005 08:33:20 GMT
Local: Thurs, Nov 10 2005 3:33 am
Subject: Re: How to use generators?
Tom Anderson <t...@urchin.earth.li> wrote in
news:Pine.LNX.4.62.0511091853420.25586@urchin.earth.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.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google