<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The Osmosian Order of Plain English Programmers Welcomes You]]></provider_name><provider_url><![CDATA[http://osmosianplainenglishprogramming.blog]]></provider_url><author_name><![CDATA[gerryrzeppa]]></author_name><author_url><![CDATA[https://osmosianplainenglishprogramming.blog/author/gerryrzeppa/]]></author_url><title><![CDATA[Anagrams]]></title><type><![CDATA[link]]></type><html><![CDATA[<header class="entry-header">
<h1 class="entry-title"></h1>
</header>
<div class="entry-content">
<p>An anagram is a word spelled by rearranging the letters of another word. The “other word” may or may not be a real word; if it is not, we say the letters have been <em>jumbled </em>or <em>scrambled</em>. Note that some “other words” yield more than one anagram. The letters TPSO, for example, can be rearranged to spell OPTS, POST, POTS, SPOT, STOP, and TOPS.</p>
<p>The obvious way to extract an anagram from a jumbled string of letters is to arrange the letters in all possible ways, checking each arrangement against a lexicon (ie, a list of valid words). This brute-force approach can quickly get out of hand, however. The number of ways that three unique letters can be arranged is only 3 times 2 times 1, or 6; but the number of ways that 9 unique letters can be arranged is 362,880. So we need a better way.</p>
<p>The not-so-obvious way to extract an anagram from a jumbled string is to first convert the lexicon into a <em>dictionary </em>where each entry in the dictionary is a sorted word, and the “definition” is the original, unscrambled word. These are the Plain English type definitions we need to describe that:</p>
<p><span style="color:#00ccff;">A dictionary is a thing with some entries.</span><br />
<span style="color:#00ccff;"> An entry is a thing with a sorted string and a string.</span></p>
<p>Our Plain English development system includes a lexicon of the 64,000 most commonly used English words in the form of a text file, one word per line, so we can use that to make our dictionary. These are the Plain English routines that do that:</p>
<p><span style="color:#00ccff;">To create a dictionary:</span><br />
<span style="color:#00ccff;"> Allocate memory for the dictionary.</span><br />
<span style="color:#00ccff;"> Put &#8220;z:\g4g articles\anagrams\lexicon.txt&#8221; into a path.</span><br />
<span style="color:#00ccff;"> Read the path into a buffer.</span><br />
<span style="color:#00ccff;"> Slap a rider on the buffer.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Get a string from the buffer given the rider.</span><br />
<span style="color:#00ccff;"> If the string is blank, break.</span><br />
<span style="color:#00ccff;"> Trim the string.</span><br />
<span style="color:#00ccff;"> Sort the string into a sorted string.</span><br />
<span style="color:#00ccff;"> Add an entry to the dictionary using the sorted string and the string.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p><span style="color:#00ccff;">To add an entry to a dictionary given a sorted string and a string:</span><br />
<span style="color:#00ccff;"> Allocate memory for the entry.</span><br />
<span style="color:#00ccff;"> Put the string into the entry&#8217;s string.</span><br />
<span style="color:#00ccff;"> Put the sorted string into the entry&#8217;s sorted string.</span><br />
<span style="color:#00ccff;"> Append the entry to the dictionary&#8217;s entries.</span></p>
<p>A random sample of entries from our “anagram dictionary” looks like this:</p>
<p><span style="color:#00ccff;">aeprrs parser</span><br />
<span style="color:#00ccff;"> aeprrss parsers</span><br />
<span style="color:#00ccff;"> aeprrss sparser</span><br />
<span style="color:#00ccff;"> aeprrssstu superstars</span><br />
<span style="color:#00ccff;"> aeprrsstu superstar</span><br />
<span style="color:#00ccff;"> aeprrssy sprayers</span><br />
<span style="color:#00ccff;"> aeprrstu raptures</span><br />
<span style="color:#00ccff;"> aeprrsy prayers</span><br />
<span style="color:#00ccff;"> aeprrsy sprayer</span><br />
<span style="color:#00ccff;"> aeprrtu rapture</span></p>
<p>Note that some of the sorted words (like <em>aeprrss</em>) have more than one “definition” (like <em>parsers </em>and <em>sparser</em>).</p>
<p>Now we can find all the anagrams for a given string simply by sorting the input string and searching for matches in the dictionary. Here are the Plain English routines that do that:</p>
<p><span style="color:#00ccff;">To run:</span><br />
<span style="color:#00ccff;"> Start up.</span><br />
<span style="color:#00ccff;"> Create the dictionary.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Write &#8220;Enter a scrambled word: &#8221; on the console without advancing.</span><br />
<span style="color:#00ccff;"> Read a string from the console. If the escape key is down, break.</span><br />
<span style="color:#00ccff;"> Unscramble the string.</span><br />
<span style="color:#00ccff;"> Repeat.</span><br />
<span style="color:#00ccff;"> Write &#8220;Done&#8230;&#8221; on the console.</span><br />
<span style="color:#00ccff;"> Destroy the dictionary.</span><br />
<span style="color:#00ccff;"> Shut down.</span></p>
<p><span style="color:#00ccff;">To unscramble a string:</span><br />
<span style="color:#00ccff;"> Trim the string.</span><br />
<span style="color:#00ccff;"> Lowercase the string.</span><br />
<span style="color:#00ccff;"> Sort the string into a sorted string.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Get an entry from the dictionary&#8217;s entries.</span><br />
<span style="color:#00ccff;"> If the entry is nil, break.</span><br />
<span style="color:#00ccff;"> If the entry&#8217;s sorted string is not the sorted string, repeat.</span><br />
<span style="color:#00ccff;"> Write the entry&#8217;s string on the console.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>A typical run looks like this:</p>
<p><img loading="lazy" class="alignnone size-full wp-image-323917" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/anagram-run-1.jpg" alt="" width="651" height="490" /></p>
<p>It takes a second or two to create the dictionary, but that only has to be done once, and it can be saved for later use. The unscrambling part is aaeinnnossttu. Whoops! Sorry. I meant to say, “instantaneous.”</p>
</div>
]]></html><thumbnail_url><![CDATA[https://i0.wp.com/cdncontribute.geeksforgeeks.org/wp-content/uploads/anagram-run-1.jpg?fit=440%2C330&ssl=1]]></thumbnail_url><thumbnail_width><![CDATA[438]]></thumbnail_width><thumbnail_height><![CDATA[330]]></thumbnail_height></oembed>