Skip to content

Instantly share code, notes, and snippets.

@alexsotocx
Last active November 18, 2021 11:39
Show Gist options
  • Save alexsotocx/fa6b050971d826a07c6fed6db48cc5af to your computer and use it in GitHub Desktop.
Save alexsotocx/fa6b050971d826a07c6fed6db48cc5af to your computer and use it in GitHub Desktop.
Coding
/*
Create a function to simulate change directory(cd) in your terminal, we want to find
in which folder you end up.
This function will automate the “$cd” command, so every item in the array is a cd command
Given a an array of paths where you will use cd path[i], find the result folder
Input:
paths string[], where paths[i] is a valid path.
paths[0] = will be your start folder, so it will be a valid path directory.
example 1:
paths[] = {"/usr/user_a/home", "..", "desktop", "../.."} -> "/usr"
explanation:
cd /usr/user_a/home -> current path "/usr/user_a/home"
cd .. -> go up one folder, current path "/usr/user_a/"
cd desktop -> move to folder desktop, current path "/usr/user_a/desktop"
cd "../.." -> move two folders up, current path "/usr"
example 2:
args string[] = {"/usr/user_a/home", "..", "desktop/../../user_b",} -> "/usr/user_b"
cd /usr/user_a/home -> current path "/usr/user_a/home"
cd .. -> go up one folder, current path "/usr/user_a/"
cd "desktop/../../user_b" -> move to folder desktop, current path "/usr/user_a/desktop", then move two folders up "/usr/", then move to user_b "/usr/user_b"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment