Create a function that simulates terminal cd (change directory) commands to determine the final path. It starts at the root (/) and processes each command in order: "cd ": moves into the specified directory. "cd ..": moves up one level (if not at root). "cd .": does nothing (stay in the current directory). "cd /": resets to root.
Anonymous
def di_sol(commands): path = [] for com in commands: if com.startswith("cd "): c = com[3:] if c == "/": path = [] elif c == "..": if path: path.pop() elif c != ".": path.append(c) return "/" + "/".join(path)
Check out your Company Bowl for anonymous work chats.