Skip to content

Create 0071-simplify-path.js #2614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 4, 2023
Merged

Create 0071-simplify-path.js #2614

merged 4 commits into from
Jul 4, 2023

Conversation

aadil42
Copy link
Contributor

@aadil42 aadil42 commented Jun 21, 2023

Solved simplify-path in JS.

File(s) Added: 0071-simplify-path.js
Language(s) Used: JavaScript
Submission URL: https://leetcode.com/problems/simplify-path/submissions/986179327/

Solved simplify-path in JS.
var simplifyPath = function(path) {
let currunt = '';
let myStack = [];
path = '/' + path + '/';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Use interpolation

path = `/${path}/`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually. We can just remove that third if statement. if the stack is empty then nothing would happen. We don't even need to check it. I updated the code please have a look.

Copy link
Collaborator

@aakhtar3 aakhtar3 Jul 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aadil42 This can be simplified

/**
 * Stack
 * Time O(N) | Space O(N)
 * https://leetcode.com/problems/simplify-path
 * @param {string} path
 * @return {string}
 */
var simplifyPath = (path, slash = '/', stack = []) => {    
    const paths = path.split(slash).filter(Boolean);

    for (const _path of paths) traversePath(_path, stack);

    return `${slash}${stack.join(slash)}`;
};

const traversePath = (path, stack) => {
    if (canPush(path)) return stack.push(path);

    if (canPop(path, stack)) stack.pop();
};

const canPush = (path) => !(
    isCurrentDirectory(path) ||
    isParentDirectory(path)
);

const canPop = (path, stack) =>
    isParentDirectory(path) &&
    !isEmpty(stack);

const isCurrentDirectory = (path) => (path === '.');

const isParentDirectory = (path) => (path === '..');

const isEmpty = ({ length }) => (0 === length);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, it's more readable. It's intimidating at first but if you try to read the code it's quite better than mine. Nicely Modularized. I have updated the file. Please have a look.

for(let i = 0; i < path.length; i++) {

console.log(myStack);
if(path[i] === '/') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Avoid nesting more than 3 times

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any other way. Can you kindly show me How can I avoid nested if statements and still get the logic right? Thank you :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use continue; when you need to exit early to avoid else

aadil42 added 3 commits July 2, 2023 21:19
Removed unnecessary nested if statment
Modularizing the code.
@aakhtar3 aakhtar3 merged commit cf13469 into neetcode-gh:main Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants