Apologies for any possible confusion about the title. I will try my best to illustrate my question.
Context:
I just finished working on an interesting coding problem on CodeWars where I was asked to write a function to simplify a list of directions.
The input will look like ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
The 'SOUTH' 'NORTH' and 'EAST' 'WEST' that are next to each other shall both be deleted since it makes little sense to go north and then immediately south. And the list shall be simplified until it can't be simplified anymore.
For instance:
["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
shall be simplified to
['WEST']
Dummy OP wrote a clunky function that does it. But my question is about a really clever answer posted by someone else.
Code:
def dirReduc(arr):
dir = " ".join(arr)
dir2 = dir.replace("NORTH SOUTH",'').replace("SOUTH NORTH",'').replace("EAST WEST",'').replace("WEST EAST",'')
dir3 = dir2.split()
return dirReduc(dir3) if len(dir3) < len(arr) else dir3
I played with it and see that it works but just couldn't wrap my head around how.
I see that in the return statement, it shall run again unless the result has the same length as input, which is understandable. But the code uses dirReduc(dir3) which seems wrong to me.
If I run dirReduc(dir3) then my input will be dir3, but then inside the frame of the function, another dir3 is created. So now when the function goes to the final return statement it shall be return dirReduc(dir3) if len(dir3) < len(dir3) else dir3. And since len(dir3) == len(dir3)', it will just returndir3`.
In my understanding, the function will be run twice, max with each input, which is not the truth. So which part of my understanding is wrong?
Sorry for the sheer volume of words, I think after all this is a simple problem. There must be a very fundamental thing that I'm getting wrong.
dir3are local variables to each function execution, they are unrelated to each other.dir3generated inside the function is local variable? Which is unrelated to which? And when the return statement evaluateslen(dir3), hasn't it been changed to the local variable already?['NORTH', 'WEST', 'SOUTH']