def getElements(bot, update, args): equation = "".join(args) user = Chemistry(equation) if user.check_equation(): # if equation is written right elements = user.returnElements() if elements == False: # if something went wrong during the balance bot.send_message(chat_id=update.message.chat_id, text=chemistry.balance_error) equation_example(bot, update) else: # if it went all right bot.send_message(chat_id=update.message.chat_id, text=elements) else: # if equation wasn't written right bot.send_message(chat_id=update.message.chat_id, text=chemistry.balance_error) equation_example(bot, update)
def checkbalance(bot, update, args): equation = "".join(args) user = Chemistry(equation) if user.check_equation(): # if equation is written right balanced = user.printifBalanced() if balanced == False: # if something went wrong during the balance bot.send_message(chat_id=update.message.chat_id, text=chemistry.balance_error) equation_example(bot, update) else: # if it went all right bot.send_message(chat_id=update.message.chat_id, text=balanced) else: # if equation wasn't written right bot.send_message(chat_id=update.message.chat_id, text=chemistry.balance_error) equation_example(bot, update)
def __init__(self, file_name_preq, file_name_opt, heteroatom_list, epochs, percents, population_size=4, code_mode='binary'): self.population_size = population_size self.code = Code(file_name_preq, file_name_opt, heteroatom_list, code_mode) self.epochs = epochs self.percents = percents self.percents = self.distribute() self.population = self.code.generate_population(self.population_size) self.current_epoch = 0 self.heteroatom_list = heteroatom_list self.chemistry = Chemistry(self.population_size) self._write_file() self._write_head_lines()
def balance(bot, update, args): equation = "".join(args) user = Chemistry(equation) if user.check_equation(): # if the equation is written right balanced = user.balance() if balanced == False: # if something went wrong during the balance bot.send_message(chat_id=update.message.chat_id, text="Something" " went wrong during the balance of the equation.") equation_example(bot, update) else: # went all right bot.send_message(chat_id=update.message.chat_id, text=balanced) else: # if the equation isn't written right bot.send_message(chat_id=update.message.chat_id, text="Something went " "wrong during the balance of the equation.") equation_example(bot, update)
import random from sys import argv from chemistry import Chemistry #random.seed(0) chemfile = 'data/world.chem' if len(argv) > 2: chemfile = argv[2] chem = Chemistry(open(chemfile).read()) quantities = [random.randint(0, 100) for i in chem.species] total_matter = sum(quantities) # iteration previous_quantities = [1] iterations = 0 print quantities while quantities != previous_quantities and iterations < 1000: previous_quantities = quantities quantities = chem.react(list(previous_quantities)) iterations += 1 print quantities print 'starting matter:', total_matter print 'end matter:', sum(quantities) print 'iterations:', iterations