avp26/u8/program.py
Your Name 5f0c63d05f u8
2026-11-05 18:34:15 +01:00

82 lines
1.9 KiB
Python

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()