Skip to content

Instantly share code, notes, and snippets.

@johandahlberg
Created April 26, 2019 16:40
Show Gist options
  • Save johandahlberg/b86656c679379d57165a3ef628360940 to your computer and use it in GitHub Desktop.
Save johandahlberg/b86656c679379d57165a3ef628360940 to your computer and use it in GitHub Desktop.
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