Spelling Words With The Elements

The other day I was tasked with creating the weekly quiz for my family. I decided it would be good to do a section that would consist of 10 words that were spelled using symbols of the chemical elements. The questions would be presented as a list of element names, like this.

Barium Carbon Potassium Tungsten Argon Darmstadtium

This breaks down to the element symbols in the following way.

Barium = Ba
Carbon = C
Potassium = K
Tungsten = W
Argon = Ar
Darmstadtium = Ds

Putting that all together we spell out a word.

BaCKWArDs

Backwards!

After a bit of thinking about what words I could use I decided to write a program that would help me. The questions in the quiz did not go down very well, but I thought I would put the code that I used here.

The first thing I did was to set up an array of elements that I could use to compare parts of words with.

$elements = [
  'H' => 'Hydrogen',
  'He' => 'Helium',
  'Li' => 'Lithium',
  'Be' => 'Beryllium',
  'B' => 'Boron',
  'C' => 'Carbon',
  // and so on...
];

The next step was to create a function that took in a word and the element array to figure out if that word could be spelt using the element symbols.

/**
 * Given a word, match elements in order to spell it.
 *
 * @param string $word
 *   The word to match.
 * @param array $elements
 *   The list of elements to match against.
 *
 * @return array
 *   An array of matched elements that spell the word.
 */
function spellWordWithElements($word, $elements) {
  $solution = [];

  $wordArray = str_split($word);

  for ($i = 0; $i < count($wordArray); $i++) {
    $found = FALSE;
    if (!$found && isset($wordArray[$i + 1])) {
      // Try an match two caracters.
      $dual = $wordArray[$i] . $wordArray[$i + 1];
      foreach ($elements as $element => $name) {
        if ($dual == strtolower($element)) {
          // Match found.
          $solution[] = $element;
          $found = TRUE;
          $i++;
        }
      }
    }

    if (!$found) {
      // If no match has been found then try for single
      // character matches.
      foreach ($elements as $element => $name) {
        if ($wordArray[$i] == strtolower($element)) {
          $solution[] = $element;
          $found = TRUE;
        }
      }
    }
  }

  // If the solution matches the original word then we found
  // a correct match.
  if ($word == strtolower(implode($solution))) {
    return $solution;
  }

  return [];
}

The function works by looping through the letters in the given word and attempts to match them with the elements array. As some elements have one letter and some have two letters the first thing needed was to compare the two letter elements, and if nothing is found then loop through the single letter elements. A final check is then done to ensure that the array found is the same as the original word, which might not always be the case.

This function returns an array of the elements found, in the order needed to spell out the word. For example, the word "backwards" would return the following array.

Array
(
    [0] => Ba
    [1] => C
    [2] => K
    [3] => W
    [4] => Ar
    [5] => Ds
)

In order to print out the data in a useful way we do the following.

$elementWord = spellWordWithElements('backwards', $elements);

foreach ($elementWord as $element) {
  $symbolString .= $element;
  $elementString .= $elements[$element] . ' ';
}
echo $word . ' = ' . $symbolString . ' = ' . $elementString;

This prints out.

backwards = BaCKWArDs = Barium Carbon Potassium Tungsten Argon Darmstadtium

Here are some more examples of this function in action.

accurate = AcCuRaTe = Actinium Copper Radium Tellurium
attention = AtTeNTiON = Astatine Tellurium Nitrogen Titanium Oxygen Nitrogen
championship = CHAmPIONSHIP = Carbon Hydrogen Americium Phosphorus Iodine Oxygen Nitrogen Sulfur Hydrogen Iodine Phosphorus
click = ClICK = Chlorine Iodine Carbon Potassium
connection = CoNNeCTiON = Cobalt Nitrogen Neon Carbon Titanium Oxygen Nitrogen
essential = EsSeNTiAl = Einsteinium Selenium Nitrogen Titanium Aluminium
generation = GeNeRaTiON = Germanium Neon Radium Titanium Oxygen Nitrogen
newspaper = NeWSPaPEr = Neon Tungsten Sulfur Protactinium Phosphorus Erbium
profession = PrOFeSSiON = Praseodymium Oxygen Iron Sulfur Silicon Oxygen Nitrogen
research = ReSeArCH = Rhenium Selenium Argon Carbon Hydrogen
satisfaction = SAtISFAcTiON = Sulfur Astatine Iodine Sulfur Fluorine Actinium Titanium Oxygen Nitrogen
teaspoon = TeAsPoON = Tellurium Arsenic Polonium Oxygen Nitrogen
virus = VIrUS = Vanadium Iridium Uranium Sulfur
wolves = WOLvEs = Tungsten Oxygen Livermorium Einsteinium

These words were found by feeding in a list of common words and selecting a few of the more interesting looking words.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
10 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.