u8
This commit is contained in:
parent
02e2c2fd21
commit
f08b7b7c28
94
u8/program.py
Normal file
94
u8/program.py
Normal file
@ -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()
|
||||
Loading…
Reference in New Issue
Block a user