u8
This commit is contained in:
parent
e2afd1cabd
commit
0b8574adbd
78
u8/zoo.py
Normal file
78
u8/zoo.py
Normal file
@ -0,0 +1,78 @@
|
||||
def read_line():
|
||||
try:
|
||||
line = input()
|
||||
except EOFError:
|
||||
return None
|
||||
if line.endswith("\r"):
|
||||
line = line[:-1]
|
||||
return line
|
||||
|
||||
|
||||
def load_tree():
|
||||
root = None
|
||||
stack = []
|
||||
questions = answers = 0
|
||||
|
||||
while True:
|
||||
line = read_line()
|
||||
if not line:
|
||||
return None
|
||||
|
||||
node = [line, None, None]
|
||||
if root is None:
|
||||
root = node
|
||||
else:
|
||||
parent = stack[-1]
|
||||
if parent[1] is None:
|
||||
parent[1] = node
|
||||
else:
|
||||
parent[2] = node
|
||||
stack.pop()
|
||||
|
||||
if line.endswith("?"):
|
||||
questions += 1
|
||||
stack.append(node)
|
||||
else:
|
||||
answers += 1
|
||||
|
||||
if not stack:
|
||||
break
|
||||
|
||||
if read_line() != "":
|
||||
return None
|
||||
return root, questions, answers
|
||||
|
||||
|
||||
def main():
|
||||
print("Expert zo zoo to vie.")
|
||||
|
||||
result = load_tree()
|
||||
if result is None:
|
||||
print("Chybna databaza")
|
||||
return
|
||||
node, questions, answers = result
|
||||
|
||||
print(f"Pocet otazok: {questions}")
|
||||
print(f"Pocet odpovedi: {answers}")
|
||||
print("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.")
|
||||
print(node[0])
|
||||
|
||||
while node[1] is not None:
|
||||
choice = read_line()
|
||||
if choice is None:
|
||||
print("Koniec vstupu")
|
||||
return
|
||||
if choice == "a":
|
||||
node = node[1]
|
||||
elif choice == "n":
|
||||
node = node[2]
|
||||
else:
|
||||
print("Nerozumiem")
|
||||
return
|
||||
print(node[0])
|
||||
|
||||
print("Koniec")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in New Issue
Block a user