Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many...
Transcript of Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many...
![Page 1: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/1.jpg)
Nanotech Example
Sets and Dictionaries
Nanotech Example
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.
![Page 2: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/2.jpg)
How many molecules of different kinds can we
make using the atoms in our warehouse?
Sets and Dictionaries Nanotech Example
![Page 3: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/3.jpg)
How many molecules of different kinds can we
make using the atoms in our warehouse?
# Molecular formula file
helium : He 1
water : H 2 O 1
hydrogen : H 2
Sets and Dictionaries Nanotech Example
![Page 4: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/4.jpg)
How many molecules of different kinds can we
make using the atoms in our warehouse?
# Molecular formula file
helium : He 1
water : H 2 O 1
hydrogen : H 2
# Atom inventory file
He 1
H 4
O 3
Sets and Dictionaries Nanotech Example
![Page 5: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/5.jpg)
How many molecules of different kinds can we
make using the atoms in our warehouse?
# Molecular formula file
helium : He 1
water : H 2 O 1
hydrogen : H 2
# Atom inventory file
He 1
H 4
O 3
Now have all the tools we need
Sets and Dictionaries Nanotech Example
Now have all the tools we need
![Page 6: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/6.jpg)
Natural to represent inventory as dictionary
Sets and Dictionaries Nanotech Example
![Page 7: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/7.jpg)
Natural to represent inventory as dictionary
Keys: atomic symbols
Sets and Dictionaries Nanotech Example
![Page 8: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/8.jpg)
Natural to represent inventory as dictionary
Keys: atomic symbols
Values: number of atoms availableValues: number of atoms available
Sets and Dictionaries Nanotech Example
![Page 9: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/9.jpg)
Natural to represent inventory as dictionary
Keys: atomic symbols
Values: number of atoms availableValues: number of atoms available
'He'
'H'1
Sets and Dictionaries Nanotech Example
'O'
3
4
![Page 10: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/10.jpg)
Represent individual molecules the same way
Sets and Dictionaries Nanotech Example
![Page 11: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/11.jpg)
Represent individual molecules the same way
water
'O'
'H'
2
water
Sets and Dictionaries Nanotech Example
1
![Page 12: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/12.jpg)
Store formulas as a dictionary of dictionaries
Sets and Dictionaries Nanotech Example
![Page 13: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/13.jpg)
Store formulas as a dictionary of dictionaries
Keys: molecule names
Sets and Dictionaries Nanotech Example
![Page 14: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/14.jpg)
Store formulas as a dictionary of dictionaries
Keys: molecule names
Values: dictionaries of formulasValues: dictionaries of formulas
Sets and Dictionaries Nanotech Example
![Page 15: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/15.jpg)
Store formulas as a dictionary of dictionaries
Keys: molecule names
Values: dictionaries of formulasValues: dictionaries of formulas
'water'
'ammonia'
Sets and Dictionaries Nanotech Example
![Page 16: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/16.jpg)
Number of molecules that can be made is:
minavailable[atom]
required[atom]min required[atom]
atom ∈ formula
Sets and Dictionaries Nanotech Example
![Page 17: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/17.jpg)
Number of molecules that can be made is:
minavailable[atom]
required[atom]min required[atom]
atom ∈ formula
If atom not in available, its count is implicitly 0
Sets and Dictionaries Nanotech Example
![Page 18: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/18.jpg)
Number of molecules that can be made is:
minavailable[atom]
required[atom]min required[atom]
atom ∈ formula
If atom not in available, its count is implicitly 0
Store results in yet another dictionary
Sets and Dictionaries Nanotech Example
![Page 19: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/19.jpg)
Number of molecules that can be made is:
minavailable[atom]
required[atom]min required[atom]
atom ∈ formula
If atom not in available, its count is implicitly 0
Store results in yet another dictionary
Keys: molecule names
Sets and Dictionaries Nanotech Example
Keys: molecule names
![Page 20: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/20.jpg)
Number of molecules that can be made is:
minavailable[atom]
required[atom]min required[atom]
atom ∈ formula
If atom not in available, its count is implicitly 0
Store results in yet another dictionary
Keys: molecule names
Sets and Dictionaries Nanotech Example
Keys: molecule names
Values: counts of how many can be made
![Page 21: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/21.jpg)
'''Calculate how many molecules of each type can be made
with the atoms on hand.'''
importimportimportimport sys
ifififif __name__ == '__main__':ifififif __name__ == '__main__':
inventory = read_inventory(sys.argv[1])
formulas = read_formulas(sys.argv[2])
counts = calculate_counts(inventory, formulas)
show_counts(counts)
Sets and Dictionaries Nanotech Example
![Page 22: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/22.jpg)
defdefdefdef read_inventory(filename):
'''Read inventory of available atoms.'''
result = {}
forforforfor line inininin read_lines(filename):
name, count = line.split(' ')name, count = line.split(' ')
result[name] = intintintint(count)
returnreturnreturnreturn result
Sets and Dictionaries Nanotech Example
![Page 23: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/23.jpg)
defdefdefdef read_lines(filename):
'''Read lines from file, stripping out
blank lines and comments.'''
reader = openopenopenopen(filename, 'r')
lines = []lines = []
forforforfor line inininin reader:
line = line.split('#')[0].strip()
ifififif line:
lines.append(line)
reader.close()
returnreturnreturnreturn lines
Sets and Dictionaries Nanotech Example
returnreturnreturnreturn lines
![Page 24: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/24.jpg)
defdefdefdef read_formulas(filename):
'''Read molecular formulas from file.'''
result = {}
forforforfor line inininin read_lines(filename):
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split(' ')
formula = {}
forforforfor i inininin rangerangerangerange(0, lenlenlenlen(atoms), 2):
formula[atoms[i]] = int(atoms[i+1])
Sets and Dictionaries Nanotech Example
formula[atoms[i]] = int(atoms[i+1])
result[name] = formula
returnreturnreturnreturn result
![Page 25: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/25.jpg)
defdefdefdef read_formulas(filename):
'''Read molecular formulas from file.'''
result = {}
forforforfor line inininin read_lines(filename):
Storing results
in dictionary
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split(' ')
formula = {}
forforforfor i inininin rangerangerangerange(0, lenlenlenlen(atoms), 2):
formula[atoms[i]] = int(atoms[i+1])
Sets and Dictionaries Nanotech Example
formula[atoms[i]] = int(atoms[i+1])
result[name] = formula
returnreturnreturnreturn result
![Page 26: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/26.jpg)
defdefdefdef read_formulas(filename):
'''Read molecular formulas from file.'''
result = {}
forforforfor line inininin read_lines(filename):
For each
interesting
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split(' ')
formula = {}
forforforfor i inininin rangerangerangerange(0, lenlenlenlen(atoms), 2):
formula[atoms[i]] = int(atoms[i+1])
line in the
input file...
Sets and Dictionaries Nanotech Example
formula[atoms[i]] = int(atoms[i+1])
result[name] = formula
returnreturnreturnreturn result
![Page 27: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/27.jpg)
defdefdefdef read_formulas(filename):
'''Read molecular formulas from file.'''
result = {}
forforforfor line inininin read_lines(filename):Separate the
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split(' ')
formula = {}
forforforfor i inininin rangerangerangerange(0, lenlenlenlen(atoms), 2):
formula[atoms[i]] = int(atoms[i+1])
Separate the
molecule name
and the formula
Sets and Dictionaries Nanotech Example
formula[atoms[i]] = int(atoms[i+1])
result[name] = formula
returnreturnreturnreturn result
![Page 28: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/28.jpg)
defdefdefdef read_formulas(filename):
'''Read molecular formulas from file.'''
result = {}
forforforfor line inininin read_lines(filename):Separate the
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split(' ')
formula = {}
forforforfor i inininin rangerangerangerange(0, lenlenlenlen(atoms), 2):
formula[atoms[i]] = int(atoms[i+1])
Separate the
atoms and
their counts
Sets and Dictionaries Nanotech Example
formula[atoms[i]] = int(atoms[i+1])
result[name] = formula
returnreturnreturnreturn result
![Page 29: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/29.jpg)
defdefdefdef read_formulas(filename):
'''Read molecular formulas from file.'''
result = {}
forforforfor line inininin read_lines(filename):
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split(' ')
formula = {}
forforforfor i inininin rangerangerangerange(0, lenlenlenlen(atoms), 2):
formula[atoms[i]] = int(atoms[i+1])
Loop over pairs
of atoms and
counts
Sets and Dictionaries Nanotech Example
formula[atoms[i]] = int(atoms[i+1])
result[name] = formula
returnreturnreturnreturn result
![Page 30: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/30.jpg)
defdefdefdef read_formulas(filename):
'''Read molecular formulas from file.'''
result = {}
forforforfor line inininin read_lines(filename):
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split(' ')
formula = {}
forforforfor i inininin rangerangerangerange(0, lenlenlenlen(atoms), 2):
formula[atoms[i]] = int(atoms[i+1])
Store the count
Sets and Dictionaries Nanotech Example
formula[atoms[i]] = int(atoms[i+1])
result[name] = formula
returnreturnreturnreturn result
as an integer
with the atomic
symbol as key
![Page 31: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/31.jpg)
defdefdefdef read_formulas(filename):
'''Read molecular formulas from file.'''
result = {}
forforforfor line inininin read_lines(filename):
name, atoms = line.split(':')
name = name.strip()
atoms = atoms.strip().split(' ')
formula = {}
forforforfor i inininin rangerangerangerange(0, lenlenlenlen(atoms), 2):
formula[atoms[i]] = int(atoms[i+1])
And store the
Sets and Dictionaries Nanotech Example
formula[atoms[i]] = int(atoms[i+1])
result[name] = formula
returnreturnreturnreturn result
molecule in
the main
dictionary
![Page 32: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/32.jpg)
defdefdefdef calculate_counts(inventory, formulas):
'''Calculate how many of each molecule
can be made with inventory.'''
counts = {}
forforforfor name inininin formulas:forforforfor name inininin formulas:
counts[name] = dict_divide(inventory,
formulas[name])
returnreturnreturnreturn counts
Sets and Dictionaries Nanotech Example
![Page 33: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/33.jpg)
defdefdefdef calculate_counts(inventory, formulas):
'''Calculate how many of each molecule
can be made with inventory.'''
counts = {}
forforforfor name inininin formulas:forforforfor name inininin formulas:
counts[name] = dict_divide(inventory,
formulas[name])
returnreturnreturnreturn countsSub-dictionary holding
atom counts for a
Sets and Dictionaries Nanotech Example
particular molecule
![Page 34: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/34.jpg)
defdefdefdef calculate_counts(inventory, formulas):
'''Calculate how many of each molecule
can be made with inventory.'''
counts = {}
forforforfor name inininin formulas:forforforfor name inininin formulas:
counts[name] = dict_divide(inventory,
formulas[name])
returnreturnreturnreturn counts
Big functions: nothing is obviously wrong
Sets and Dictionaries Nanotech Example
![Page 35: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/35.jpg)
defdefdefdef calculate_counts(inventory, formulas):
'''Calculate how many of each molecule
can be made with inventory.'''
counts = {}
forforforfor name inininin formulas:forforforfor name inininin formulas:
counts[name] = dict_divide(inventory,
formulas[name])
returnreturnreturnreturn counts
Big functions: nothing is obviously wrong
Sets and Dictionaries Nanotech Example
Small functions: obviously, nothing is wrong
![Page 36: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/36.jpg)
defdefdefdef dict_divide(inventory, molecule):
'''Calculate how much of a single molecule
can be made with inventory.'''
number = NoneNoneNoneNone
forforforfor atom inininin molecule:forforforfor atom inininin molecule:
required = molecule[atom]
available = inventory.get(atom, 0)
limit = available / required
ifififif (number isisisis None) orororor (limit < number):
number = limit
returnreturnreturnreturn number
Sets and Dictionaries Nanotech Example
returnreturnreturnreturn number
![Page 37: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/37.jpg)
defdefdefdef dict_divide(inventory, molecule):
'''Calculate how much of a single molecule
can be made with inventory.'''
number = NoneNoneNoneNone
forforforfor atom inininin molecule:
Identical format:
keys are atoms,forforforfor atom inininin molecule:
required = molecule[atom]
available = inventory.get(atom, 0)
limit = available / required
ifififif (number isisisis None) orororor (limit < number):
number = limit
returnreturnreturnreturn number
keys are atoms,
values are counts
Sets and Dictionaries Nanotech Example
returnreturnreturnreturn number
![Page 38: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/38.jpg)
defdefdefdef dict_divide(inventory, molecule):
'''Calculate how much of a single molecule
can be made with inventory.'''
number = NoneNoneNoneNone
forforforfor atom inininin molecule:forforforfor atom inininin molecule:
required = molecule[atom]
available = inventory.get(atom, 0)
limit = available / required
ifififif (number isisisis None) orororor (limit < number):
number = limit
returnreturnreturnreturn number
Common pattern: None
Sets and Dictionaries Nanotech Example
returnreturnreturnreturn numbermeans "uninitialized",
so initialize the first
time through the loop
![Page 39: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/39.jpg)
defdefdefdef dict_divide(inventory, molecule):
'''Calculate how much of a single molecule
can be made with inventory.'''
number = NoneNoneNoneNone
forforforfor atom inininin molecule:forforforfor atom inininin molecule:
required = molecule[atom]
available = inventory.get(atom, 0)
limit = available / required
ifififif (number isisisis None) orororor (limit < number):
number = limit
returnreturnreturnreturn number
Common pattern:
Sets and Dictionaries Nanotech Example
returnreturnreturnreturn numberstored value or default
![Page 40: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/40.jpg)
defdefdefdef dict_divide(inventory, molecule):
'''Calculate how much of a single molecule
can be made with inventory.'''
number = NoneNoneNoneNone
forforforfor atom inininin molecule:forforforfor atom inininin molecule:
required = molecule[atom]
available = inventory.get(atom, 0)
limit = available / required
ifififif (number isisisis None) orororor (limit < number):
number = limit
returnreturnreturnreturn numberCommon pattern:
Sets and Dictionaries Nanotech Example
returnreturnreturnreturn number
find minimum of
calculated values
![Page 41: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/41.jpg)
defdefdefdef show_counts(counts):
'''Show how many of each molecule can be made.'''
counts = invert_dict(counts)
forforforfor key inininin sortedsortedsortedsorted(counts.keys(), reverse=TrueTrueTrueTrue):
forforforfor name inininin sortedsortedsortedsorted(counts[key]):forforforfor name inininin sortedsortedsortedsorted(counts[key]):
printprintprintprint key, name
Sets and Dictionaries Nanotech Example
![Page 42: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/42.jpg)
defdefdefdef show_counts(counts):
'''Show how many of each molecule can be made.'''
counts = invert_dict(counts)
forforforfor key inininin sortedsortedsortedsorted(counts.keys(), reverse=TrueTrueTrueTrue):
forforforfor name inininin sortedsortedsortedsorted(counts[key]):forforforfor name inininin sortedsortedsortedsorted(counts[key]):
printprintprintprint key, name
Reverse to get
Sets and Dictionaries Nanotech Example
greatest first
![Page 43: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/43.jpg)
defdefdefdef invert_dict(src):
'''Invert a dictionary, returning value->set{key}.'''
dst = {}
forforforfor key inininin src:
value = src[key]value = src[key]
ifififif value notnotnotnot inininin dst:
dst[value] = set()set()set()set()
dst[value].add(key)
returnreturnreturnreturn dst
Sets and Dictionaries Nanotech Example
![Page 44: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/44.jpg)
defdefdefdef invert_dict(src):
'''Invert a dictionary, returning value->set{key}.'''
dst = {}
forforforfor key inininin src:
value = src[key]value = src[key]
ifififif value notnotnotnot inininin dst:
dst[value] = set()set()set()set()
dst[value].add(key)
returnreturnreturnreturn dst
Common pattern:
Sets and Dictionaries Nanotech Example
make sure there's a
collection, then add
![Page 45: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/45.jpg)
defdefdefdef show_counts(counts):
'''Show how many of each molecule can be made.'''
counts = invert_dict(counts)
forforforfor key inininin sortedsortedsortedsorted(counts.keys(), reverse=TrueTrueTrueTrue):
ifififif key > 0:ifififif key > 0:
forforforfor name inininin sortedsortedsortedsorted(counts[key]):
printprintprintprint key, name
Go back and only
Sets and Dictionaries Nanotech Example
show molecules that
can actually be made
![Page 46: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/46.jpg)
# inventory-00.txt # formulas-01.txt
helium : He 1
No output, which is correct.
Sets and Dictionaries Nanotech Example
![Page 47: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/47.jpg)
# inventory-01.txt
He 1
# formulas-01.txt
helium : He 1
1 helium
Sets and Dictionaries Nanotech Example
![Page 48: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/48.jpg)
# inventory-02.txt
He 1
H 4
# formulas-01.txt
helium : He 1
1 helium
Sets and Dictionaries Nanotech Example
![Page 49: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/49.jpg)
# inventory-02.txt
He 1
H 4
# formulas-02.txt
helium : He 1
water : H 2 O 1
1 helium
Sets and Dictionaries Nanotech Example
![Page 50: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/50.jpg)
# inventory-02.txt
He 1
H 4
# formulas-03.txt
helium : He 1
water : H 2 O 1
hydrogen : H 2
2 hydrogen
1 helium
hydrogen : H 2
Sets and Dictionaries Nanotech Example
![Page 51: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/51.jpg)
# inventory-03.txt
He 1
H 4
O 3
# formulas-03.txt
helium : He 1
water : H 2 O 1
hydrogen : H 2
2 hydrogen
2 water
1 helium
hydrogen : H 2
Sets and Dictionaries Nanotech Example
![Page 52: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/52.jpg)
# inventory-03.txt
He 1
H 4
O 3
# formulas-03.txt
helium : He 1
water : H 2 O 1
hydrogen : H 2
2 hydrogen
2 water
1 helium
hydrogen : H 2
Sets and Dictionaries Nanotech Example
Looks good...
![Page 53: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/53.jpg)
# inventory-03.txt
He 1
H 4
O 3
# formulas-03.txt
helium : He 1
water : H 2 O 1
hydrogen : H 2
2 hydrogen
2 water
1 helium
hydrogen : H 2
Sets and Dictionaries Nanotech Example
Looks good...
Code is much simpler than it would be using
lists of pairs
![Page 54: Nanotech Example - v4.software-carpentry.org · Sets and Dictionaries Nanotech Example. How many molecules of different kinds can we make using the atoms in our warehouse? # Molecular](https://reader036.fdocuments.net/reader036/viewer/2022081612/5f6d2cef7cc51d20cd055549/html5/thumbnails/54.jpg)
June 2010
created by
Greg Wilson
June 2010
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.