Created
April 5, 2019 02:05
-
-
Save Glavak/4ad6d4143cc8b8053105d9a2d2915593 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
# Вспомогательная функция, её наличие не обязательно и не будет проверяться | |
def build_tree(start, end, path): | |
link_re = re.compile(r"(?<=/wiki/)[\w()]+") # Искать ссылки можно как угодно, не обязательно через re | |
files = dict.fromkeys(os.listdir(path)) # Словарь вида {"filename1": None, "filename2": None, ...} | |
queue = [start] | |
i = 0 | |
while i < len(queue): | |
current_file = queue[i] | |
with open(path + current_file, 'r') as content_file: | |
content = content_file.read() | |
for f in re.findall(link_re, content): | |
if f not in files or f is None: | |
continue | |
if files[f] == None: | |
files[f] = current_file | |
if f not in queue: | |
queue.append(f) | |
i += 1 | |
return files | |
# Вспомогательная функция, её наличие не обязательно и не будет проверяться | |
def build_bridge(start, end, path): | |
files = build_tree(start, end, path) | |
bridge = [end] | |
while bridge[0] != start: | |
bridge.insert(0, files[bridge[0]]) | |
return bridge | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment