Before Jessica Lee graduated high school in 2014, she went out with more street cred than anybody else who has ever made a chemistry joke in school with her yearbook quote. While her peers were probably quoting Ferris Bueller, Ralph Waldo Emerson or Mahatma Ghandi, the piece of advice she wanted to pass along to her classmates came from the song Get Money1 of Christopher George Latore Wallace — better known to the world as The Notorious B.I.G. For those of you who don't have a periodic table handy, this is the translation taken from Biggie's song.
While it hasn't been made public what, if any, repercussions Lee faced because of her brilliant joke, it appears that Paris Gray's similar-style joke caused more of an uproar with her school's administration. As she and her fellow classmates get ready to embark on adulthood and the ups and downs associated with post-high school life, she had this to say to keep her peers motivated.
Once the school board had decoded her message (of course without making the link with Juvenile's hit single Back That Azz Up2), she was given in-school suspension and prohibited from attending her school's senior walk. When she was also threatened with not being able to attend the graduation ceremony, that took things too far. Missing out on graduation would be more than just missing out on walking across the stage to receive her diploma, Gray was also slated to give the inspirational speech during the ceremony. Only after Gray and her mother sat down with the principal and the superintendent of the district, it was decided that she would be allowed to participate in the ceremony and could give her commencement address.
In the periodic table of the elements, each chemical element is assigned an official name and a symbolic representation. Both the name and the symbol start with an uppercase letter, followed by zero or more lowercase letters. Symbols have at most three letters. All names and symbols are unique. Your task:
Write function mendeleev that takes the location of a text file (str). Each line of the file must contain the symbol of a chemical element, followed by a tab ('\t') and the name of the element. The function must return a dictionary (dict) that maps each chemical symbol (str) in the given file onto the corresponding name of the element (str).
Write a function symbols that takes a string (str) that only contains uppercase and lowercase letter, with the first character being an uppercase letter. The function must return a list of the successive substrings (str) of the given string that start with an uppercase letter and are followed by all lowercase letters that immediately follow the uppercase letter. As a result, this function can be used to split a string that contains a sequence of chemical symbols (without separation characters) into a list of symbols.
Write a function encode that takes two arguments: i) a string (str) containing a sequence of chemical symbols (without separation characters) and ii) a dictionary (dict) that maps chemical symbols (str) onto the corresponding names of the elements (str). The function must convert each chemical symbol in the given string into the corresponding name of the element (based on the mapping defined by the given dictionary), and return the string (str) that contains the sequence of names separated by a single space. In case the given string contains a chemical symbol that does not occur as a key in the given dictionary, the function must raise an AssertionError with the message invalid symbol.
Write a function decode that takes two arguments: i) a string (str) containing a sequence of names of chemical elements that are separated by a single space and ii) a dictionary (dict) that maps chemical symbols (str) onto the corresponding names of the elements (str). The function must convert each name in the given string into the corresponding symbol (based on the mapping defined by the given dictionary) and return the string (str) containing the sequence of chemical symbols without separation characters. In case the given string contains the name of a chemical element that is not found in the given dictionary, the function must raise an AssertionError with the message invalid name.
The following interactive session assumes that the text file chemical_elements.txt3 to be located in the current directory.
>>> table = mendeleev('chemical_elements.txt4')
>>> table['I']
'Iodine'
>>> table['K']
'Potassium'
>>> table['Be']
'Beryllium'
>>> symbols('IKBeNHeTbEu')
['I', 'K', 'Be', 'N', 'He', 'Tb', 'Eu']
>>> symbols('FIReISFUN')
['F', 'I', 'Re', 'I', 'S', 'F', 'U', 'N']
>>> symbols('IPLaYWIThDyNAmITe')
['I', 'P', 'La', 'Y', 'W', 'I', 'Th', 'Dy', 'N', 'Am', 'I', 'Te']
>>> encode('IKBeNHeTbEu', table)
'Iodine Potassium Beryllium Nitrogen Helium Terbium Europium'
>>> encode('FIReISFUN', table)
'Fluorine Iodine Rhenium Iodine Sulfur Fluorine Uranium Nitrogen'
>>> encode('IPLaYWIThDyNAmITe', table)
'Iodine Phosphorus Lanthanum Yttrium Tungsten Iodine Thorium Dysprosium Nitrogen Americium Iodine Tellurium'
>>> encode('HeLiUm', table)
Traceback (most recent call last):
AssertionError: invalid symbol
>>> decode('Iodine Potassium Beryllium Nitrogen Helium Terbium Europium', table)
'IKBeNHeTbEu'
>>> decode('Fluorine Iodine Rhenium Iodine Sulfur Fluorine Uranium Nitrogen', table)
'FIReISFUN'
>>> decode('Iodine Phosphorus Lanthanum Yttrium Tungsten Iodine Thorium Dysprosium Nitrogen Americium Iodine Tellurium', table)
'IPLaYWIThDyNAmITe'
>>> decode('Mithril Oxygen Nitrogen Titanium Phosphorus Yttrium Thorium Oxygen Nitrogen', table)
Traceback (most recent call last):
AssertionError: invalid name
Ghent University students should not fear to be expelled, since the following tweet of one of its students was used in the university's community magazine Durf Denken. The phrase "ik ben het beu" means "I'm sick of it", referring to the feeling most students have during examination periods.
Did you know there are 12 chemical elements whose name can be spelled using element symbols?
ArSeNiC |
AsTaTiNe |
BiSmUTh |
CArBON |
CoPPEr |
IrON |
KrYPtON |
NeON |
PHOsPHORuS |
SiLiCoN |
TiN |
XeNoN |