0

I have tried many times to make a program that finds a path by itself in a table. However I always manage to make a function that works only once.

Here is the function that I've made:

def PathSearch(StartingPoint, Table, List= [], PointsAlreadyVisited = []): #tuple, double List, List, List
#Initialisation des variables
    LU, LL, LR = [], [], []
    PointsAlreadyVisited  += [StartingPoint]
    PositionAbove = (StartingPoint[0],StartingPoint[1]-1)
    value_above = Table[PositionAbove[1]][PositionAbove[0]]
    PositionRight = (StartingPoint[0]+1,StartingPoint[1])
    value_right = Table[PositionRight[1]][PositionRight[0]]
    PositionLeft = (StartingPoint[0]-1,StartingPoint[1])
    value_left= Table[PositionLeft[1]][PositionLeft[0]]
    Retour = []

if value_above== 0 and (PositionAbove not in PointsAlreadyVisited ): 
    LU = Liste + [PositionAbove]
    if PositionAbove[1] != 1:
        LU = PathSearch(PositionAbove, Table, LU)
if value_right== 0  and (PositionRight not in PointsAlreadyVisited ): #This checks that there is a path on the right and that we haven't checked that position already.
    LR = PathSearch(PositionRight, Table, List +[PositionRight])
if value_left == 0  and (PositionLeft not in PointsAlreadyVisited ): #This checks that there is a path on the left and that we haven't checked that position already.
    LL = PathSearch(PositionLeft, Table, List +[PositionLeft])
if value_left != 0 and value_right!= 0 and value_above!= 0: #This is the case where there is no more path (either on left, right or above.
    retour =[-1]
#This part is to determine the shortest path:
    else: 
        minimum = Minimum_Liste(LL, LR, LU)
        if minimum == [-1]: 
            minimum_bis = Minimum_Liste(Maximum_Liste(LL,LU), Maximum_Liste(LU, LR))
            if minimum_bis == [-1]: 
                retour = Maximum_Liste(Maximum_Liste(LL, LU), Maximum_Liste(LR, LU))
            else:
                retour = minimum_bis
        else:
            retour = minimum
    return(retour)

The function works when I call it once, however when I call it a second time with the same parameters, it doesn't work.

The problem lies in the PointsAlreadyVisited list: When I run the function a second time, the list is still full of the position checked during the last call.

I have tried to use the "clear" function for lists but I can't manage to implement it without breaking the function.

If anyone has any thoughts on how to solve this problem, I'd be grateful, Thanks !

  • 2
    Probably because of the mutable default argument: ["Least Astonishment" and the Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – Stef Feb 10 '22 at 14:00
  • Try replacing `List= [], PointsAlreadyVisited = []` in the arguments with `List= None, PointsAlreadyVisited = None`; and at the beginning of the function, add two conditionals `if List is None: List = []; if PointsAlreadyVisited is None: PointsAlreadyVisited = []` – Stef Feb 10 '22 at 14:02
  • Did you try making the initial call supplying value for all of the arguments, including the ones w/ defaults? – Scott Hunter Feb 10 '22 at 14:03
  • Arguably the [#1 mistake](https://www.toptal.com/python/top-10-mistakes-that-python-programmers-make) that Python programmers make. Understandable why it behaves this way, if you know Python well enough, but a horrible result imo. – jarmod Feb 10 '22 at 14:10
  • @Stef It worked, thank you so much ! I didn't know that List don't automatically reset. Thanks again !! – Nicolas Chaplain Feb 11 '22 at 09:37

0 Answers0