diff --git a/u8/program.py b/u8/program.py new file mode 100644 index 0000000..0ff66ca --- /dev/null +++ b/u8/program.py @@ -0,0 +1,81 @@ +import sys + +class Node: + def __init__(self, text): + self.text = text + self.left = None + self.right = None + +def main(): + print("Expert zo zoo to vie.") + + raw_input = sys.stdin.read() + lines = raw_input.splitlines() + iterator = iter(lines) + + try: + def parse(): + try: + line = next(iterator) + except StopIteration: + raise ValueError() + if line == "": + raise ValueError() + node = Node(line) + if line.endswith("?"): + node.left = parse() + node.right = parse() + return node + + root = parse() + + try: + sep = next(iterator) + except StopIteration: + raise ValueError() + + if sep != "": + raise ValueError() + + except ValueError: + print("Chybna databaza") + return + + def count_nodes(node): + if node is None: + return 0, 0 + if node.left is None and node.right is None: + return 0, 1 + ql, al = count_nodes(node.left) + qr, ar = count_nodes(node.right) + return 1 + ql + qr, al + ar + + q_count, a_count = count_nodes(root) + + print(f"Pocet otazok: {q_count}") + print(f"Pocet odpovedi: {a_count}") + print("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.") + + curr = root + while True: + print(curr.text) + if curr.left is None and curr.right is None: + print("Koniec") + break + + try: + choice = next(iterator) + except StopIteration: + print("Koniec vstupu") + break + + if choice == "a": + curr = curr.left + elif choice == "n": + curr = curr.right + else: + print("Nerozumiem") + break + +if __name__ == "__main__": + main()