Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

class atom:

def __init__(self,x):
self.num=x
self.bonds=[]

def addBond(self,strength,x):
self.bonds=self.bonds+[[strength,x]]
return self.bonds

def printStructure(self,space):
print space,"atomic number:",self.num
for i in self.bonds:
print space,"bond strength:",i[0]
i[1].printStructure(space*2)
print
return True

def getStructure(self):
# first account for all the atomic species
accum = [[ self.num, 1 ]]
for i in self.bonds:
accum = accum + i[1].getStructure()

# next produce a "condensed" list


condensed = []
for i in accum:
found = False
for j in condensed:
# if we already have that species, just up the count
if j[0] == i[0]:
j[1] = j[1] + 1
found = True
# if we dont have that species, add it
if found == False:
condensed = condensed + [i]

# finally, sort that list


return sorted(condensed)

def genFormula(self):
x=self.getStructure()
accum=""
for i in x:
accum = accum + str(i[0]) + "_" + str(i[1]) + "_"

# get rid of last underscore


return accum[0:len(accum)-1]

You might also like