Created
April 26, 2019 16:40
-
-
Save johandahlberg/b86656c679379d57165a3ef628360940 to your computer and use it in GitHub Desktop.
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
from pathlib import Path | |
from queue import Queue | |
def bf_search(search_for, root, max_depth): | |
class PathLevel(): | |
def __init__(self, path, level): | |
self.path = path | |
self.level = level | |
def bf_search_helper(queue): | |
if queue.empty(): | |
return None | |
e = queue.get() | |
if e.level > max_depth: | |
return None | |
if e.path.name == search_for: | |
return e.path | |
dirs = [x for x in e.path.iterdir() if x.is_dir()] | |
for d in dirs: | |
queue.put(PathLevel(path=d, level=e.level + 1)) | |
return bf_search_helper(queue) | |
init_queue = Queue() | |
init_queue.put(PathLevel(path=Path(root), level=0)) | |
return bf_search_helper(init_queue) | |
if __name__ == "__main__": | |
print(bf_search('redo', '/home/', 10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment