avp26/u8/program.py
2026-11-04 17:47:40 +01:00

120 lines
2.2 KiB
Python

import sys
class Node:
def __init__(self, text):
self.text = text
self.right = None
self.left = None
self.otazka = text.endswith("?")
def para_stromov(lines, index):
if index >= len(lines):
raise ValueError("Chybna databaza")
text = lines[index]
if not text:
raise ValueError("Chybna databaza")
node = Node(text)
index += 1
if node.otazka:
node.left, index = para_stromov(lines, index)
node.right, index = para_stromov(lines, index)
return node, index
def pocet_nodov(node):
if not node:
return 0, 0
if node.otazka:
a_left, n_left = pocet_nodov(node.left)
a_right, n_right = pocet_nodov(node.right)
return 1 + a_left + a_right, n_left + n_right
else:
return 0, 1
def main():
lines = []
while True:
try:
line = input()
except EOFError:
break
line = line.rstrip("\r")
if line == "":
lines.append("")
break
lines.append(line)
for line in sys.stdin:
lines.append(line.rstrip("\r\n"))
print("Expert zo zoo to vie.")
if not lines:
print("Chybna databaza")
return
try:
prvi = lines.index("")
except ValueError:
print("Chybna databaza")
return
stromlines = lines[:prvi]
vibir = [l.strip() for l in lines[prvi + 1 :] if l.strip() != ""]
if not stromlines:
print("Chybna databaza")
return
try:
root, druhy = para_stromov(stromlines, 0)
except Exception:
print("Chybna databaza")
return
if druhy != len(stromlines):
print("Chybna databaza")
return
n_pocet, a_pocet = pocet_nodov(root)
print(f"Pocet otazok: {n_pocet}")
print(f"Pocet odpovedi: {a_pocet}")
print("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.")
potocni = root
viber_indx = 0
while potocni.otazka:
print(potocni.text)
if viber_indx >= len(vibir):
print("Koniec vstupu")
return
choice = vibir[viber_indx]
viber_indx += 1
if choice != "a" and choice != "n":
print("Nerozumiem")
return
if choice == "a":
potocni = potocni.left
else:
potocni = potocni.right
print(potocni.text)
print("Koniec")
if __name__ == "__main__":
main()