Spelling Words With The Elements

6th June 2020

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.

  1. Barium = Ba
  2. Carbon = C
  3. Potassium = K
  4. Tungsten = W
  5. Argon = Ar
  6. 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.

  1. $elements = [
  2. 'H' => 'Hydrogen',
  3. 'He' => 'Helium',
  4. 'Li' => 'Lithium',
  5. 'Be' => 'Beryllium',
  6. 'B' => 'Boron',
  7. 'C' => 'Carbon',
  8. // and so on...
  9. ];

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.

  1. /**
  2.  * Given a word, match elements in order to spell it.
  3.  *
  4.  * @param string $word
  5.  * The word to match.
  6.  * @param array $elements
  7.  * The list of elements to match against.
  8.  *
  9.  * @return array
  10.  * An array of matched elements that spell the word.
  11.  */
  12. function spellWordWithElements($word, $elements) {
  13. $solution = [];
  14.  
  15. $wordArray = str_split($word);
  16.  
  17. for ($i = 0; $i < count($wordArray); $i++) {
  18. $found = FALSE;
  19. if (!$found && isset($wordArray[$i + 1])) {
  20. // Try an match two caracters.
  21. $dual = $wordArray[$i] . $wordArray[$i + 1];
  22. foreach ($elements as $element => $name) {
  23. if ($dual == strtolower($element)) {
  24. // Match found.
  25. $solution[] = $element;
  26. $found = TRUE;
  27. $i++;
  28. }
  29. }
  30. }
  31.  
  32. if (!$found) {
  33. // If no match has been found then try for single
  34. // character matches.
  35. foreach ($elements as $element => $name) {
  36. if ($wordArray[$i] == strtolower($element)) {
  37. $solution[] = $element;
  38. $found = TRUE;
  39. }
  40. }
  41. }
  42. }
  43.  
  44. // If the solution matches the original word then we found
  45. // a correct match.
  46. if ($word == strtolower(implode($solution))) {
  47. return $solution;
  48. }
  49.  
  50. return [];
  51. }

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.

  1. Array
  2. (
  3. [0] => Ba
  4. [1] => C
  5. [2] => K
  6. [3] => W
  7. [4] => Ar
  8. [5] => Ds
  9. )

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

  1. $elementWord = spellWordWithElements('backwards', $elements);
  2.  
  3. foreach ($elementWord as $element) {
  4. $symbolString .= $element;
  5. $elementString .= $elements[$element] . ' ';
  6. }
  7. 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.

  1. accurate = AcCuRaTe = Actinium Copper Radium Tellurium
  2. attention = AtTeNTiON = Astatine Tellurium Nitrogen Titanium Oxygen Nitrogen
  3. championship = CHAmPIONSHIP = Carbon Hydrogen Americium Phosphorus Iodine Oxygen Nitrogen Sulfur Hydrogen Iodine Phosphorus
  4. click = ClICK = Chlorine Iodine Carbon Potassium
  5. connection = CoNNeCTiON = Cobalt Nitrogen Neon Carbon Titanium Oxygen Nitrogen
  6. essential = EsSeNTiAl = Einsteinium Selenium Nitrogen Titanium Aluminium
  7. generation = GeNeRaTiON = Germanium Neon Radium Titanium Oxygen Nitrogen
  8. newspaper = NeWSPaPEr = Neon Tungsten Sulfur Protactinium Phosphorus Erbium
  9. profession = PrOFeSSiON = Praseodymium Oxygen Iron Sulfur Silicon Oxygen Nitrogen
  10. research = ReSeArCH = Rhenium Selenium Argon Carbon Hydrogen
  11. satisfaction = SAtISFAcTiON = Sulfur Astatine Iodine Sulfur Fluorine Actinium Titanium Oxygen Nitrogen
  12. teaspoon = TeAsPoON = Tellurium Arsenic Polonium Oxygen Nitrogen
  13. virus = VIrUS = Vanadium Iridium Uranium Sulfur
  14. 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.