def decode(chromosome):
"""Takes a chromosome (list) and returns an expression."""
# this needs to be a mini state machine.
# We expect a stream of number + operator pairs terminated with a number
output = ""
need_op = False
for key in chromosome:
gene = genes[key]
if need_op:
if gene in operators:
output += gene
need_op = False
else:
continue
else:
if gene in digits:
output += gene
need_op = True
else:
continue
if not need_op:
# we don't want an op hanging off the end
output = output[:len(output)-1]
return output
评论列表
文章目录