Last active
August 29, 2016 23:45
-
-
Save compiler-errors/b439a4f5e281c4486ee7ef57c5be36c8 to your computer and use it in GitHub Desktop.
Jeremy's thing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
things = """ | |
L 855.91 | |
PhNHPz 158.072 | |
tBuPhNHPz 214.135 | |
pyDAPz 239.093 | |
OAc 59.014 | |
Fe 55.933 | |
O 15.996 | |
OH 17.003 | |
H2O 18.011 | |
Cl 34.969 | |
OTf 148.952 | |
""" | |
limits = """ | |
L = 1 | |
PhNHPz < 5 | |
tBuPhNHPz < 5 | |
pyDAPz < 5 | |
Fe < 8 | |
O < 3 | |
OH < 3 | |
H2O < 3 | |
Cl < 5 | |
""" | |
target_mass = 1736 | |
threshold = 2 # The `abs(x-x') < precision` we need to sum our masses up to | |
# ----------------------------- # | |
# ----------------------------- # | |
# ----------------------------- # | |
# Lets parse the things first | |
new_things = {} | |
for line in things.split("\n"): | |
line_split = line.split() | |
if len(line_split) == 2: | |
new_things[line_split[0]] = float(line_split[1]) | |
def parse_limit(thing, op, num): | |
num = int(num) | |
if op == "<": | |
def limit(soln): | |
if thing in soln: | |
return soln[thing] < num | |
return True | |
return limit | |
elif op == ">": | |
def limit(soln): | |
if thing not in soln: | |
return False | |
return soln[thing] > num | |
return limit | |
elif op == "=": | |
def limit(soln): | |
if thing not in soln: | |
return num == 0 | |
return soln[thing] == num | |
return limit | |
else: | |
raise Exception("Unknown comparison `%s`!" % op) | |
new_limits = [] | |
for line in limits.split("\n"): | |
line_split = line.split() | |
if len(line_split) == 3: | |
limit = parse_limit(line_split[0], line_split[1], line_split[2]) | |
new_limits.append(limit) | |
limits = new_limits | |
things = new_things | |
keys = list(things.keys()) | |
def search(solution, i, leftover_mass): | |
global things | |
global threshold | |
global keys | |
if i != len(things): | |
solution = solution[:] # Clone the solution (sin) | |
while leftover_mass > 0: | |
if abs(leftover_mass) < threshold: | |
break | |
search(solution, i + 1, leftover_mass) | |
solution[i] += 1 | |
leftover_mass -= things[keys[i]] | |
if abs(leftover_mass) < threshold: | |
# We gotta prepare a proper solution to print now | |
soln = {} | |
mass = 0 | |
for i in range(0, len(things)): | |
if solution[i] == 0: | |
continue | |
mass += things[keys[i]] * solution[i] | |
soln[keys[i]] = solution[i] | |
for limit in limits: | |
if not limit(soln): | |
return | |
print("Solution:", soln, "with mass", mass) | |
search([0] * len(things), 0, target_mass) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment