def helper_search(prev_row, max_c, element_list, last_index, repetitions, empty_formula, terminate, restrict): this_row = [(0, empty_formula)] while repetitions < 1 and last_index < len(element_list): last_index += 1 if last_index == len(element_list): terminate = True else: this_mass = element_list[last_index]['freqisotope']['mass'] * scalar this_mass_int = element_list[last_index]['freqisotope']['mass_int'] * scalar repetitions = int(math.ceil(max_c / min(this_mass, this_mass_int))) + 1 repetitions -= 1 if not terminate: this_mass = element_list[last_index]['freqisotope']['mass'] * scalar this_mass_int = element_list[last_index]['freqisotope']['mass_int'] * scalar for c in range(1, max_c): if min(this_mass, this_mass_int) < c: (prev_mass, prev_formula) = prev_row[c] (other_mass, other_formula) = prev_row[c - this_mass_int] if prev_mass < this_mass + other_mass: new_formula = other_formula.copy() new_formula[element_list[last_index]] += 1 if the_7rules.rule1(new_formula,restrict): this_row.append((this_mass + other_mass, new_formula)) else: this_row.append(prev_row[c]) else: this_row.append(prev_row[c]) else: this_row.append(prev_row[c]) return this_row, last_index, repetitions, terminate
def helper_search(mass_min, mass_max, formula_mass, formula, last_index, delta, restrict): """ This function is called when the formula_mass is less than the mass_max It attempts to add a new element and if still in the mass tolerance window and complies with the first rule calls itself recursively :param mass_min: Minimum mass accepted for solution :param mass_max: Maximum mass accepted for solution :param formula_mass: The mass of the formula now :param formula: The formula now :param last_index: The last element added :param delta: Computation error allowed :return: not explicit, it adds to global list of formulas all 7rules complying formulas """ for index, element in enumerate(formula): if index >= last_index: new_formula_mass = formula_mass + element['freqisotope']['mass'] if mass_max - new_formula_mass >= -delta: new_formula = formula.copy() new_formula[element] += 1 if new_formula_mass - mass_min >= -delta: # formula in tolerance interval, add it to solutions if filter_formula(new_formula, restrict): formulas.append(new_formula) else: if rule1(new_formula, restrict): # still some mass left, keep searching helper_search(mass_min, mass_max, new_formula_mass, new_formula, index, delta, restrict)