James Bond never really explains why he likes to drink his Martinis shaken, not stirred. So in 1999 the University of Western Ontario (Canada) biochemistry department decided to find out.

shaken, not stirred

They discovered that a shaken gin Martini has stronger antioxidant properties than a stirred one — which would help Bond avoid cardiovascular disease, stroke, and cataracts. In their write-up for the British Medical Journal, they conclude:

007's profound state of health may be due, at least in part, to compliant bartenders

Assignment

All chemical elements are indicated both by a name and a symbolic representation. Both start with an uppercase letter, followed by zero or more lowercase letters. Names of chemical elements have at least three letters, whereas some symbols only contain a single (uppercase) letter.

To get rid of the fact that there isn't always a direct connection between the name of a chemical element and its symbolic representation, we have decided to use a systematic way to assign a new symbol to each chemical element. To do so, we use the following procedure:

  1. traverse the names of all chemical elements in a particular order

  2. for each name, determine the corresponding symbol in the following way

    1. list all pairs of successive letters in the name of the element, in order of appearance in the name; each pair of letters should have the first letter converted to uppercase and the second to lowercase

    2. select the first pair of letters from this sequence that has not yet been assigned to an element

    3. assign the selected pair of letters as a symbol to the element

This is called the shaken-not-stirred procedure. Your task:

Example

In the following interactive session, we assume the text file elements.txt1 to be located in the current directory.

>>> pairs('Neon')
('Ne', 'Eo', 'On')
>>> pairs('Rutherfordium')
('Ru', 'Ut', 'Th', 'He', 'Er', 'Rf', 'Fo', 'Or', 'Rd', 'Di', 'Iu', 'Um')

>>> first(['Ne', 'Eo', 'On'], {'Ne', 'On'})
'Eo'
>>> first(('Ne', 'Eo', 'On'), {'Ne', 'On', 'Eo'})

>>> symbol = assign('elements.txt', impossible='???')
>>> symbol['Neon']
'Ne'
>>> symbol['Rubidium']
'Ru'
>>> symbol['Ruthenium']
'Ut'
>>> symbol['Rutherfordium']
'Rf'
>>> symbol['Tin']
'???'

Resources