You can order nine kinds of cocktails at the bar. Yet there seems to be only eight cocktails on the drinks menu. Can you find out the ninth kind of cocktail that is hidden in the drinks menu?
Answer: Shirley Temple Black1
To find the name of the hidden cocktail, first sort the cocktails on the drinks menu in ascending order by the number after the decimal comma in their price:
Sex on the beach (09)
Cuba libre (44)
Bramble (51)
Mary Pickford (56)
Tuxedo (68)
Cosmopolitan (69)
Suffering bastard (86)
Black Russian (88)
In the price of a cocktail, the number before the decimal comma then determines which characters should be selected from the name of the cocktail. This selection is based on a conversion of the (decimal) number into its binary representation. Reverse that binary representation and fill it with zeros at the end until it has the same length as the name of the cocktail. Then select the corresponding character from the name of the cocktail wherever there is a 1 in the reverse binary representation.
257 → 100000001 ⇄ 1000000010000000 → Sex on the beach → Sh
320 → 101000000 ⇄ 0000001010 → Cuba libre → ir
96 → 1100000 ⇄ 0000011 → Bramble → le
24 → 11000 ⇄ 0001100000000 → Mary Pickford → y␣
9 → 1001 ⇄ 100100 → Tuxedo → Te
168 → 10101000 ⇄ 000101010000 → Cosmopolitan → mpl
528 → 1000010000 ⇄ 00001000010000000 → Suffering bastard → e␣
31 → 11111 ⇄ 1111100000000 → Black Russian → Black
The name of the hidden cocktail is now obtained by concatenating all selected characters: Shirley Temple Black.
A drinks menu is a text file with UTF-82 character encoding that contains prices for a list of cocktails. This is an example drinks menu that corresponds to the image in the introduction (cocktails.txt3):
Black Russian...............€31,88 Bramble.....................€96,51 Cosmopolitan...............€168,69 Cuba libre.................€320,44 Mary Pickford...............€24,56 Sex on the beach...........€257,09 Suffering bastard..........€528,86 Tuxedo.......................€9,68
Each line of the text file contains the description of a cocktail. The description starts with the name of the cocktail, which never contains any dots (.). This is followed by one or more dots (.), a euro symbol (€) and the price of the cocktail. The price is always represented with two digits after a decimal comma (,). A drinks menu never contains cocktails that have the same two digits after the decimal comma in their price.
Your task is to extract the name of the hidden cocktail from a drinks menu. This is done in the following way:
Write a function read_cocktail that takes the description of a cocktail (str). The function must return a tuple with three elements from the description of the cocktail: i) the name (str), ii) the number before the decimal comma (int) in the price and iii) the number after the decimal comma (int) in the price.
Write a function read_menu that takes the location (str) of a drinks menu. The function must return a list containing for each cocktail on the drinks menu a tuple with three elements from the description of the cocktail: i) the name (str), ii) the number before the decimal comma (int) in the price and iii) the number after the decimal comma (int) in the price. These tuples must be listed in ascending order by the number after the decimal comma in the price.
Write a function mask that takes two arguments: i) the name of a cocktail and ii) the number before the decimal comma (int) in the price of the cocktail. The function must return a string (str) with the reverse binary representation of the number, filled with zeros at the end until it has the same length as the name of the cocktail.
Write a function select that takes two arguments: i) the name of a cocktail and ii) the number before the decimal comma (int) in the price of the cocktail. The function must return a string (str) containing the characters that are selected from the name of the cocktail to obtain the hidden cocktail in a drinks menu.
Write a function extract that takes the location (str) of a drinks menu. The function must return the name (str) of the hidden cocktail in the drinks menu.
In the following interactive session we assume that the text file cocktails.txt6 is located in the current directory.
>>> read_cocktail('Black Russian...............€31,88')
('Black Russian', 31, 88)
>>> read_cocktail('Cosmopolitan...............€168,69')
('Cosmopolitan', 168, 69)
>>> read_menu('cocktails.txt7')
[('Sex on the beach', 257, 9), ('Cuba libre', 320, 44), ('Bramble', 96, 51), ('Mary Pickford', 24, 56), ('Tuxedo', 9, 68), ('Cosmopolitan', 168, 69), ('Suffering bastard', 528, 86), ('Black Russian', 31, 88)]
>>> mask('Black Russian', 31)
'1111100000000'
>>> mask('Cosmopolitan', 168)
'000101010000'
>>> select('Black Russian', 31)
'Black'
>>> select('Cosmopolitan', 168)
'mpl'
>>> extract('cocktails.txt8')
'Shirley Temple Black'
Shirley Jane Temple9 (April 23, 1928 – February 10, 2014) was an American actress and diplomat. As a child with a diminutive stature, sparkling eyes, dimpled smile, and fifty-six blond curls, she became one of the most famous child stars in film history. Between 1932 and 1949 she appeared in more than fifty American movies and remains the youngest ever winner of an Academy Juvenile Award10. As an adult she adopted the name Shirley Temple Black and built a successful career as an ambassador.
Her name is further immortalized by the mocktail named after her: the Shirley Temple11. This non-alcoholic mixed drink is traditionally made with ginger ale and a splash of grenadine, garnished with a maraschino cherry12. It is often served to children dining with adults in lieu of real cocktails. If dark rum is added, it is called a Sherley Temple Black13 as an homage to her married surname.