Breaking Bad1 is an American television drama series that tells the story of Walter White, a struggling high school chemistry teacher who is diagnosed with inoperable lung cancer. He turns to a life of crime, producing and selling methamphetamine with the aim of securing his family's financial future before he dies.

Breaking Bad Logo
Logo of the American television series Breaking Bad.

If you've ever seen the series, you might have noticed how the opening and closing credits highlight some of the letters in the names of the cast with a green color. These letters refer to the chemical elements in the periodic table. The example below shows the letter S that corresponds to the chemical element sulfur and the letters Th that are used as the symbolic name of thorium.

Breaking Bad Credits
The closing credits of the television series Breaking Bad highlight the symbolic names of the chemical elements in the periodic table using a green color.

The logo of the series also frames the first two letters of each word in the title within a green rectangle. Br is the symbolic name used for bromide and Ba is the symbolic name of barium.

Assignment

  1. Write a function stressedWord that takes exactly two arguments. The first argument is a string that contains a single word. The second argument is a list of strings. This list may represent a list of symbolic names of chemical elements, but any other strings are also allowed. The function must return a string containing the original word, where some of the letters have been stressed by surrounding them with square brackets. These letters correspond to the "leftmost and longest" occurrence of a symbol from the given list of symbols. With "leftmost and longest" we mean that the position of the opening square bracket ([) should be as small as possible. If multiple symbols start from this position in the word, the longest one should be chosen. If none of the symbols from the given list of symbols occurs in the word, the original word without any square brackets must be returned by the function. Inside the string returned by the function there is at most one pair of square brackets. Comparisons between the given word and the symbols from the given list of symbols should be case insensitive, but the use of uppercase and lowercase letters in the word that was passed to the function should remain intact in the word that is returned by the function.

    stressedWord(word, symbols)

  2. Use the function stressedWord to write a function stressedSentence. The first string argument passed to this function represents a given sentence. The function must return a string containing the same sentence, in which each word highlights the "leftmost and longest" occurrence of the letters of a symbol in between square brackets. By default, the function highlights the symbolic names from the chemical elements (in the example below, this list of symbolic names is assigned to the variable chemicalSymbols). If a list of strings is passed as a second argument to the function, these strings represent the symbols that must be highlighted with square brackets. You may assume that all words in the sentence are separated with a single space.

    stressedSentence(sentence[, symbols])

Example

>>> chemicalSymbols = [
...     'Ac', 'Ag', 'Al', 'Am', 'Ar', 'As', 'At', 'Au', 'B', 'Ba', 'Be', 'Bh',
...     'Bi', 'Bk', 'Br', 'C', 'Ca', 'Cd', 'Ce', 'Cf', 'Cl', 'Cm', 'Cn', 'Co',
...     'Cr', 'Cs', 'Cu', 'Db', 'Ds', 'Dy', 'Er', 'Es', 'Eu', 'F', 'Fe', 'Fl',
...     'Fm', 'Fr', 'Ga', 'Gd', 'Ge', 'H', 'He', 'Hf', 'Hg', 'Ho', 'Hs', 'I',
...     'In', 'Ir', 'K', 'Kr', 'La', 'Li', 'Lr', 'Lu', 'Lv', 'Md', 'Mg', 'Mn',
...     'Mo', 'Mt', 'N', 'Na', 'Nb', 'Nd', 'Ne', 'Ni', 'No', 'Np', 'O', 'Os',
...     'P', 'Pa', 'Pb', 'Pd', 'Pm', 'Po', 'Pr', 'Pt', 'Pu', 'Ra', 'Rb', 'Re',
...     'Rf', 'Rg', 'Rh', 'Rn', 'Ru', 'S', 'Sb', 'Sc', 'Se', 'Sg', 'Si', 'Sm',
...     'Sn', 'Sr', 'Ta', 'Tb', 'Tc', 'Te', 'Th', 'Ti', 'Tl', 'Tm', 'U', 'Uuo',
...     'Uup', 'Uus', 'Uut', 'V', 'W', 'Xe', 'Y', 'Yb', 'Zn', 'Zr'
... ]

>>> stressedWord('Graham', chemicalSymbols)
'G[ra]ham'
>>> stressedWord('Python', ['Th', 'T', 'hon', 'on'])
'Py[th]on'

>>> stressedSentence("Monty Python's Life of Brian.")
"[Mo]nty [P]ython's [Li]fe [o]f [Br]ian."
>>> stressedSentence("Monty Python's Life of Brian.", ['Th', 'T', 'hon', 'on'])
"M[on]ty Py[th]on's Life of Brian."