reorganise directories

upd chapter_01/binary.py
This commit is contained in:
2025-03-26 01:27:35 +00:00
parent ab09aa5269
commit 4cd7bb7d18
43 changed files with 54 additions and 53 deletions

8
chapter_07/README.md Normal file
View File

@@ -0,0 +1,8 @@
# BFS DFS on rooted tree (connected acyclic graph)
The BFS example uses a queue which results in a breadth first search. When a directory is found its contents are appended to the queue to be processed later
The DFS example uses the call stack which results in a depth first search. When a directory is found it is recursively passed to files_with_extension to be processed immediately.
Note. DFS cannot be used to find the shortest path. In the mango seller example, a DFS search may have found a second or third degree
seller before a first. However, DFS may be used to find the topological sort.

31
chapter_07/bfs_dirtrav.py Normal file
View File

@@ -0,0 +1,31 @@
import logging
from collections import deque
from pathlib import Path
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
EXT = ".rb"
scripts_path = Path.home() / "scripts"
def files_with_extension(start_directory):
queue = deque()
queue.append(start_directory)
while queue:
items = queue.popleft()
for item in items.glob("*"):
# if item is a file and has extension EXT then print
if item.is_file():
if item.suffix == EXT:
print(item)
# otherwise append directory to the queue
else:
queue.append(item)
files_with_extension(scripts_path)

24
chapter_07/dfs_dirtrav.py Normal file
View File

@@ -0,0 +1,24 @@
import logging
from pathlib import Path
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
EXT = ".rb"
scripts_path = Path.home() / "scripts"
def files_with_extension(directory):
for item in directory.glob("*"):
# if item is a file and has extension EXT then print
if item.is_file():
if item.suffix == EXT:
print(item)
# otherwise pass directory to recursive call
else:
files_with_extension(item)
files_with_extension(scripts_path)