Created
March 17, 2014 09:36
-
-
Save asi1024/9596446 to your computer and use it in GitHub Desktop.
Calculate the molecular weight
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
s = input() | |
s += '#' | |
it = -1 | |
dict = {'H' : 1, | |
'C' : 12, | |
'N' : 14, | |
'O' : 16, | |
'Na': 23, | |
'P' : 31, | |
'S' : 32, | |
'Cl': 35.5} | |
def atom(): | |
global it | |
if s[it:it+2] in dict: | |
return dict[s[it:it+2]] | |
return dict[s[it]] | |
def number(): | |
global it | |
n = 0 | |
while s[it+1] >= '0' and s[it+1] <= '9': | |
n = n * 10 + int(s[it+1]) | |
it += 1 | |
if n == 0: | |
return 1 | |
return n | |
def molecule(): | |
global it | |
it += 1 | |
if s[it] == '#' or s[it] == ')': | |
return 0 | |
if s[it] == '(': | |
return molecule() * number() + molecule() | |
return atom() * number() + molecule() | |
print(molecule()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment