Word game: Finding anadromes with Linux

Word game Finding anadromes with Linux

In these anxious instances, one strategy to distract ourselves from the gloom is by enjoying phrase video games. With this thought in thoughts, I challenged myself to determine phrases that, spelled backwards, would nonetheless be phrases.

Instead of dishonest by doing a easy Google search, I cheated by utilizing my Linux instructions abilities. So, on this publish, we’ll take a look at how Linux instructions and assets can be utilized to determine such phrases.

Defining the search

Before we get began on the Linux search approach, I ought to level out that what I used to be on the lookout for weren’t simply palindromes – phrases like “civic” and “deified” that learn the identical from left to proper as they do proper to left. Instead, I used to be additionally on the lookout for phrases like “reward” and “decaf” that flip into completely different phrases – on this case, “drawer” and “faced” — when one reads them backwards.

For the file, one of many many phrases that individuals have used to outline phrases which might be reversible on this method is “anadromes” and the longest recognized anadromes in English are “stressed” and “desserts” (confirmed by my little undertaking). I’m additionally together with palindromic phrases.

Using the phrases file

For my phrase useful resource, I used the Linux phrases file. It is likely to be /usr/dict/phrases or /usr/share/dict/phrases in your system. The phrases file on the system I’m utilizing for this publish has over 100,000 phrases. You can use the wc command to do the counting.

$ wc -l /usr/share/dict/phrases
102403 /usr/share/dict/phrases

One of the peculiarities of the phrases file is that it accommodates single-letter separator traces between every group of phrases beginning with the identical letter (for phrases with preliminary caps and preliminary lowercase letters). Here, as examples, are the beginning of each the B and b sections:

B		b
B's		baa
BBB		baa’s
BBB's	        baaed
BMW		baaing
BMW's	        baas

The phrases file additionally consists of loads of correct names, acronyms and abbreviations. Proper names (like “Ada”) received’t work as anadromes as a result of names don’t usually qualify as “words” and since reversals would capitalize the ultimate latter (like “adA”). The phrases file additionally accommodates phrases with ‘s on the finish (possessives and contractions like “isle’s” and “isn’t”). Since I don’t rely any of those entries as phrases, I crafted my approach to keep away from contemplating them.

In addition, the one reputable single-letter phrases are “a” and “I”. Since these ought to rely towards the entire, I added these to my phrase reversal checklist in some initials steps to make the rest of the processing simpler by discounting the one letter part headings whereas looping by means of the phrases file.

While omitting single-letter entries, phrases in all caps and possessives, the script that I put collectively to seek out the reversible phrases runs by means of the entire remaining phrases, reverses every of them after which appears again on the phrases file to see if the reversed phrase can also be within the file.

Starting the script

At the start of the script, I set the beginning rely to 2 and show the phrases “a” and “I”:

#!/bin/bash

# begin with 2 for "a" and "I" (single letters prevented beneath)
rely=2
echo a
echo I

Looping by means of the phrases file

I then run by means of the phrases file. The best strategy to choose solely the phrases that may qualify (no caps, no apostrophes and so forth), was to make use of the grep expression grep -E ‘^[a-z]{2,23}*$’. This selects all lowercase phrases with from 2 to 23 letters. I had individually decided that the longest phrase within the phrases file (electroencephalograph’s) has 23 letters.

for phrase in `cat /usr/share/dict/phrases | grep -E '^[a-z]{2,23}*$'`

Reversing every phrase

I exploit the rev command to reverse every phrase the for loop produced. If I had been doing this manually, a reversal may appear like this:

$ echo caviar | rev
raivac

Here’s the reversal within the script:

revword=`echo $phrase | rev`

I then search for the phrase in its reversed type to see if it additionally exists within the phrases file. This grep command shows the phrase if it’s discovered. If grep doesn’t discover the phrase, no output is generated. The ^ and second $ signal be sure that it’s solely discovering full phrases.

   grep ^$revword$ /usr/share/dict/phrases

Counting the finds

If the grep command succeeds (i.e., if it finds the reversal of the phrase), we all know the reversal is a phrase too and add it to our rely. Note that $? represents the exit standing generated by the grep command and zero signifies that no errors had been encountered (i.e., the phrase was discovered).

   if [ $? -eq 0 ]; then
      ((rely=rely+1))
   fi

I then finish the loop by means of the phrases file and show the rely of anadromes. Words which might be palindromic (like “civic”), may have been counted as soon as. Words that aren’t palindromic may have been counted twice (as soon as for every of the phrases). As a outcome, the ultimate rely is likely to be an odd or a good quantity.

echo $rely reversible phrases discovered

Calculating the share

To fulfill my curiosity, I additionally added some instructions on the finish of the script to calculate the share of entries within the phrases file that qualify as anadromes.

# decide the share
phrases=`wc -l /usr/share/dict/phrases`
p.c=`echo $rely $phrases | awk '{print $1/$2*100}'`
echo -n "Percentage: "
echo $p.c% 

The proportion in my case turned out to be .399%. That’s rather less than half a p.c – lower than one phrase in each 200. Still, I used to be shocked that there have been so many.

The script

The script in its entirety is proven beneath and features a few feedback so as to add to its readability. In this model of the script, I put the entire positioned anadromes right into a file.

#!/bin/bash

output="anadromes"

# begin with 1 for "a" (single letter entries prevented beneath)
rely=2
echo a > $output
echo I >> $output

# discover phrases that, when reversed, are nonetheless phrases
for phrase in `cat /usr/share/dict/phrases | grep -E '^[a-z]{2,23}*$'`
do
   revword=`echo $phrase | rev`
   grep ^$revword$ /usr/share/dict/phrases >> $output
   if [ $? -eq 0 ]; then
      ((rely=rely+1))
   fi
performed

# show a phrase rely
echo $rely reversible phrases discovered

# decide proportion
phrases=`wc -l /usr/share/dict/phrases`
p.c=`echo $rely $phrases | awk '{print $1/$2*100}'`
echo -n "Percentage: "
echo $p.c%

What did I see?

If you run the script as proven above, you should utilize the column command to get a properly formatted itemizing of the anadromes that it has recognized. Here’s the beginning of what I discovered:

$ column anadromes
a               ogre            dim             redder          wets
I               ate             slim            warder          knits
tuba            eve             denim           deer            loots
raga            stay            minim           leer            pots
ha              relive          mother             fer             spots
aha             ewe             doom            refer           components
raja            eye             room            lager           warts
ma              decaf           um              denier          desserts
period             ref             gum             eviler          ports
sera            golf            mum             timer           sports activities
…

The script discovered 409 anadromes within the phrases file.

Wrap-Up

A easy problem turned out to be a script that exercised a few of my Linux abilities. These included:

Looping by means of the phrases file
Crafting a grep command that will seize solely what I needed
Reversing every of the phrases (final letters first)
Counting the anadromes discovered
Calculating the share of phrases that match my standards

I’m not but positive what tomorrow’s problem shall be, however fixing issues with Linux instruments is a really satisfying psychological train!

Now see

Copyright © 2020 , Inc.

Spread the love