Created
April 29, 2022 06:20
-
-
Save s8sg/0cd45f26c495481571f8a5bf401434c0 to your computer and use it in GitHub Desktop.
Java Implementation of String Path Changer (cd <dir>)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);
}