Created
August 10, 2022 09:30
-
-
Save kirilltobola/5c3bb5e49220281fe8d2fa8c27afc378 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/python3 | |
class tree(): | |
def add(self, variable): | |
self.variables.append(variable) | |
def create(self, namespace): | |
namespace.parent = self | |
self.children.append(namespace) | |
def get(self, item): | |
if item not in self.variables: | |
parent = self.parent | |
while parent: | |
if item in parent.variables: | |
return parent.name | |
parent = parent.parent | |
return None | |
return self.name | |
def __init__(self, name, parent=None): | |
self.name = name | |
self.parent = parent | |
self.variables = [] | |
self.children = [] | |
def __str__(self): | |
return f"parent: {self.parent}, name: {self.name}, vars: {self.variables}, children: {self.children}" | |
global_tree = tree('global') | |
def bfs(item, start=global_tree): | |
queue = [start] | |
while queue: | |
current_tree = queue.pop(0) | |
if item == current_tree.name: | |
return current_tree | |
for child in current_tree.children: | |
queue.append(child) | |
return None | |
def create(name, parent_name): | |
parent = bfs(parent_name) | |
parent.create(tree(name)) | |
def add(namespace, item): | |
parent = bfs(namespace) | |
parent.add(item) | |
def get(namespace, var): | |
parent = bfs(namespace) | |
print(parent.get(var)) | |
if __name__ == '__main__': | |
iterations = int(input()) | |
for _ in range(iterations): | |
command, arg1, arg2 = input().split() | |
if command == 'add': | |
add(arg1, arg2) | |
elif command == 'create': | |
create(arg1, arg2) | |
elif command == 'get': | |
get(arg1, arg2) | |
else: | |
print('undefined command') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment