Skip to content

Commit

Permalink
file system
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Oct 31, 2024
1 parent c52e324 commit b5c27a4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ This repo register my evolution in the Cracking The Coding Interview book as I g

[BoggleBoard](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/data/structure/tree/BoggleBoard.java)

[File System Recursion]()

# AWS Challenges

[Review Score](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/aws/ReviewScore.java)
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/algorithm/recursion/FileSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package algorithm.recursion;

import java.util.ArrayList;
import java.util.List;

public class FileSystem {

public static class Directory {

String name;
List<Directory> sub = new ArrayList<>();
List<String> files = new ArrayList<>();

}


/**
* Recursively search for files in a directory that match the query (either full filename or extension).
*
* @param directory obj directory
* @param query file name or extension
* @return return file full path list. every single place that it was found
*/
public List<String> searchFiles(Directory directory, String query) {

final List<String> pathsList = new ArrayList<>();

for (String fileName : directory.files) {

String[] nameExt = fileName.split("\\.");

if (query.equals(fileName) || query.substring(1).equals(nameExt[1]))
pathsList.add(directory.name + "/" + fileName);
}

if (directory.sub.isEmpty())
return pathsList;

List<String> subPath = new ArrayList<>();
for (Directory subdirectory : directory.sub) {
subPath.addAll(searchFiles(subdirectory, query));
}

for (String path : subPath) {
pathsList.add(directory.name + "/" + path);
}

return pathsList;
}

}
52 changes: 52 additions & 0 deletions src/test/java/algorithm/recursion/FileSystemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package algorithm.recursion;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class FileSystemTest {

@Test
public void test() {

FileSystem.Directory root = new FileSystem.Directory();
root.name = "root";

FileSystem.Directory folder1 = new FileSystem.Directory();
folder1.name = "folder1";
folder1.files.add("file1.txt");
folder1.files.add("file2.doc");

FileSystem.Directory subFolder1 = new FileSystem.Directory();
subFolder1.name = "subFolder1";

FileSystem.Directory folder2 = new FileSystem.Directory();
folder2.name = "folder2";
folder2.sub.add(subFolder1);

subFolder1.files.add("file3.txt");
subFolder1.files.add("file4.jpg");

folder2.files.add("file5.pdf");

root.sub.add(folder1);
root.sub.add(folder2);
root.files.add("file6.txt");

FileSystem fileSystem = new FileSystem();

var paths = fileSystem.searchFiles(root, ".txt");
Assertions.assertEquals(paths.get(0), "root/file6.txt");
Assertions.assertEquals(paths.get(1), "root/folder1/file1.txt");
Assertions.assertEquals(paths.get(2), "root/folder2/subFolder1/file3.txt");

paths = fileSystem.searchFiles(root, ".doc");
Assertions.assertEquals(paths.get(0), "root/folder1/file2.doc");

paths = fileSystem.searchFiles(root, ".jpg");
Assertions.assertEquals(paths.get(0), "root/folder2/subFolder1/file4.jpg");

paths = fileSystem.searchFiles(root, "file5.pdf");
Assertions.assertEquals(paths.get(0), "root/folder2/file5.pdf");

}
}

0 comments on commit b5c27a4

Please sign in to comment.