From f08b7b7c28ba9ee814fb11b951217aa61cd8a51c Mon Sep 17 00:00:00 2001 From: MartinSkovran Date: Tue, 3 Nov 2026 13:46:31 +0100 Subject: [PATCH] u8 --- u8/program.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 u8/program.py diff --git a/u8/program.py b/u8/program.py new file mode 100644 index 0000000..a899368 --- /dev/null +++ b/u8/program.py @@ -0,0 +1,94 @@ +class Node: + def __init__(self, text): + self.text = text + self.left = None + self.right = None + + def is_question(self): + return self.text.endswith("?") + + +def read_line(): + try: + return input() + except EOFError: + return None + + +class Tree: + def __init__(self): + self.root = None + self.question_count = 0 + self.answer_count = 0 + + def load(self): + pending_questions = [] + + while True: + text = read_line() + if text is None: + return False + + if text == "": + if self.root is None or pending_questions: + return False + return True + + node = Node(text) + if self.root is None: + self.root = node + else: + if not pending_questions: + return False + + parent = pending_questions[-1] + if parent.left is None: + parent.left = node + else: + parent.right = node + pending_questions.pop() + + if node.is_question(): + self.question_count += 1 + pending_questions.append(node) + else: + self.answer_count += 1 + + def find_animal(self): + current_node = self.root + + while True: + print(current_node.text) + if not current_node.is_question(): + print("Koniec") + return + + choice = read_line() + if choice is None: + print("Koniec vstupu") + return + if choice == "a": + current_node = current_node.left + elif choice == "n": + current_node = current_node.right + else: + print("Nerozumiem") + return + + +def main(): + print("Expert zo zoo to vie.") + tree = Tree() + if not tree.load(): + print("Chybna databaza") + return + + print(f"Pocet otazok: {tree.question_count}") + print(f"Pocet odpovedi: {tree.answer_count}") + print("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.") + + tree.find_animal() + + +if __name__ == "__main__": + main()