-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c52e324
commit b5c27a4
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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; | ||
} | ||
|
||
} |
This file contains 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
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"); | ||
|
||
} | ||
} |