Last active
December 17, 2015 13:28
-
-
Save battis/5616942 to your computer and use it in GitHub Desktop.
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
{ | |
"metadata": { | |
"name": "Calculate Masses" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "In an attempt to simplivy your problem, let's focus on the nitty-gritty of it: the actual calculation. Let's save the user input for another moment (maybe we can convince Eddie and Jeremy to do it for you\u2026)" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "'''\n Assume you have the following equation and that the user has identified\n for which molecule they have given you the desired mass. Assume also that\n you have the mass of a particular reactant or product already entered by\n the user (ignore that problem, at least for now). For example, suppose\n you have a structure like masses, in which one mass has been specified.\n\n How would you go about implementing calculateMasses()?\n (HINT: figure out which mass you _have_ first!)\n\n I'll throw in a handy conversion function for you, in case you need it...\n'''\n\n'''\n Let's imagine the user interface for a second (inputs are prefaced by >>>)\n\n C3H8 + 5O2 \u2014> 4H2O + 3CO2\n (1) (2) (3) (4)\n\n What reactant or product do you have a mass for?\n >>> 3\n What is the mass of 4H2O?\n >>> 23.746kg\n\n This would generate a masses that looks like:\n\n masses = [\n [\n {'mass': -1,\n 'unit': '?'},\n {'mass': -1,\n 'unit': '?'}\n ],\n [\n {'mass': 23.746,\n 'unit': 'kg'},\n {'mass': -1,\n 'unit': '?'}\n ]\n ]\n\n '''\n\ndef convert(mass, oldUnit, newUnit):\n '''\n Convert between metric mass units (can be extended, btw...).\n For example convert(10, 'kg', 'g') would return 1000.0, while\n convert(10, 'g', 'kg') would return 0.01\n '''\n conversionTable = dict()\n conversionTable['t'] = 1000000.0\n conversionTable['kg'] = 1000.0\n conversionTable['g'] = 1.0\n conversionTable['mg'] = 0.001\n return mass * (conversionTable[oldUnit]/conversionTable[newUnit])\n\ndef calculateMasses(equation, masses):\n '''\n Use the (assume balanced) equation to calculate all of the masses,\n given one mass filled in for you by the user...\n '''\n # magic goes here\n return calculatedMasses\n\nmasses = [\n [\n {'mass': 23.2,\n 'unit': 'kg'},\n {'mass': -1,\n 'unit': '?'}\n ],\n [\n {'mass': -1,\n 'unit': '?'},\n {'mass': -1,\n 'unit': '?'}\n ]\n]\n\nknownMass = 0 # the first in the list of reactants and products\n \nequation = [\n [\n {'coefficient': 1,\n 'formula': {\n 'C': 3,\n 'H': 8\n }},\n {'coefficient': 5,\n 'formula': {\n 'O': 2\n }}\n ],\n [\n {'coefficient': 4,\n 'formula': {\n 'H': 2,\n 'O': 1\n }},\n {'coefficient': 3,\n 'formula': {\n 'C': 1,\n 'O': 2\n }}\n ]\n]", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "But\u2026 what if we modified the printEquation functions from the class' sample code to return strings (whose length we can calculate) rather than just printing them..." | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "def printEquation(equation):\n for side in equation:\n for molecule in side:\n # printOperand(molecule)\n operandText = generateOperandText(molecule)\n print(operandText, sep = '', end = '')\n print(' is',len(operandText), 'long')\n if molecule != side[-1]:\n print(' + ', sep = '', end = '')\n if side != equation[-1]:\n print(' --> ', sep = '', end = '')\n print()\n\ndef generateOperandText(operand):\n operandText = ''\n if operand['coefficient'] > 1:\n # operand['coefficient'], '(', sep = '', end = '')\n operandText = operandText + str(operand['coefficient']) + '('\n operandText = operandText + generateMoleculeText(operand['formula'])\n if operand['coefficient'] > 1:\n # print(')', sep = '', end = '')\n operandText = operandText + ')'\n return operandText\n\ndef generateMoleculeText(molecule):\n # FIXME why does this print in the wrong order?\n moleculeText = ''\n for symbol, subscript in molecule.items():\n if subscript > 1:\n # print(symbol, subscript, sep = '_', end = '')\n moleculeText = moleculeText + symbol + '_' + str(subscript)\n else:\n # print(symbol, sep = '', end = '')\n moleculeText = moleculeText + symbol\n return moleculeText\n\npropane = dict() # of coefficient and formula\npropane['coefficient'] = 1\npropane['formula'] = dict()\npropane['formula']['C'] = 3\npropane['formula']['H'] = 8\n\noxygen = dict()\noxygen['coefficient'] = 5\noxygen['formula'] = dict()\noxygen['formula']['O'] = 2\n\nreactants = [propane, oxygen]\n\nwater = dict()\nwater['coefficient'] = 4\nwater['formula'] = dict()\nwater['formula']['H'] = 2\nwater['formula']['O'] = 1\n\ncarbondioxide = dict()\ncarbondioxide['coefficient'] = 3\ncarbondioxide['formula'] = dict()\ncarbondioxide['formula']['C'] = 1\ncarbondioxide['formula']['O'] = 2\n\nproducts = [water, carbondioxide]\n\nequation = [reactants, products]\n\nprintEquation(equation)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment