@virnuls/

Classification

Python

One of the OCR Coding Challenges

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Using a list to represent a binary tree - numbers are indexes for ease of maintenance
# Using an array would allow null (i.e. numbered) elements to be omitted
# A more balanced tree produces a shorter, less sparsely populated list
tree = [0,"QIs it a mammal","QDoes it spend all of its time on land","QIs it a bird","QIs it feline","QDoes it ever leave the water","QCan it fly","QIs it an insect","QIs it a domestic pet","QCan it be milked","Aseal","QDoes it have conical teeth and a single blowhole","Asparrow","QDoes it swim","QDoes it sting","QHas it got eight limbs","Acat","QDoes it have stripes","QDoes it produce wool","QIs it usually ridden",20,21,"Adolphin","Awhale",24,25,"Apenguin","Aostrich","QDoes it produce honey","QDoes it have a narrow waist","QDoes it live in water","Asquid",32,33,"Atiger","Alion","Asheep","Acow","Ahorse","QIs it farmed",40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,"Abee","Awasp","Aant","Atermite","Aoctopus","Aspider",62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,"Apig","Adog"]

current = 1
print("Answer the questions to identify an animal from the following list: horse, cow, sheep, pig, dog, cat, lion, tiger, whale, dolphin, seal, penguin, ostrich, sparrow, spider, ant, bee, wasp, termite, octopus, squid\n")
while tree[current][0] == "Q":
    answer = input(tree[current][1:] + "? [Y/N]").lower()
    if answer in ("y","yes","ye","yeah"):
        current = current * 2
    else:
        current = current * 2 + 1
print("My guess would be...",tree[current][1:])