Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
custom exceptions solution in Clear category for Periodic Table by FromSnakeToPython
orbital_levels=['*','s','p','d','f','g','h','i']
elements = ['*','H','He','Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al', 'Si',
'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu',
'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc',
'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La',
'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu',
'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At',
'Rn', 'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es',
'Fm', 'Md', 'No', 'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn', 'Uut',
'Fl', 'Uup', 'Lv', 'Uus', 'Uuo']
exceptions = {
'Cr':[['4s',1],['3d',5]],
'Cu':[['4s',1],['3d',10]],
'Nb':[['5s',1],['4d',4]],
'Mo':[['5s',1],['4d',5]],
'Ru':[['5s',1],['4d',7]],
'Rh':[['4d',8],['5s',1]],
'Pd':[['4d',10],['5s',0]],
'Ag':[['5s',1],['4d',10]],
'Pt':[['6s',1],['4f',14],['5d',9]],
'Au':[['6s',1],['4f', 14],['5d',10]],
'Lr':[['7s',2],['5f', 14],['6d',0],['7p',1]],
'La':[['6s',2],['4f',0],['5d',1]],
'Ce':[['6s',2],['4f',1],['5d',1]],
'Gd':[['6s',2],['4f',7],['5d',1]],
'Ac':[['7s',2],['5f',0],['6d',1]],
'Th':[['7s',2],['5f',0],['6d',2]],
'Pa':[['7s',2],['5f',2],['6d',1]],
'U':[['7s',2],['5f',3],['6d',1]],
'Np':[['7s',2],['5f',4],['6d',1]],
'Cm':[['7s',2],['5f',7],['6d',1]]
}
noble_gases = [(2,'He'),(10,'Ne'),(18,'Ar'),(36,'Kr'),(54,'Xe'),(86,'Rn')]
superscript = { 0:'\u2070', 1:'\u00b9', 2:'\u00b2', 3:'\u00b3', 4:'\u2074', 5:'\u2075', 6:'\u2076', 7:'\u2077', 8:'\u2078', 9:'\u2079'}
def get_noble_gas(electrons):
noble_range = [x for x in noble_gases if x[0] < electrons]
return max(noble_range)[1] if noble_range != [] else ''
def get_superscript(number_as_string):
retval = ''
for n in number_as_string:
retval+=superscript[int(n)]
return retval
def get_next_shell(orbit, shell):
if shell > 1:
shell -= 1
orbit += 1
else:
#go one step to the right
shell+=1
#if not possible go to the next orbit (we are at orbit one)
if shell > orbit:
orbit += 1
shell -= 1
#if possible, go step by step to the minimum orbit/spot that is not filled
else:
while shell <= orbit:
shell += 1
orbit -= 1
if shell > orbit:
shell -= 1
orbit += 1
break
max_electrons = shell * 4 - 2
return (orbit, shell, max_electrons)
def fill_orbits(electrons):
orbits=[]
#set first orbit
orbit=1
shell=1
max_electrons=2
while electrons > 0:
shell_electrons = 0
for j in range(min(max_electrons, electrons)):
shell_electrons += 1
electrons -= 1
orbits.append([str(orbit) + orbital_levels[shell], shell_electrons])
orbit, shell, max_electrons = get_next_shell(orbit, shell)
return orbits
def fill_shell(shell, electrons):
shell_no = orbital_levels.index(shell)
size = int((shell_no * 4 - 2) / 2)
shell_list=[0 for i in range(size)]
j = 0
while electrons > 0:
shell_list[j % size] += 1
electrons-=1
j+=1
return ''.join([str(s) for s in shell_list])
def checkio(element):
electrons = elements.index(element)
my_config = fill_orbits(electrons)
electron_config = ''
noble_gas = get_noble_gas(electrons)
if noble_gas != '':
noble_config = fill_orbits(elements.index(noble_gas))
noble_gas='[' + noble_gas + '] '
electron_config = ' '.join(fill_shell(x[0][1], x[1]) for x in noble_config) + ' '
for nc in noble_config:
my_config.remove(nc)
if element in exceptions:
my_config=exceptions[element]
noble_gas_config = [x[0] + get_superscript(str(x[1])) for x in my_config]
noble_gas_config.sort(key=lambda x:(x[0],orbital_levels.index(x[1])))
noble_gas_config = noble_gas + ' '.join(noble_gas_config)
electron_config += ' '.join(fill_shell(x[0][1], x[1]) for x in my_config)
return [str(electrons), noble_gas_config, electron_config]
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert (checkio('H') == ["1", u"1s¹", "1"]), "First Test - 1s¹"
assert (checkio('He') == ["2", u"1s²", "2"]), "Second Test - 1s²"
assert (checkio('Al') == ["13", u"[Ne] 3s² 3p¹", "2 2 222 2 100"]), "Third Test - 1s² 2s² 2p⁶ 3s² 3p¹"
assert (checkio('O') == ["8", u"[He] 2s² 2p⁴", "2 2 211"]), "Fourth Test - 1s² 2s² 2p⁴"
assert (checkio('Li') == ["3", u"[He] 2s¹", "2 1"]), "Fifth Test - 1s² 2s¹"
print('All done!')
Feb. 16, 2021
Comments: