Skip to content

Instantly share code, notes, and snippets.

@s8sg
Created April 29, 2022 06:20
Show Gist options
  • Save s8sg/0cd45f26c495481571f8a5bf401434c0 to your computer and use it in GitHub Desktop.
Save s8sg/0cd45f26c495481571f8a5bf401434c0 to your computer and use it in GitHub Desktop.
Java Implementation of String Path Changer (cd <dir>)
package com.codility;
import java.util.EmptyStackException;
import java.util.Stack;
public class StringPathChanger {
public static final String PARENT_DIR_NOTATION = "..";
public static final String SINGLE_SPACE = " ";
public static final String DIR_PARTITION_NOTATION = "/";
public static final String CD_COMMAND = "cd ";
public String changeDirectoryString(String currentDirectory, String command) {
// currentDirectory : absolute current path
// command: cd ../..
if(currentDirectory == null || command == null) {
throw new IllegalStateException();
}
String[] listSource = validateCurrentDirectory(currentDirectory);
String[] listDest = validateCommand(command);
boolean absoluteDestination = isAbsoluteDestination(command);
if(absoluteDestination) {
return getAbsoluteDestination(listDest);
}
return getRelativeDestination(listSource, listDest);
}
private String getRelativeDestination(String[] listSource, String[] listDest) {
Stack<String> directoryStack = new Stack<>();
for(String dir : listSource) {
directoryStack.push(dir);
}
traverseRelativeDirectory(listDest, directoryStack);
if(directoryStack.isEmpty()) {
return DIR_PARTITION_NOTATION;
}
return DIR_PARTITION_NOTATION + String.join(DIR_PARTITION_NOTATION, directoryStack);
}
private String getAbsoluteDestination(String[] listDest) {
Stack<String> directoryStack = new Stack<>();
traverseRelativeDirectory(listDest, directoryStack);
if(directoryStack.isEmpty()) {
return DIR_PARTITION_NOTATION;
}
return DIR_PARTITION_NOTATION + String.join(DIR_PARTITION_NOTATION, directoryStack);
}
private void traverseRelativeDirectory(String[] listDest, Stack<String> directoryStack) {
for (String dir : listDest) {
// if '..' we pop the directory from stack
if ((dir.equals(PARENT_DIR_NOTATION))) {
// if no where to go, we stick to root
try {
directoryStack.pop();
} catch (EmptyStackException e) {
}
}
// else we push it into stack
else {
directoryStack.push(dir);
}
}
}
private boolean isAbsoluteDestination(String command) {
String[] commandPartitions = command.split(SINGLE_SPACE);
return commandPartitions[1].startsWith(DIR_PARTITION_NOTATION);
}
private String[] validateCurrentDirectory(String currentDirectory) {
if(currentDirectory.isEmpty()) {
throw new IllegalStateException();
}
if(!currentDirectory.startsWith(DIR_PARTITION_NOTATION)) {
throw new IllegalStateException();
}
// remove initial '/'
return currentDirectory.substring(1).split(DIR_PARTITION_NOTATION);
}
private String[] validateCommand(String command) {
if(command.isEmpty()) {
throw new IllegalStateException();
}
if(!command.startsWith(CD_COMMAND)) {
throw new IllegalStateException();
}
String[] commandPartitions = command.split(SINGLE_SPACE);
if(commandPartitions.length != 2) {
throw new IllegalStateException();
}
if(commandPartitions[1].startsWith(DIR_PARTITION_NOTATION)) {
commandPartitions[1] = commandPartitions[1].substring(1);
}
return commandPartitions[1].split(DIR_PARTITION_NOTATION);
}
}
@djnautiyal
Copy link

Here's my code:

package com.codility;

import java.util.*;

public class StringPathChanger {
public String changeDirectoryString(String dir, String command) {
System.out.print("dir: " + dir + " command:" + command);

    if(dir == null || command == null || !command.startsWith("cd ")) throw new IllegalStateException();

    command = command.substring(3).trim();
    
    if(command.startsWith("/")) return command;
    
    Deque<String> stack  = new ArrayDeque<>();

    for(String part: dir.split("/")){
        stack.push(part);
    }


    //System.out.print(Arrays.toString(command.split("/"))); 

    for(String part: command.split("/")){
        if(part.equals(".."))
            stack.pop();
        
        else if(!part.equals("."))
            stack.push(part);
    }


    StringBuilder res = new StringBuilder();
    List<String> list = new ArrayList<>();
    while(!stack.isEmpty()) {
        list.add(stack.pop());
    }
    Collections.reverse(list);


    for(String part: list){
        res.append(part);
        res.append("/");
    }

    res.setLength(res.length()-1);
    System.out.print(" ans: " + res.toString() + "\n");
    return res.toString();

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment