Question

You have been given a list of binary compounds. Build a function that # parses their...

You have been given a list of binary compounds. Build a function that # parses their chemical formula. They will be of the form AxBy. # Make a list with dictionaries for all formula in the "parse_me" list. # A & B are element symbols. # x & y are integers from 0 to 9. # # Your function should return a dictionary with the elements as keys, # and their fractional amount as values. A list of element symbols is # provided if needed. # Al2O3 => {"Al": 0.4, "O": 0.6} # # NOTE: I am not looking for a "clean" solution. Be as hacky as you need # to create a function that parses Binaries of the form AxBy. Go wild! # ============================================================================= all_symbols = ['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', 'Nh', 'Fl', 'Mc', 'Lv', 'Ts', 'Og'] parse_me = ['Dy1Rh2', 'Ba1Pt1', 'Al1Pt1', 'Ca1Si2', 'Ru3Zr1', 'Dy2Se3', 'Ir2Lu1', 'Be2C1', 'Al2Ho3', 'Al2Y1', 'I1Rb1', 'C1Zr1', 'B4Fe1', 'Ba1Pt2', 'Ca1In2', 'Cu1Ti3', 'C1Nb1', 'Br1Cu1', 'S1Zn1', 'Ni1Zn1', 'As1Ti3', 'O3Tb2', 'Al3Zr4', 'O1Sr1', 'Ca1Ir2', 'Mg1Sc1', 'Sn2Tb1', 'Au1Ti1', 'B1N1', 'Ho1Te2', 'Rh5Tb1', 'As1Pu1']

This is in python.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. PLEASE MAKE SURE INDENTATION IS EXACTLY AS SHOWN IN IMAGE.
Please do rate the answer if it helped. Thank you.

import re

def process(formulas):
   result = []
   for f in formulas:
       elems = re.split('\d', f) #get the alphabetic names separated by digits
       values = re.findall('\d', f) #get all the digits list
       values = [int(x) for x in values] #convert all values to int
       total = sum(values)
      
       d = {}
       for i in range(2):
           name = elems[i]
           frac = round(values[i]/total, 2) #round to 2 decimal places
           d[name] = frac
       result.append(d)
   return result
      
all_symbols = ['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', 'Nh', 'Fl', 'Mc', 'Lv', 'Ts', 'Og']

parse_me = ['Dy1Rh2', 'Ba1Pt1', 'Al1Pt1', 'Ca1Si2', 'Ru3Zr1', 'Dy2Se3', 'Ir2Lu1', 'Be2C1', 'Al2Ho3', 'Al2Y1', 'I1Rb1', 'C1Zr1', 'B4Fe1', 'Ba1Pt2', 'Ca1In2', 'Cu1Ti3', 'C1Nb1', 'Br1Cu1', 'S1Zn1', 'Ni1Zn1', 'As1Ti3', 'O3Tb2', 'Al3Zr4', 'O1Sr1', 'Ca1Ir2', 'Mg1Sc1', 'Sn2Tb1', 'Au1Ti1', 'B1N1', 'Ho1Te2', 'Rh5Tb1', 'As1Pu1']

print(process(parse_me))


output
=====


[{'Dy': 0.33, 'Rh': 0.67}, {'Ba': 0.5, 'Pt': 0.5}, {'Al': 0.5, 'Pt': 0.5}, {'Ca': 0.33, 'Si': 0.67}, {'Ru': 0.75, 'Zr': 0.25}, {'Dy': 0.4, 'Se': 0.6}, {'Ir': 0.67, 'Lu': 0.33}, {'Be': 0.67, 'C': 0.33}, {'Al': 0.4, 'Ho': 0.6}, {'Al': 0.67, 'Y': 0.33}, {'I': 0.5, 'Rb': 0.5}, {'C': 0.5, 'Zr': 0.5}, {'B': 0.8, 'Fe': 0.2}, {'Ba': 0.33, 'Pt': 0.67}, {'Ca': 0.33, 'In': 0.67}, {'Cu': 0.25, 'Ti': 0.75}, {'C': 0.5, 'Nb': 0.5}, {'Br': 0.5, 'Cu': 0.5}, {'S': 0.5, 'Zn': 0.5}, {'Ni': 0.5, 'Zn': 0.5}, {'As': 0.25, 'Ti': 0.75}, {'O': 0.6, 'Tb': 0.4}, {'Al': 0.43, 'Zr': 0.57}, {'O': 0.5, 'Sr': 0.5}, {'Ca': 0.33, 'Ir': 0.67}, {'Mg': 0.5, 'Sc': 0.5}, {'Sn': 0.67, 'Tb': 0.33}, {'Au': 0.5, 'Ti': 0.5}, {'B': 0.5, 'N': 0.5}, {'Ho': 0.33, 'Te': 0.67}, {'Rh': 0.83, 'Tb': 0.17}, {'As': 0.5, 'Pu': 0.5}]

Add a comment
Know the answer?
Add Answer to:
You have been given a list of binary compounds. Build a function that # parses their...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT