Thursday, February 6, 2014

Solve Hangman With Linux

By using Linux and standard commands (cat and grep) that come with it we can easily solve hangman puzzle.

Lets see with an example.
Let us suppose the word is PLANET and so far we have guessed  _ _ ANE_
Lets assume that we have made incorrect guesses with letters b,s,a,i,u.

Now lets try to solve the hangman with the help of our Linux box.

We all know that there is a file called words that ships with all most all Linux distribution. The file contains dictionary word. Many people have used the file for many clever uses. Let us also try to use that file to solve our hangman.

In my box the file is in "/etc/dictionaries-common/words"

If you do:
cat /etc/dictionaries-common/words
it will show you all the words. Lets try to grep our answer with regular expression.

cat /etc/dictionaries-common/words | grep -iE ^..ane.$

here,
-i makes the grep case insensitive.
- E is for extended regular expression.
^ means starting of the word
$ means end of the word
. means a letter

so, ^..ane.$ means we are grepping for words that can have any first two letter followed by ane and one single letter.

Depending upon the answer this can sometime bring too much words. But we do know letter that doesnt match the word (b,s,a,i,u). Lets try to use this information to limit the output.

 cat /etc/dictionaries-common/words | grep -iE ^..an..$   | grep -ivE (b|s|a|i|u)
here we are again grepping the output but this time with reverse match(-v), i.e. words doesnt matching the letters inside the  brackets.

This hopefully will help you to solve most of the hangman.

Few more examples:
computer - cat /etc/dictionaries-common/words | grep -iE ^co..u...$   | grep -ivE (b|w|v|s|q) | less
cricket -   cat /etc/dictionaries-common/words | grep -iE ^cr.c..t$   | grep -ivE (b|w|v|s|q) | less
download
alternative link download

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.