1

the problem is here:

https://binarysearch.com/problems/Unix-Path-Resolution

Given a Unix path, represented as a list of strings, return its resolved version.

In Unix, ".." means to go to the previous directory and "." means to stay on the current directory. By resolving, we mean to evaluate the two symbols so that we get the final directory we're currently in.

Constraints

  • n ≤ 100,000 where n is the length of path

Example 1
Input

path = ["usr", "..", "usr", ".", "local", "bin", "docker"]

Output

["usr", "local", "bin", "docker"]

Explanation

The input represents "/usr/../usr/./local/bin" which resolves to "/usr/local/bin/docker"

Example 2
Input

path = ["bin", "..", ".."]

Output

[]

Explanation

The input represents "/bin/../.." which resolves to "/"

my correct solution:

class Solution:
    def solve(self, path):
        
        stack = []

        for s in path:
            if s == "..":
                if len(stack)> 0:
                    stack.pop()
            elif s == ".":                   #if "elif" is replaced with "if" - it gives error
                continue
            else:
                stack.append(s)
        print(stack)
        return stack

on this test case - ["..","..","..","..","..","..","."], the below code

class Solution:
    def solve(self, path):
        
        stack = []

        for s in path:
            if s == "..":
                if len(stack)> 0:
                    stack.pop()
            if s == ".":                   # "elif" is replaced with if and gives error
                continue
            else:
                stack.append(s)
        print(stack)
        return stack

the expected result is [], but my code gives [".."] - wrong result.

The input is not a string where a for loop can mistake ".." vs "." - 1dot and 2dot.

The input is a list and it should clearly distinguish btw ".." and "." - so i assumed if/elif is not critical.

why does simple replacing elif with "if" gives wrong result?

4
  • 1
    Well, you assumed wrong. In the second code, whenever s is not equal to "." you will append it to the stack, even if it's "..". You could add a continue in the if s == "..": block to get the same behavior as with elif, but IMHO elif is the better choice and makes it clear that you have multiple cases to check and each one doing something different. Commented Apr 26, 2022 at 15:35
  • Lazy and probably too slow would be return os.path.abspath('/'.join(path)).split("/")? Commented Apr 26, 2022 at 15:41
  • @PatrickArtner, thx and the question is testing how to use stack , not about knowing shortest 1liner in python Commented Apr 26, 2022 at 18:17
  • 1
    @ERJAN nothing in your questions OR under the link binarysearch.com/problems/Unix-Path-Resolution limits HOW you solve the task. You choose to use stacks - but htat is not the only way to solve it. From a learning experience using a stack is good. knowing what methods python has that can do stuff is good as well :) Commented Apr 27, 2022 at 7:04

1 Answer 1

1

There is actually differences between if-elif-else and if-if-else. In simple terms, if-elif-else is regarded as "one body" while if-if-else is in fact two conditional blocks: if and if-else.

First we look at the if-elif-else version. If s is "..", it will never reach the code inside -elif or -else. Sensible.

However for the if-if-else version of code, as we said it actually goes through two blocks of if-else logic. If s is "..", it first goes through the first if statement. Then it proceed to the next if-else block. Since s does not equal ".", so it proceeeds to the code in the else block, which is I believe not what you want.

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.