PROG0044 - Yearbook

no tags 

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 Money 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.

Jessica Lee
Yearbook photograph of Jessica Lee with quote (2014).

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.

Paris Gray
Yearbook photograph of Paris Gray with quote (2014).

Once the school board had decoded her message (of course without making the link with Juvenile's hit single Back That Azz Up), 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.

Assignment

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 as its argument. 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 that maps all chemical symbols in the given file onto the corresponding name of the element.
  • Write a function symbols that takes a string 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 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 the list of symbols.
  • Write a function encode that takes two arguments: i) a string that contains a sequence of chemical symbols (without separation characters) and ii) a dictionary that maps chemical symbols onto the corresponding names of the elements. 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 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 containing a sequence of names of chemical elements that are separated by a single space and ii) a dictionary that maps chemical symbols onto the corresponding names of the elements. 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 contains 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.

Example

The following interactive session assumes that the text file chemical_elements.txt is located in the current directory.

>>> table = mendeleev('chemical_elements.txt')
>>> 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

Epilogue

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.

IKBeNHeTbEu

Epilogue

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

Toen de Amerikaanse studente Jessica Lee in 2014 haar diploma secundair onderwijs behaalde, verliet ze de school met meer street credibility dan gelijk welke andere student die al eens een chemiegrapje heeft uitgehaald op school. De reden daarvoor was de quote die ze liet optekenen bij haar foto in het jaarboek. Terwijl haar medestudenten vermoedelijk quotes gebruikten van Ferris Bueller, Ralph Waldo Emerson of Mahatma Ghandi, haalde zij het advies dat ze meegaf aan haar klasgenoten uit het nummer Get Money van Christopher George Lattore Wallace — bij het brede publiek beter bekend als The Notorious B.I.G.. Wie het periodiek systeem der elementen niet direct bij de hand heeft, vindt hieronder alvast de vertaling van die quote.

Jessica Lee
Jaarboekfoto van Jessica Lee met bijschrift (2014).

Daar waar niet bekend is of Lee eventueel gevolgen heeft moeten dragen van haar briljante grap, is de Amerikaanse studente Paris Gray voor een gelijkaardige grap in het oog van de storm terechtgekomen. Toen zij en haar klasgenoten zich klaarstoomden om de volwassenheid in te duiken — en de hoogtes en laagtes van het post-secundaire schoolleven — liet ze de volgende motivatie voor haar klasgenoten in het jaarboek optekenen.

Paris Gray
Jaarboekfoto van Paris Gray met bijschrift (2014).

Van zodra de schooldirectie haar boodschap had ontcijferd (zonder natuurlijk de link te leggen met de hitsingle Back That Azz Up van de band Juvenile), werd ze geschorst voor het bijwonen van de laatste lessen en werd het haar verboden om zich in te schrijven voor de hogere opleidingen die door de school werden aangeboden. Toen de school haar ook ontzegde om de diploma-uitreiking te mogen bijwonen, was het hek helemaal van de dam. Pas nadat Gray en haar moeder hadden samengezeten met de schooldirectie werd besloten dat ze toch mocht deelnemen aan de ceremonie, en zelfs de speech mocht geven die toekwam aan de beste studente van de school.

Opgave

In het periodiek systeem der elementen krijgt elk scheikundig element een naam en een symbool toegekend. Beide worden gevormd door een hoofdletter, gevolgd door nul of meer kleine letters. Het symbool bestaat maximaal uit drie letters. Alle symbolen en namen zijn uniek. Gevraagd wordt:

  • Schrijf een functie mendelejev waaraan de locatie van een tekstbestand moet doorgegeven worden. Elke regel van dit bestand moet het symbool van een scheikundig element bevatten, gevolgd door een tab ('\t') en de naam van het element. De functie moet een dictionary teruggeven die alle chemische symbolen uit het bestand afbeeldt op de corresponderende naam van het element.
  • Schrijf een functie symbolen waaraan een string moet doorgegeven worden die enkel bestaat uit hoofdletters en kleine letters, en waarvan de eerste letter een hoofdletter is. De functie moet een lijst teruggeven met alle opeenvolgende deelstrings van de gegeven string die bestaan uit een hoofdletter gevolgd door alle kleine letters die er onmiddellijk op volgen. Deze functie kan dus gebruikt worden om een string die bestaat uit een opeenvolging van chemische symbolen op te splitsen in een lijst van symbolen.
  • Schrijf een functie codeer waaraan twee argumenten moeten doorgegeven worden: i) een string die bestaat uit een opeenvolging van chemische symbolen, en ii) een dictionary die chemische symbolen afbeeldt op de namen van de elementen. De functie moet elk chemisch symbool uit de gegeven string omzetten naar de corresponderende naam van het element (op basis van de afbeelding die bepaald wordt door de gegeven dictionary), en een string teruggeven die de opeenvolgende namen bevat die van elkaar gescheiden worden door één enkele spatie. Indien de gegeven string een chemisch symbool bevat dat niet als sleutel voorkomt in de gegeven dictionary, dan moet de functie een AssertionError opwerpen met de boodschap onbekend symbool.
  • Schrijf een functie decodeer waaraan twee argumenten moeten doorgegeven worden: i) een string die bestaat uit een aantal namen van chemische elementen die telkens van elkaar gescheiden worden door één enkele spatie, en ii) een dictionary die chemische symbolen afbeeldt op de namen van de elementen. De functie moet elke naam in de gegeven string omzetten naar het corresponderende symbool (conform de afbeelding die bepaald wordt door de gegeven dictionary), en een string teruggeven die bestaat uit de opeenvolgende chemische symbolen (zonder scheidingsteken). Indien de gegeven string de naam van een chemisch element bevat dat niet kan teruggevonden worden in de gegeven dictionary, dan moet de functie een AssertionError opwerpen met de boodschap onbekende naam.

Voorbeeld

Bij onderstaande voorbeeldsessie gaan we ervan uit dat het tekstbestand chemische_elementen.txt zich in de huidige directory bevindt.

>>> tabel = mendelejev('chemische_elementen.txt')
>>> tabel['I']
'Jood'
>>> tabel['K']
'Kalium'
>>> tabel['Be']
'Beryllium'

>>> symbolen('IKBeNHeTbEu')
['I', 'K', 'Be', 'N', 'He', 'Tb', 'Eu']
>>> symbolen('FIReISFUN')
['F', 'I', 'Re', 'I', 'S', 'F', 'U', 'N']
>>> symbolen('IPLaYWIThDyNAmITe')
['I', 'P', 'La', 'Y', 'W', 'I', 'Th', 'Dy', 'N', 'Am', 'I', 'Te']

>>> codeer('IKBeNHeTbEu', tabel)
'Jood Kalium Beryllium Stikstof Helium Terbium Europium'
>>> codeer('FIReISFUN', tabel)
'Fluor Jood Renium Jood Zwavel Fluor Uraan Stikstof'
>>> codeer('IPLaYWIThDyNAmITe', tabel)
'Jood Fosfor Lanthanium Yttrium Wolfraam Jood Thorium Dysprosium Stikstof Americium Jood Telluur'
>>> codeer('HeLiUm', tabel)
Traceback (most recent call last):
AssertionError: onbekend symbool

>>> decodeer('Jood Kalium Beryllium Stikstof Helium Terbium Europium', tabel)
'IKBeNHeTbEu'
>>> decodeer('Fluor Jood Renium Jood Zwavel Fluor Uraan Stikstof', tabel)
'FIReISFUN'
>>> decodeer('Jood Fosfor Lanthanium Yttrium Wolfraam Jood Thorium Dysprosium Stikstof Americium Jood Telluur', tabel)
'IPLaYWIThDyNAmITe'
>>> decodeer('Mithril Zuurstof Stikstof Titaan Fosfor Yttrium Thorium Zuurstof Stikstof', tabel)
Traceback (most recent call last):
AssertionError: onbekende naam

Epiloog

Studenten van de UGent mogen op hun beide oren slapen. De universiteit nam de volgende tweet van één van haar studenten zelfs over in haar communitymagazine Durf Denken.

IKBeNHeTbEu

Epiloog

Wist je dat er 12 chemische elementen zijn waarvan de (Engelse) naam kan samengesteld worden uit de symbolische voorstelling van chemische elementen?

ArSeNiC
AsTaTiNe
BiSmUTh
CArBON
CoPPEr
IrON
KrYPtON
NeON
PHOsPHORuS
SiLiCoN
TiN
XeNoN


Added by:Peter Dawyndt
Date:2011-07-24
Time limit:10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None