In [7]:
from pyXRD.pt_tables import pt_p
In [4]:
def WT_dic(Mdict):
"""Return the weight uma of a formula
input:
--------------------------------------
Mdict : dictionary with molecule stochio
"""
wt=0
for atom, num in Mdict.items():
wt+= pt_p(atom, 'At_w')*num
return wt
def find_NEle(Formula):
"""Find Next element
function to find the numbers after a braket
input:
string with formula
output:
next_element (str), stochio (int/float), the rest of the string
"""
atom0=Formula[0]
num0=''
for i, char in enumerate(Formula[1:]+'J'):
if char.islower():
atom0 += char
elif char.isnumeric() or char=='.':
num0 += char
elif char.isupper() or (char in '() '):
break
if num0=='':
num0='1'
return atom0, float(num0), Formula[i+1:]
def Formula2Dict(Formula):
"""conver a string with a formula to dictionary with stochiometry
input:
formula (string): formula of the molecule
output:
dictionary of the type stochiopmetry
----------------------------------------------------------------------
ex :
Formula_parser_s(H2O)
>> {'H':2, 'O':2}
"""
FDict ={}
while True:
if Formula == '':
break
atom, num, Formula = find_NEle(Formula)
if atom in FDict:
FDict[atom]+= num
else:
FDict[atom] = num
return FDict
def Dict2Formula(Fdict):
"""conver a dictionary to a formula
input:
dictionary of the type stochiopmetry
output:
string
----------------------------------------------------------------------
ex :
Formula_parser_s(H2O)
>> {'H':2, 'O':2}
"""
out=''
for atom, num in Fdict.items():
out += f'{atom}{num:g}'
return out
def Formula_parser(Formula):
"""conver a string with a formula to dictionary with stochiometry
with manage of braket
input:
formula (string): formula of the molecule
output:
dictionary of the type stochiopmetry
----------------------------------------------------------------------
ex :
Formula_parser_s(H2O)
>> {'H':2, 'O':2}
"""
Formula=Formula.replace('[','(').replace(']', ')')
while True:
start = Formula.rfind('(')
if start == -1:
break
end = Formula[start:].find(')')+start
Idict = Formula2Dict(Formula[start+1:end])
dummy, Inum, EndFormula = find_NEle('X'+Formula[end+1:])
Idict= {key:value* Inum for key, value in Idict.items()}
Formula = f'{Formula[:start]}{Dict2Formula(Idict)}{EndFormula}'
return Formula2Dict(Formula)
In [5]:
def Formula_parser_s(formula):
"""conver a string with a formula to dictionary with stochiometry
input:
formula (string): formula of the molecule
output:
dictionary of the type stochiopmetry
----------------------------------------------------------------------
ex :
Formula_parser_s(H2O)
>> {'H':2, 'O':2}
"""
FDict={}
atom0=formula[0]
num0=''
for char in formula[1:]+'J':
if char.islower():
atom0 += char
elif char.isnumeric() or char=='.':
num0 += char
elif char.isupper() :
if atom0 in FDict:
FDict[atom0]+=float(num0)
else:
FDict[atom0]=float(num0)
atom0 = char
num0=''
return FDict
def MW_dic(Mdict):
"""return molecular weight
"""
return sum([pt_p(atom, 'At_w')*num for atom, num in Mdict.items()])
def WT_dic(Mdict):
""" return molecular weight%
"""
tweigh = MW_dic(Mdict)
return {atom: pt_p(atom, 'At_w') * num /tweigh for atom, num in Mdict.items()}
Calculate the synthesis of 5g Cu22Fe8Ge4S32¶
starting from pure elements
In [6]:
finalD=Formula_parser_s('Cu22Fe8Ge4S32')
print(finalD)
finalwt=WT_dic(finalD)
print(finalwt)
final_ele_w = {atom:val*5 for atom, val in finalwt.items()}
print(final_ele_w)
{'Cu': 22.0, 'Fe': 8.0, 'Ge': 4.0, 'S': 32.0} {'Cu': 0.4422112650929395, 'Fe': 0.14131660156917225, 'Ge': 0.09190829920301435, 'S': 0.3245638341348739} {'Cu': 2.2110563254646975, 'Fe': 0.7065830078458613, 'Ge': 0.45954149601507177, 'S': 1.6228191706743695}
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [91]:
In [1]:
def hanoi(n, source, target, auxiliary):
"""
Solves the Tower of Hanoi problem using recursion.
Parameters:
- n: Number of disks.
- source: Source peg.
- target: Target peg.
- auxiliary: Auxiliary peg.
"""
if n > 0:
# Move n-1 disks from source to auxiliary peg
hanoi(n - 1, source, auxiliary, target)
# Move the nth disk from source to target peg
print(f"Move disk {n} from {source} to {target}")
# Move the n-1 disks from auxiliary peg to target peg
hanoi(n - 1, auxiliary, target, source)
# Example: Solve Tower of Hanoi for 3 disks
hanoi(3, 'A', 'C', 'B')
Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to C
In [ ]:
In [ ]: