Is it efficient solution for 4-8 quiz?

I'm not very familiar with python, so can you please tell me which improvements can be done here?
Thanks.

def in_range(grid, state):
    if state[0] >= 0 and state[1] >= 0 and state[0] < len(grid) and state[1] < len(grid[state[0]]):
        return True
    return False

def search():
    # ----------------------------------------
    # insert code here and make sure it returns the appropriate result
    # ----------------------------------------
    state = [0] + init

    marked = []
    for i in xrange(len(grid)):
        marked.append([0]*len(grid[i]))

    marked[state[1]][state[2]] = 1

    expanded = [state]
    while len(expanded) > 0:
        expanded.sort(lambda a, b: b[0] - a[0])
        state = expanded.pop()
        if goal == [state[1], state[2]]:
            print state
            return
        for i in xrange(len(delta)):
            x = state[1] + delta[i][0]
            y = state[2] + delta[i][1]
            if in_range(grid, [x, y]) and grid[x][y] != 1 and marked[x][y] != 1:
                expanded.append([state[0] + cost, x, y])
                marked[x][y] = 1

    print 'fail'

asked 13 Mar '12, 04:20

Alexander%20Grebennik's gravatar image

Alexander Gr...
1773717
accept rate: 55%

retagged 24 Mar '12, 23:38

Matt%20Bell's gravatar image

Matt Bell
2.1k31342

Be the first one to answer this question!
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×5,185
×195

Asked: 13 Mar '12, 04:20

Seen: 81 times

Last updated: 13 Mar '12, 04:20