Name:Henrik H 1.0079 E nonexistent N 14.0067 R nonexistent I 126.9054 K 39.098 Your total atomic weight is 181.018The main loop of the program asks you for a molecular formula and checks its syntactical correctness. This all takes place in the terminal window.
Molecule: Si(C3(COOH)2)4(H2O)7 The formula is syntactically correct Molecule: Ag7(FyOj6)17 Unknown element Fy at line ending Oj6)17 Molecule: H1SO4 Too small number at line ending SO4 Molecule: Ca(OH)NO3 Missing number at line ending NO3 Molecule: Na(OH Missing right parenthesis at line ending Molecule: Nacl Missing capital letter at line ending clWhen the user hits return on an empty formula, the program finishes.
Ag 107.868You shall input the file into a hashtable
atomlistwith the atom name as the key and the atomic weight as the value, or as expressed in python:
atomlist["Ag"]="107.868"A few helpful hints for the personality chemistry application:
name.upper()to get uppercase letters.
atomlist.has_key()if the atom name exists.
float(vikt)converts them into numbers you can compute with.
<formel>::= <mol> \n <mol> ::= <group> | <group><mol> <group> ::= <atom> |<atom><num> | (<mol>) <num> <atom> ::= <LETTER> | <LETTER><letter> <LETTER>::= A | B | C | ... | Z <letter>::= a | b | c | ... | z <num> ::= 2 | 3 | 4 | ...Your program is to read the formula character by character and check the the syntas by recursive descent. It is simplest to read the complete line, put its characters in a queue and end with the newline character, like this:
rad=raw_input("Molecule:) for tkn in rad: q.put(tkn) q.put("\n")Recursive descent means that the main program calls
readgroup()and then possibly calls itself, that
readatom()or reads a parenthesis and calls
readmol()etc. -- all according to the syntax. When a violation of syntax is discovered, an exception is generated (
raise Exception,msg) which is caught in the main program, where the error message and what is left of the queue are printed.
You often have to peek at the next character to decide which branch
of the syntax tree to follow. Therefore your
should include a
peek()-method that returns the first
value but leaves the queue unchanged.
The molecular weight can be computed while checking the syntax of the formula, if you let all your methods return a number, like this:
readatomreturns an atomic weight from the hashtable
readnumreturns an integer
readgroupreturns the weight of a molecular group, for example Na or (OH)2
readmolreturns the weight of a submolecule
readformelreturns the total weight
Molecule:Si(C3(COOH)2)4(H2O)7 The formula is syntactically correct Molecular weight: 658.466
Write your own hash module
and use it instead of python's built-in dictionary.
Your main program has
from hash import Hash
and the hash module includes the following.
hash(key,n)is a function that takes
keyand creates an index less than
n. The function can reside outside the Hash class (but in the hash.py file).
class Hashconstructs a table of length n and fills it with None.
put(self,key,info)hashes a key to an index in the table, creates a node with key and info and links the index to that node. The node has a next-pointer for the case of more than one key hashing to the same index.
get(self,key)hashes key to a table index, searches the node list for the right key and returns info. If the key is not found, a
has_key, the main program must have
try: - - - except Exception: - - -