#!/usr/local/bin/ruby A=[ ["F",-1],["Cl",-1],["Br",-1],["I",-1],["At",-1], ["Li",1],["Na",1],["K",1],["Rb",1],["Cs",1],["Fr",1], ["Be",2],["Mg",2],["Ca",2],["Sr",2],["Ba",2],["Ra",2], ["O",-2], ["Al",3], ["Zn",2],["Cd",2], ["Sc",3],["Y",3],["La",3],["Ac",3],["Lu",3],["Lr",3], ["Ag",1], ["B",3], ["H",1], ["Ga",3],["In",3], ["Bi",3],["Sb",3], ["Pb",2],["Sn",2], ["Tl",1], ["N",-3],["P",5],["As",3], ["S",-2],["Se",-2],["Te",-2],["Po",-2], ["Si",4],["Ge",4], ] D=[ ["Ti",4],["Zr",4],["Hf",4], ["V",5],["Nb",5],["Ta",5], ["Cr",6],["Mo",6],["W",6], ["Mn",7],["Tc",7],["Re",7], ["Fe",8],["Ru",8],["Os",8], ["Co",9],["Rh",9],["Ir",9], ["Ni",10],["Pd",10],["Pt",10], ["Cu",11],["Ag",11],["Au",11], ] def vlnc(fml) fa=[] while fml.size>0 break unless m=/([A-Z][a-z]?)([\d\.]*)/.match(fml) an=1; an=m[2].to_f unless m[2]=="" fa.push([m[1],an]) fml=m.post_match end fb=[] for e in A break if fa.size==1 tmp=fa.assoc(e[0]) fb.push(tmp+[e[1],tmp[1]*e[1]]) if tmp fa.delete(tmp) end v=0 for e in fb v-=e[3] end fb.push(fa[0]+[v/fa[0][1],v]) fb.push(["error","","there are more than one unknown valence atom"]) if fa.size>1 return fb end while true fml=STDIN.readline.chomp tmp=vlnc(fml) for e in tmp str="#{e[2]}" dtmp=D.assoc(e[0]) str+=" (d^#{dtmp[1]-e[2]})" if dtmp print "#{e[0]}: #{str}\n" end end