In [7]:
!cat inventory-03.txt
In [8]:
!cat formulas-03.txt
helium 1
hydrogen 2
water 2
In [9]:
def main(inventory_file, formula_file):
inventory = read_inventory(inventory_file)
formulas = read_formulas(formula_file)
counts = calculate_counts(inventory, formulas)
show_counts(counts)
In [10]:
def read_inventory(inventory_file):
result = {}
with open(inventory_file, 'r') as reader:
for line in reader:
name, count = line.strip().split()
result[name] = int(count)
return result
In [11]:
print read_inventory('inventory-03.txt')
In [12]:
def read_inventory(inventory_file):
result = {}
with open(inventory_file, 'r') as reader:
for line in reader:
line = line.strip()
if (not line) or line.startswith('#'):
continue
name, count = line.split()
result[name] = int(count)
return result
print read_inventory('inventory-03.txt')
In [13]:
def read_formulas(formula_file):
result = {}
with open(formula_file, 'r') as reader:
for line in reader:
line = line.strip()
if (not line) or line.startswith('#'):
continue
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split()
formula = {}
for i in range(0, len(atoms), 2):
symbol = atoms[i].strip()
count = int(atoms[i+1])
formula[symbol] = count
result[name] = formula
return result
In [14]:
print read_formulas('formulas-03.txt')
In [15]:
def calculate_counts(inventory, formulas):
counts = {}
for name in formulas:
counts[name] = dict_divide(inventory, formulas[name])
return counts
In [16]:
def dict_divide(inventory, molecule):
number = None
for atom in molecule:
required = molecule[atom]
available = inventory.get(atom, 0)
limit = available / required
if (number is None) or (limit < number):
number = limit
return number
None
rather than some arbitrary large value
In [17]:
def show_counts(counts):
names = counts.keys()
names.sort()
for name in names:
print name, counts[name]
In [18]:
!cat inventory-00.txt
In [19]:
!cat formulas-00.txt
In [20]:
main('inventory-00.txt', 'formulas-00.txt')
In [21]:
!cat formulas-01.txt
In [22]:
main('inventory-00.txt', 'formulas-01.txt')
In [23]:
!cat inventory-01.txt
In [24]:
main('inventory-01.txt', 'formulas-01.txt')
In [25]:
!cat inventory-02.txt
In [26]:
!cat formulas-02.txt
In [27]:
main('inventory-02.txt', 'formulas-02.txt')
In [28]:
!cat inventory-03.txt
In [29]:
main('inventory-03.txt', 'formulas-02.txt')
read_inventory
and read_formulas
to use it
In [33]:
def readlines(filename):
result = []
with open(filename, 'r') as reader:
for line in reader:
line = line.strip()
if line and (not line.startswith('#')):
result.append(line)
return result
In [34]:
def read_inventory(inventory_file):
result = {}
for line in readlines(inventory_file):
name, count = line.split()
result[name] = int(count)
return result
In [ ]:
def read_formulas(formula_file):
result = {}
for line in readlines(formula_file):
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split()
formula = {}
for i in range(0, len(atoms), 2):
symbol = atoms[i].strip()
count = int(atoms[i+1])
formula[symbol] = count
result[name] = formula
return result
In [ ]:
def read_formulas(formula_file):
result = {}
for line in readlines(formula_file):
name, atoms = line.split(':')
name = name.strip()
result[name] = make_formula(atoms)
return result
def make_formula(atoms):
formula = {}
atoms = atoms.strip().split()
for i in range(0, len(atoms), 2):
symbol = atoms[i].strip()
count = int(atoms[i+1])
formula[symbol] = count
return formula
In [35]:
main('inventory-00.txt', 'formulas-00.txt')
In [36]:
main('inventory-01.txt', 'formulas-01.txt')
In [37]:
main('inventory-02.txt', 'formulas-02.txt')
In [38]:
main('inventory-03.txt', 'formulas-03.txt')
read_inventory