From b5c27a454e5d5712df287963651cca56e5b055ef Mon Sep 17 00:00:00 2001 From: brendon <36212954+brendonmiranda@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:03:51 +0000 Subject: [PATCH] file system --- README.md | 2 + .../java/algorithm/recursion/FileSystem.java | 51 ++++++++++++++++++ .../algorithm/recursion/FileSystemTest.java | 52 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/main/java/algorithm/recursion/FileSystem.java create mode 100644 src/test/java/algorithm/recursion/FileSystemTest.java diff --git a/README.md b/README.md index b756942..dd35471 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/main/java/algorithm/recursion/FileSystem.java b/src/main/java/algorithm/recursion/FileSystem.java new file mode 100644 index 0000000..99a699f --- /dev/null +++ b/src/main/java/algorithm/recursion/FileSystem.java @@ -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 sub = new ArrayList<>(); + List 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 searchFiles(Directory directory, String query) { + + final List 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 subPath = new ArrayList<>(); + for (Directory subdirectory : directory.sub) { + subPath.addAll(searchFiles(subdirectory, query)); + } + + for (String path : subPath) { + pathsList.add(directory.name + "/" + path); + } + + return pathsList; + } + +} diff --git a/src/test/java/algorithm/recursion/FileSystemTest.java b/src/test/java/algorithm/recursion/FileSystemTest.java new file mode 100644 index 0000000..58d11a4 --- /dev/null +++ b/src/test/java/algorithm/recursion/FileSystemTest.java @@ -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"); + + } +}