Anagrams: Raga Mans

Anagrams are words or phrases that are created from the letters of another word or phrase. They are useful in games like Scrabble or Boggle as well as being a fun way to show your skill in coding.

The Challenge

This challenge was inspired by a code kata from Dave Thomas that can be found here. We’re taking his kata and making it our own. Though since he’s already created a thorough word list we’ll be using that as part of this challenge.

Dave Thomas Word List

In the challenge we’ll be looking at the characters in a word, counting them, and manipulating them. We’ll even turn it into a fun text based game. For each level you may use the command line for input and output or create a view to display on the web.

Creating a Test Case

Like in past challenges we’ll be using our tests to drive our development. No matter the level you choose to work with write a test case before you start your code. Then write just enough code to pass the test. Once you have passing code refactor to pass other test cases.

Level 1

For level 1 we’ll build a useful tool for counting the number of characters in a word or phrase. Start out by taking in a word or phrase as input. Then ignoring the spaces return the total number of characters in that word or phrase. Write unit tests for both words and phrases. Next return only the letters. Finally return the number of punctuation marks (.:;,!?)

Input:

1
2
Input a word or phrase:
This is a test.

Output:

1
2
3
Characters: 11
Letters: 10
Punctuation: 1

A Step Further

Going a step further we’ll add some math into the mix. Calculate the possible number of combinations of the letters in the phrase.

Input:

1
2
Input a word or phrase:
Test!

Output:

1
2
3
4
Characters: 5
Letters: 4
Punctuation: 1
Combinations: 24

Hint: The formula for calculating the possible combinations is n! or n(n - 1)(n - 2)…

Level 2

Instead of looking for the number of possible combinations of letters from the input create a list of combinations and then check that on the word list provided to see if the combination is an anagram or just gibberish.

Input:

1
2
Input a word to find it's anagrams:
start

Output:

1
2
tarts
trats

A Step Further

Going a step further include in your results phrases or multiple words with the same set of characters.

Input:

1
2
Input a word or phrase to find it's anagrams:
start

Output:

1
2
3
4
5
tarts
trats
rat St
art St
tar St

Level 3

Now the fun begins. We are creating a game of anagrams. Instead of taking in a word or phrase have the player input the number of characters for the word with a minimum of three characters. Next randomly select a word from the word list with that number of characters and display it to the player.

Allow the player to input different anagrams for the word selected. Calculate points based on the following scheme:

Input Points
Correct +2
Incorrect -1

Display the player’s score after they input their anagrams.

The display should look something like this:

1
2
3
4
5
6
7
8
Enter the number of characters for the word:
5
Your word is 'start' enter anagrams separated by a comma:
tarts, ratts, trats, rat st
Score: 5
Total Possible: 10

A Step Further

Make the game more interesting by adding a timer to limit the amount of time the player has to enter anagrams. If you have time make the game multiplayer and track scores for each player in the round.