83 lines
2.0 KiB
Python
83 lines
2.0 KiB
Python
import sys
|
|
|
|
def main():
|
|
print("Expert zo zoo to vie.")
|
|
tree_lines = read_tree_lines()
|
|
if not controla_batabaz(tree_lines):
|
|
return
|
|
q = pocet_otazok(tree_lines)
|
|
a = pocet_odpovedi(tree_lines)
|
|
tree = build_tree(tree_lines)
|
|
if tree is None or len(tree) == 0:
|
|
print("Chybna databaza")
|
|
return
|
|
print("Pocet otazok:", q)
|
|
print("Pocet odpovedi:", a)
|
|
print("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.")
|
|
koren = tree
|
|
while koren is not None:
|
|
if koren["otazka"].endswith("?"):
|
|
print(koren["otazka"])
|
|
try:
|
|
odpoved = input().rstrip("\r")
|
|
except EOFError:
|
|
print("Koniec vstupu")
|
|
return
|
|
if odpoved == "a":
|
|
koren = koren["a"]
|
|
elif odpoved == "n":
|
|
koren = koren["n"]
|
|
else:
|
|
print("Nerozumiem")
|
|
return
|
|
else:
|
|
print(koren["otazka"])
|
|
print("Koniec")
|
|
break
|
|
|
|
def build_tree(tree_lines):
|
|
if len(tree_lines) == 0:
|
|
return None
|
|
line = tree_lines.pop(0)
|
|
node = {"otazka": line, "a": None, "n": None}
|
|
if line.endswith("?"):
|
|
node["a"] = build_tree(tree_lines)
|
|
node["n"] = build_tree(tree_lines)
|
|
return node
|
|
|
|
def pocet_otazok(tree_lines):
|
|
a = 0
|
|
for line in tree_lines:
|
|
if line.endswith("?"):
|
|
a += 1
|
|
return a
|
|
def pocet_odpovedi(tree_lines):
|
|
a = 0
|
|
for line in tree_lines:
|
|
if not line.endswith("?"):
|
|
a += 1
|
|
return a
|
|
|
|
def read_tree_lines():
|
|
tree_lines = []
|
|
try:
|
|
text = input().rstrip("\r")
|
|
while text:
|
|
tree_lines.append(text)
|
|
text = input().rstrip("\r")
|
|
except EOFError:
|
|
pass
|
|
return tree_lines
|
|
|
|
def controla_batabaz(tree_lines):
|
|
if tree_lines is None or len(tree_lines) == 0:
|
|
print("Chybna databaza")
|
|
return False
|
|
return True
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|