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.
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
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:
traverse the names of all chemical elements in a particular order
for each name, determine the corresponding symbol in the following way
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
select the first pair of letters from this sequence that has not yet been assigned to an element
assign the selected pair of letters as a symbol to the element
This is called the shaken-not-stirred procedure. Your task:
Write a function pairs that takes the name of a chemical element. The function must return a tuple containing all pairs of successive letters in the given name, listed in order of appearance in the name. Each pair of letters should have the first letter converted to uppercase and the second to lowercase.
Write a function first that takes two arguments: a sequence of strings (a list or tuple) and a container of strings (a list, tuple or set). The function must return the first string from the given sequence (first argument) that is does not occur in the given container (second argument). If the given sequence does not contain a single element that does not occur in the given container, the function must return the value None.
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']
'???'
Trevithick CC, Chartrand MM, Wahlman J, Rahman F, Hirst M, Trevithick JR (1999). Shaken, not stirred: bioanalytical study of the antioxidant activities of martinis. BMJ 319(7225), 1600-1602. 2
Johnson G, Guha IN, Davies P (2013). Were James Bond's drinks shaken because of alcohol induced tremor? BMJ 347, f7255. 3