Programming Quiz 4.8

Hi,
Why my answer in this quiz is displaying as "incorrect" whereas code is displaying right result. Can anyone help ? Answer of quiz should be [11,4,5].. and I am getting same answer . what should I do to make it displayed as"correct" after submitting it?

def check_goal(node):
    if (node[0] == goal[0]) and (node[1]==goal[1]):
        return True
def Add(delta,node):
    neighbor = []
    for i in range(len(delta)):
      x = node[0] + delta[i][0]
      y = node[1] + delta[i][1]
      if(x>=0 and x<len(grid)and(y>=0 and y<len(grid[0]))):
          neighbor.append([x,y])
    return neighbor
def search():
    g = 0
    x = init[0]
    y = init[1]
    closed = []
    opened = [[g,x,y]]
    found = False
    resign = False
    while (found is False) and (resign is False):
        if (len(opened)==0):
            resign= True
            print "Fail to complete search"
        else:
            opened.sort()
            opened.reverse()
            nextt = opened.pop()
            g = nextt[0]
            x = nextt[1]
            y = nextt[2]
            node = [x,y]
            if (check_goal(node)):
                found = True
                print nextt
            else:
                n = Add(delta,node)
                g = g+cost
                for i in range(len(n)):
                    x2 = n[i][0]
                    y2 = n[i][1]
                    if (n[i] not in closed)and(grid[x2][y2]==0):
                        opened.append([g,x2,y2])
            closed.append([x,y])
    return found

print search()

asked 14 Mar '12, 04:05

zaib's gravatar image

zaib
2543619
accept rate: 0%

closed 13 Apr '12, 15:09


7 Answers:

Hi! Make sure your search function returns (and not just prints) a list in the form of [optimal path length, x, y]. See the user instructions at the top of the provided code.

link

answered 14 Mar '12, 04:10

MichaelUdacity's gravatar image

MichaelUdacity ♦♦
2.9k62231

edited 14 Mar '12, 04:13

You have to change

        if (check_goal(node)):
            found = True
            print nextt

to:

        if (check_goal(node)):
            found = True
            return nextt

and

        resign= True
        print "Fail to complete search"

to

        resign= True
        return "fail"

because right now you print out the right answer, but return a boolean True, and that is now what is asked

link

answered 14 Mar '12, 06:13

Gundega's gravatar image

Gundega ♦♦
44.0k70170315

I typed "print search()" to make sure it is working correctly ....if I omit "print" and write only "search()" .. even then "incorrect" message is displayed

link

answered 14 Mar '12, 06:08

zaib's gravatar image

zaib
2543619

still incorrect :(

link

answered 14 Mar '12, 13:27

zaib's gravatar image

zaib
2543619

I'm having similar trouble. The comment block at the beginning of the template code says to "return" [g,x,y] - but in the video that follows it looks like what they wanted was print [g,x,y]. Either way I can't get it accept my routine. Frustrating as it functionally worked with all the test cases from the video.

Maybe something similar to the HW3.6 issue

edit:
switching the code back to "return" the values, resetting the template, then pasting the code back in made it work. This is a problem for homework where you only get one try...

link

answered 14 Mar '12, 13:57

Chad%20Seymour-1's gravatar image

Chad Seymour-1
122

edited 14 Mar '12, 14:06

If you change position of 1 as shown in grid of quiz 4.8 .. you will get [11,4,5] .. for different position of "1" in grid we can get different values for g,x and y.
you are right .. may be it is the same problem as what happened in home works.

link

answered 14 Mar '12, 15:33

zaib's gravatar image

zaib
2543619

/facepalm I was toying around and changed the grid in my example. Sorry for trying to confuse you!

have you tried to do the "reset" trick and paste only the function itself, leaving last line intact ?

(14 Mar '12, 15:42) Gundega ♦♦ Gundega's gravatar image

yes I did

link

answered 14 Mar '12, 15:45

zaib's gravatar image

zaib
2543619

Your answer
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: 14 Mar '12, 04:05

Seen: 262 times

Last updated: 13 Apr '12, 15:10