Ok, so here is another coding question:
Given an input string, determine whether or not the string is composed of only abbreviations from the periodic table of elements.
This actually sounds kind of complex, like the stacking athlete question I answered previously, but it actually has a 2-line solution:
1 2 3 4 5 |
# $line holds the string we are computing $line =~ s/\W//g; # $elems is a string of all the elemental abbreviations # separated by '|' (e.g. "H|He|...") $result = ($line =~ m/^($allelems)+$/i); |
That’s right, given a string in $line, $result with be true if the string is composed solely of element name abbreviations. Simple huh? As for complexity, who knows. I don’t know enough of how regular expressions are computed to guess, but I assume it’s been made to be somewhat efficient.
Of course, additional optimizations can be made, for instance, “J” is not used in an elemental abbreviation, so any line with “J” could be disqualified (of course that requires a linear search of the data first).
Anyway, here’s the full script, in case you want to impress your friends.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
use strict; my @testline = ("The quick brown fox jumped over the lazy dog", "Sally sells sea shells down by the sea shore.", "There once was a man from nantucket, that is all.", "AuAgONaNAl"); # Sorting is not necessary, only done for readability. my @elems = ( "Ac", #Actinium "Ag", #Silver "Al", #Aluminum "Am", #Americium "Ar", #Argon "As", #Arsenic "At", #Astatine "Au", #Gold "B", #Boron "Ba", #Barium "Be", #Beryllium "Bh", #Bohrium "Bi", #Bismuth "Bk", #Berkelium "Br", #Bromine "C", #Carbon "Ca", #Calcium "Cd", #Cadmium "Ce", #Cerium "Cf", #Californium "Cl", #Chlorine "Cm", #Curium "Co", #Cobalt "Cn", #Copernicium "Cr", #Chromium "Cs", #Cesium "Cu", #Copper "Db", #Dubnium "Ds", #Darmstadtium "Dy", #Dysprosium "Er", #Erbium "Es", #Einsteinium "Eu", #Europium "F", #Fluorine "Fe", #Iron "Fm", #Fermium "Fr", #Francium "Ga", #Gallium "Gd", #Gadolinium "Ge", #Germanium "H", #Hydrogen "He", #Helium "Hf", #Hafnium "Hg", #Mercury "Ho", #Holmium "Hs", #Hassium "I", #Iodine "In", #Indium "Ir", #Iridium "K", #Potassium "Kr", #Krypton "La", #Lanthanum "Li", #Lithium "Lr", #Lawrencium "Lu", #Lutetium "Md", #Mendelevium "Mg", #Magnesium "Mn", #Manganese "Mo", #Molybdenum "Mt", #Meitnerium "N", #Nitrogen "Na", #Sodium "Nb", #Niobium "Nd", #Neodymium "Ne", #Neon "Ni", #Nickel "No", #Nobelium "Np", #Neptunium "O", #Oxygen "Os", #Osmium "P", #Phosphorus "Pa", #Protactinium "Pb", #Lead "Pd", #Palladium "Pm", #Promethium "Po", #Polonium "Pr", #Praseodymium "Pt", #Platinum "Pu", #Plutonium "Ra", #Radium "Rb", #Rubidium "Re", #Rhenium "Rf", #Rutherfordium "Rg", #Roentgenium "Rh", #Rhodium "Rn", #Radon "Ru", #Ruthenium "S", #Sulfur "Sb", #Antimony "Sc", #Scandium "Se", #Selenium "Sg", #Seaborgium "Si", #Silicon "Sm", #Samarium "Sn", #Tin "Sr", #Strontium "Ta", #Tantalum "Tb", #Terbium "Tc", #Technetium "Te", #Tellurium "Th", #Thorium "Ti", #Titanium "Tl", #Thallium "Tm", #Thulium "U", #Uranium "Uuh", #Ununhexium "Uun", #Ununnilium "Uuo", #Ununoctium "Uup", #Ununpentium "Uuq", #Ununquadium "Uus", #Ununseptium "Uut", #Ununtrium "Uuu", #Ununumium "V", #Vanadium "W", #Tungsten "Xe", #Xenon "Y", #Yttrium "Yb", #Ytterbium "Zn", #Zinc "Zr" #Zirconium }; my $allelems = join "|", @elems; print $allelems."\n\n"; foreach my $line (@testline) { $line =~ s/\W//g; print $line."n"; my $res = ($line =~ m/^($allelems)+$/i); if ($res) { print " - Yes\n"; } else { print " - No\n"; } } |