0

So, here is the code I am running and it's giving me a TypeError. I am trying to traverse a 2d array and then returning the path from starting point to the target point.

I applied Breadth-first search for the path traversing but seems like something is wrong with the algorithm.

class Grid:
    def __init__(self, str1):
        self.maze = str1.splitlines()

    def get_start_cordinates(self):
        rr = 0
        cc = 0
        return rr, cc

    def main(self, r, c):
        queue = []
        visited = {}
        visited[(r, c)] = (-1, -1)
        queue.append((r, c))
        while len(queue) > 0:
            r, c = queue.pop(0)

            if r == 4 and c == 2:
                path_actual = []
                while r != -1:
                    path_actual.append((r, c))
                    r, c = visited[(r, c)]
                path_actual.reverse()
                return path_actual

            # avoid repetition of code: make a loop
            for dx, dy in ((-1, 0), (0, -1), (1, 0), (0, 1), (1, 1), (1, -1), (-1, 1), (-1, -1)):
                new_r = r + dy
                new_c = c + dx
                if (0 <= new_r < len(self.maze) and
                        0 <= new_c < len(self.maze[0]) and
                        not (new_r, new_c) in visited):
                    visited[(new_r, new_c)] = (r, c)
                    queue.append((new_r, new_c))


maze = Grid("""1 12 2 0 0
2 11 1 11 0
3 2 -1 9 0""")

path = Grid.main(*Grid.get_start_cordinates())
print(path)

This is the error I am getting:

path = Grid.main(*Grid.get_start_cordinates())

TypeError: get_start_cordinates() missing 1 required positional argument: 'self'

6
  • 1
    Instead of Grid.main(...) call maze.main(...) Commented Sep 24, 2019 at 10:41
  • @kuco23 thank you so much. Also, can you please check whether this code is correct or not? Since I am not getting the desired output. Commented Sep 24, 2019 at 10:50
  • 1
    Your logic seems flawed. Try to dry run it and see where you're going wrong. Commented Sep 24, 2019 at 10:53
  • 1
    @NaumanNaeem okay. Thanks for the help. Will update this question after checking the whole algo by myself because it's just giving me output as "None". Thanks again. Commented Sep 24, 2019 at 11:04
  • 2
    @Sanya I found a couple of things wrong here. First when calling len on a string it will give you the length of the string, while you want the number of elements inside. I think inside __init__ it should be self.maze = [s.split() for s in str1.splitlines()] and you reversed the roles of c and r. r should be the number of lines, as you increase it by dy and c should be the number of columns, so just write if r == 2 and c == 4 Commented Sep 24, 2019 at 11:16

1 Answer 1

1
path = maze.main(*maze.get_start_cordinates())

use the object you created not the class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.