This is how my brain works.
This morning, on my way to work, I remembered a t-shirt from a TV show we’re currently binging on with the word “sarcasm” written using element symbols from the periodic table:
I then started to wondered about how I would go about finding all the words that would be written using element symbols. So when I got into work, I quickly hacked this together. Out of a starting word set of 58109 words, I find 7417 “periodic” words. The longest word, and one of several with the most component elements (13) is:
Internationalisation, elements=In,Te,Rn,At,I,O,Na,Li,S,At,I,O,N
Other amusing ones are:
Inarticulateness, elements=In,Ar,Ti,Cu,La,Te,Ne,S,S
Supersaturation, elements=S,U,P,Er,S,At,U,Ra,Ti,O,N
Consciousnesses, elements=CO,N,Sc,I,O,U,Sn,Es,Se,S
Procrastination, elements=Pr,O,Cr,As,Ti,Na,Ti,O,N
Hyperinflation, elements=H,Y,P,Er,In,F,La,Ti,O,N
Falsification, elements=F,Al,Si,F,I,Ca,Ti,O,N,S
Perniciousness, elements=P,Er,Ni,C,I,O,U,Sn,Es,S
Perambulations, elements=P,Er,Am,B,U,La,Ti,O,N,S
Pontifications, elements=Po,N,Ti,F,I,Ca,Ti,O,N,S
Hallucinations, elements=H,Al,Lu,C,In,At,I,O,N,S
Voluptuousness, elements=V,O,Lu,Pt,U,O,U,Sn,Es,S
Sensationalism, elements=Se,N,S,At,I,O,Na,Li,Sm
And many, many more.
I then added a little tweak to filter out words that use repetitions of elements, and that brings down the number of “periodic” words to 5483. In that case, the longest word, and one of several with the most component elements (10) is:
Reclassification, elements=Re,Cl,As,Si,F,I,Ca,Ti,O,N
package wordlist; import java.io.*; import java.util.*; public class PeriodicWordFinder { private TreeSet<String> words; private TreeSet<String> elements; private ArrayList<PeriodicWord> periodicWords; public PeriodicWordFinder() { init(); } private void init(){ try{ words = read("wordlist.txt"); elements = new TreeSet<>(new AlphaLengthComparaator()); elements.addAll(read("elements.txt")); periodicWords = new ArrayList<>(); System.out.println("words.size() = " + words.size()); System.out.println("elements.size() = " + elements.size()); } catch (Exception e) { e.printStackTrace(); } } private TreeSet<String> read(String fileName) throws Exception{ TreeSet<String> retval = new TreeSet<>(); InputStream stream = getClass().getClassLoader().getResourceAsStream(fileName); BufferedReader in = new BufferedReader(new InputStreamReader(stream)); String line; while ((line = in.readLine()) != null){ line = line.toUpperCase().trim(); //no elements contain Q or J if (line.contains("Q") || line.contains("J")){ continue; } retval.add(line); } in.close(); return retval; } private void findWords(boolean onlyUniqueElements) { for(String word : words){ PeriodicWord pWord = new PeriodicWord(word); if ( periodicWord(pWord) ){ //if the word only contains unique instances of elements, or we don't care if (checkUniqueElements(pWord) || !onlyUniqueElements) { System.out.println("found: " + pWord); periodicWords.add(pWord); } } } } private boolean periodicWord(PeriodicWord word) { if (word.getWord().length() == 0) return true; for (String element : elements){ if (word.getWord().startsWith(element)){ word.getElements().add(element); word.setWord(word.getWord().substring(element.length())); return periodicWord(word); } } return false; } private boolean checkUniqueElements(PeriodicWord pWord) { //if elements are duplicated, size of hashset will be different than size of original list return (pWord.getElements().size() == new HashSet<>(pWord.getElements()).size()); } public static void main(String[] args){ PeriodicWordFinder ew = new PeriodicWordFinder(); ew.findWords(true); } private class AlphaLengthComparaator implements Comparator<String>{ @Override public int compare(String o1, String o2) { Integer l1 = o1.length(); Integer l2 = o2.length(); int lengthComparison = l1.compareTo(l2); if (lengthComparison == 0){ return o1.compareTo(o2); } else { return l1.compareTo(l2)*-1; } } } private class PeriodicWord { private String word; //will get mangled during recursion private String initialWord; //keep track of initial word private ArrayList<String> elements; //will get updated during recursion public PeriodicWord(String word) { this.initialWord = word; this.word = word; } public void setWord(String word) { this.word = word; } public String getWord() { return word; } public ArrayList<String> getElements() { if (elements == null) elements = new ArrayList<>(); return elements; } @Override public String toString() { return initialWord +", elements=" + String.join(",", elements); } } }