Question
What is the shortest and most efficient (preferrable most efficient) way of reading in a single depth folder and creating a file tree that consists of the longest substrings of each file?
start with this
.
├── hello
├── lima_peru
├── limabeans
├── limes
├── limit
├── what_are_those
├── what_is_bofa
└── what_is_up
end with this
.
├── hello
├── lim
│ ├── lima
│ │ ├── lima_peru
│ │ └── limabeans
│ ├── limes
│ └── limit
└── what_
├── what_are_those
└── what_is_
├── what_is_bofa
└── what_is_up
Framing
I feel like I know how to do the naive version of this problem but I wanted to see what the best way to do this in python would be.
Format
def longest_substrings_filetree(old_directory: str, new_directory: str):
...
You can use a trie, or prefix tree, which is a data structure that achieves exactly what you are looking for. Here is a link to numerous python implementations for a python trie.
How to create a trie in Python