Nanotech Example Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Interfaces Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter Three: STOICHIOMETRY.
Python for Informatics: Exploring Information
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 6 CHEMICAL BONDING. WHAT IS ELECTRONEGATIVITY? WHY DOES IT MATTER?
Counting Atoms Obviously, there are more materials in the world than just 100 or so elements on the periodic table.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
MML The Molecular Model Lab. INFORMATION The following information will be on the board the number of the model the model the name of the molecule the.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Sorting and Modules. Sorting Lists have a sort method >>> L1 = ["this", "is", "a", "list", "of", "words"] >>> print L1 ['this', 'is', 'a', 'list', 'of',
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Browsing Directories Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Pipes and Filters Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Built-in Data Structures in Python An Introduction.
Libraries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Tools Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
…and how to do stuff with them Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
COUNT ATOM How to count atoms. 2H 2 O COEFFICIENT SUBSCRIPT.
Operators Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Counting Stars Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Percent Composition.
Introduction to Programming
Program Design Invasion Percolation: Aliasing
The Molecular Model Lab
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
Program Design Invasion Percolation: Resolving Ties
Program Design Invasion Percolation: Neighbors
The Molecular Model Lab
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Photosynthesis Lab To further reinforce the overall process of photosynthesis and also the details of how it works.
Version Control Basic Operation Copyright © Software Carpentry 2010
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Version Control Introduction Copyright © Software Carpentry 2010
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Python for Informatics: Exploring Information
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Browsing Directories Using walk
Chemical Composition.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
GCSE Computing Mini Assignment.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

Nanotech Example Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Sets and Dictionaries

Nanotech Example How many molecules of different kinds can we make using the atoms in our warehouse?

Sets and DictionariesNanotech Example 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 DictionariesNanotech Example 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 DictionariesNanotech Example 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 DictionariesNanotech Example Natural to represent inventory as dictionary

Sets and DictionariesNanotech Example Natural to represent inventory as dictionary Keys: atomic symbols

Sets and DictionariesNanotech Example Natural to represent inventory as dictionary Keys: atomic symbols Values: number of atoms available

Sets and DictionariesNanotech Example Natural to represent inventory as dictionary Keys: atomic symbols Values: number of atoms available 'He' 'O' 'H' 1 3 4

Sets and DictionariesNanotech Example Represent individual molecules the same way

Sets and DictionariesNanotech Example Represent individual molecules the same way 'O' 'H' 1 2 water

Sets and DictionariesNanotech Example Store formulas as a dictionary of dictionaries

Sets and DictionariesNanotech Example Store formulas as a dictionary of dictionaries Keys: molecule names

Sets and DictionariesNanotech Example Store formulas as a dictionary of dictionaries Keys: molecule names Values: dictionaries of formulas

Sets and DictionariesNanotech Example Store formulas as a dictionary of dictionaries Keys: molecule names Values: dictionaries of formulas 'water' 'ammonia'

Sets and DictionariesNanotech Example Number of molecules that can be made is: min available[atom] required[atom] atom ∈ formula

Sets and DictionariesNanotech Example Number of molecules that can be made is: min available[atom] required[atom] atom ∈ formula If atom not in available, its count is implicitly 0

Sets and DictionariesNanotech Example Number of molecules that can be made is: min available[atom] required[atom] atom ∈ formula If atom not in available, its count is implicitly 0 Store results in yet another dictionary

Sets and DictionariesNanotech Example Number of molecules that can be made is: min available[atom] 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 DictionariesNanotech Example Number of molecules that can be made is: min available[atom] required[atom] atom ∈ formula If atom not in available, its count is implicitly 0 Store results in yet another dictionary Keys: molecule names Values: counts of how many can be made

Sets and DictionariesNanotech Example '''Calculate how many molecules of each type can be made with the atoms on hand.''' import sys if __name__ == '__main__': inventory = read_inventory(sys.argv[1]) formulas = read_formulas(sys.argv[2]) counts = calculate_counts(inventory, formulas) show_counts(counts)

Sets and DictionariesNanotech Example def read_inventory(filename): '''Read inventory of available atoms.''' result = {} for line in read_lines(filename): name, count = line.split(' ') result[name] = int(count) return result

Sets and DictionariesNanotech Example def read_lines(filename): '''Read lines from file, stripping out blank lines and comments.''' reader = open(filename, 'r') lines = [] for line in reader: line = line.split('#')[0].strip() if line: lines.append(line) reader.close() return lines

Sets and DictionariesNanotech Example def read_formulas(filename): '''Read molecular formulas from file.''' result = {} for line in read_lines(filename): name, atoms = line.split(':') name = name.strip() atoms = atoms.strip().split(' ') formula = {} for i in range(0, len(atoms), 2): formula[atoms[i]] = int(atoms[i+1]) result[name] = formula return result

Sets and DictionariesNanotech Example def read_formulas(filename): '''Read molecular formulas from file.''' result = {} for line in read_lines(filename): name, atoms = line.split(':') name = name.strip() atoms = atoms.strip().split(' ') formula = {} for i in range(0, len(atoms), 2): formula[atoms[i]] = int(atoms[i+1]) result[name] = formula return result Storing results in dictionary

Sets and DictionariesNanotech Example def read_formulas(filename): '''Read molecular formulas from file.''' result = {} for line in read_lines(filename): name, atoms = line.split(':') name = name.strip() atoms = atoms.strip().split(' ') formula = {} for i in range(0, len(atoms), 2): formula[atoms[i]] = int(atoms[i+1]) result[name] = formula return result For each interesting line in the input file...

Sets and DictionariesNanotech Example def read_formulas(filename): '''Read molecular formulas from file.''' result = {} for line in read_lines(filename): name, atoms = line.split(':') name = name.strip() atoms = atoms.strip().split(' ') formula = {} for i in range(0, len(atoms), 2): formula[atoms[i]] = int(atoms[i+1]) result[name] = formula return result Separate the molecule name and the formula

Sets and DictionariesNanotech Example def read_formulas(filename): '''Read molecular formulas from file.''' result = {} for line in read_lines(filename): name, atoms = line.split(':') name = name.strip() atoms = atoms.strip().split(' ') formula = {} for i in range(0, len(atoms), 2): formula[atoms[i]] = int(atoms[i+1]) result[name] = formula return result Separate the atoms and their counts

Sets and DictionariesNanotech Example def read_formulas(filename): '''Read molecular formulas from file.''' result = {} for line in read_lines(filename): name, atoms = line.split(':') name = name.strip() atoms = atoms.strip().split(' ') formula = {} for i in range(0, len(atoms), 2): formula[atoms[i]] = int(atoms[i+1]) result[name] = formula return result Loop over pairs of atoms and counts

Sets and DictionariesNanotech Example def read_formulas(filename): '''Read molecular formulas from file.''' result = {} for line in read_lines(filename): name, atoms = line.split(':') name = name.strip() atoms = atoms.strip().split(' ') formula = {} for i in range(0, len(atoms), 2): formula[atoms[i]] = int(atoms[i+1]) result[name] = formula return result Store the count as an integer with the atomic symbol as key

Sets and DictionariesNanotech Example def read_formulas(filename): '''Read molecular formulas from file.''' result = {} for line in read_lines(filename): name, atoms = line.split(':') name = name.strip() atoms = atoms.strip().split(' ') formula = {} for i in range(0, len(atoms), 2): formula[atoms[i]] = int(atoms[i+1]) result[name] = formula return result And store the molecule in the main dictionary

Sets and DictionariesNanotech Example def calculate_counts(inventory, formulas): '''Calculate how many of each molecule can be made with inventory.''' counts = {} for name in formulas: counts[name] = dict_divide(inventory, formulas[name]) return counts

Sets and DictionariesNanotech Example def calculate_counts(inventory, formulas): '''Calculate how many of each molecule can be made with inventory.''' counts = {} for name in formulas: counts[name] = dict_divide(inventory, formulas[name]) return counts Sub-dictionary holding atom counts for a particular molecule

Sets and DictionariesNanotech Example def calculate_counts(inventory, formulas): '''Calculate how many of each molecule can be made with inventory.''' counts = {} for name in formulas: counts[name] = dict_divide(inventory, formulas[name]) return counts Big functions: nothing is obviously wrong

Sets and DictionariesNanotech Example def calculate_counts(inventory, formulas): '''Calculate how many of each molecule can be made with inventory.''' counts = {} for name in formulas: counts[name] = dict_divide(inventory, formulas[name]) return counts Big functions: nothing is obviously wrong Small functions: obviously, nothing is wrong

Sets and DictionariesNanotech Example def dict_divide(inventory, molecule): '''Calculate how much of a single molecule can be made with inventory.''' 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

Sets and DictionariesNanotech Example def dict_divide(inventory, molecule): '''Calculate how much of a single molecule can be made with inventory.''' 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 Identical format: keys are atoms, values are counts

Sets and DictionariesNanotech Example def dict_divide(inventory, molecule): '''Calculate how much of a single molecule can be made with inventory.''' 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 Common pattern: None means "uninitialized", so initialize the first time through the loop

Sets and DictionariesNanotech Example def dict_divide(inventory, molecule): '''Calculate how much of a single molecule can be made with inventory.''' 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 Common pattern: stored value or default

Sets and DictionariesNanotech Example def dict_divide(inventory, molecule): '''Calculate how much of a single molecule can be made with inventory.''' 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 Common pattern: find minimum of calculated values

Sets and DictionariesNanotech Example def show_counts(counts): '''Show how many of each molecule can be made.''' counts = invert_dict(counts) for key in sorted(counts.keys(), reverse=True): for name in sorted(counts[key]): print key, name

Sets and DictionariesNanotech Example def show_counts(counts): '''Show how many of each molecule can be made.''' counts = invert_dict(counts) for key in sorted(counts.keys(), reverse=True): for name in sorted(counts[key]): print key, name Reverse to get greatest first

Sets and DictionariesNanotech Example def invert_dict(src): '''Invert a dictionary, returning value->set{key}.''' dst = {} for key in src: value = src[key] if value not in dst: dst[value] = set() dst[value].add(key) return dst

Sets and DictionariesNanotech Example def invert_dict(src): '''Invert a dictionary, returning value->set{key}.''' dst = {} for key in src: value = src[key] if value not in dst: dst[value] = set() dst[value].add(key) return dst Common pattern: make sure there's a collection, then add

Sets and DictionariesNanotech Example def show_counts(counts): '''Show how many of each molecule can be made.''' counts = invert_dict(counts) for key in sorted(counts.keys(), reverse=True): if key > 0: for name in sorted(counts[key]): print key, name Go back and only show molecules that can actually be made

Sets and DictionariesNanotech Example # inventory-00.txt# formulas-01.txt helium : He 1 No output, which is correct.

Sets and DictionariesNanotech Example 1 helium # inventory-01.txt He 1 # formulas-01.txt helium : He 1

Sets and DictionariesNanotech Example 1 helium # inventory-02.txt He 1 H 4 # formulas-01.txt helium : He 1

Sets and DictionariesNanotech Example 1 helium # inventory-02.txt He 1 H 4 # formulas-02.txt helium : He 1 water : H 2 O 1

Sets and DictionariesNanotech Example 2 hydrogen 1 helium # inventory-02.txt He 1 H 4 # formulas-03.txt helium : He 1 water : H 2 O 1 hydrogen : H 2

Sets and DictionariesNanotech Example 2 hydrogen 2 water 1 helium # inventory-03.txt He 1 H 4 O 3 # formulas-03.txt helium : He 1 water : H 2 O 1 hydrogen : H 2

Sets and DictionariesNanotech Example 2 hydrogen 2 water 1 helium # inventory-03.txt He 1 H 4 O 3 # formulas-03.txt helium : He 1 water : H 2 O 1 hydrogen : H 2 Looks good...

Sets and DictionariesNanotech Example 2 hydrogen 2 water 1 helium # inventory-03.txt He 1 H 4 O 3 # formulas-03.txt helium : He 1 water : H 2 O 1 hydrogen : H 2 Looks good... Code is much simpler than it would be using lists of pairs

June 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.