🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of cedricvaneer

cedricvaneer's solution

to Anagram in the CFML Track

Published at Dec 05 2019 · 0 comments
Instructions
Test suite
Solution

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".


To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, cd into the exercise folder and run the following:

box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

AnagramTest.cfc

component extends="testbox.system.BaseSpec" {

	function beforeAll(){
	  SUT = createObject( 'Anagram' );
	}

	function run(){
	
		describe( "My Anagram class", function(){			

			it( 'no matches', function(){
				expect( SUT.anagrams( subject='diaper', candidates=["hello", "world", "zombies", "pants"] ) ).toBe( [] );
			});

			it( 'detects two anagrams', function(){
				expect( SUT.anagrams( subject='master', candidates=["stream", "pigeon", "maters"] ) ).toBe( ["stream","maters"] );
			});

			it( 'does not detect anagram subsets', function(){
				expect( SUT.anagrams( subject='good', candidates=["dog", "goody"] ) ).toBe( [] );
			});

			it( 'detects anagram', function(){
				expect( SUT.anagrams( subject='listen', candidates=["enlists", "google", "inlets", "banana"] ) ).toBe( ["inlets"] );
			});

			it( 'detects three anagrams', function(){
				expect( SUT.anagrams( subject='allergy', candidates=["gallery", "ballerina", "regally", "clergy", "largely", "leading"] ) ).toBe( ["gallery","regally","largely"] );
			});

			it( 'does not detect non-anagrams with identical checksum', function(){
				expect( SUT.anagrams( subject='mass', candidates=["last"] ) ).toBe( [] );
			});

			it( 'detects anagrams case-insensitively', function(){
				expect( SUT.anagrams( subject='Orchestra', candidates=["cashregister", "Carthorse", "radishes"] ) ).toBe( ["Carthorse"] );
			});

			it( 'detects anagrams using case-insensitive subject', function(){
				expect( SUT.anagrams( subject='Orchestra', candidates=["cashregister", "carthorse", "radishes"] ) ).toBe( ["carthorse"] );
			});

			it( 'detects anagrams using case-insensitive possible matches', function(){
				expect( SUT.anagrams( subject='orchestra', candidates=["cashregister", "Carthorse", "radishes"] ) ).toBe( ["Carthorse"] );
			});

			it( 'does not detect a anagram if the original word is repeated', function(){
				expect( SUT.anagrams( subject='go', candidates=["go Go GO"] ) ).toBe( [] );
			});

			it( 'anagrams must use all letters exactly once', function(){
				expect( SUT.anagrams( subject='tapper', candidates=["patter"] ) ).toBe( [] );
			});

			it( 'capital word is not own anagram', function(){
				expect( SUT.anagrams( subject='BANANA', candidates=["Banana"] ) ).toBe( [] );
			});

		});
		
	}
 
}

SolutionTest.cfc

component extends="AnagramTest" {

	function beforeAll(){
	  SUT = createObject( 'Solution' );
	}

}
/**
* Your implmentation of the Anagram exercise
*/
component {

  /**
	* @word word we want the map of
	* @returns a map with all letters of the @word and for each letter, the count
	*/
  private struct function mapWord(required string word) {
    var wordMap = {};
    for (var letter in listToArray(arguments.word, "")) {
      if (structKeyExists(wordMap, letter)) {
        wordMap[letter] += 1;
      } else {
        wordMap[letter] = 1;
      }
    }
    return wordMap;
  }

  /**
	* @subject word we're looking if the @candidate is an anagrams of
	* @candidate candidate of anagram of the @subject
	* @returns true if @candidate is an anagram of @subject
	*/
  private boolean function isAnagram(required string subject, required string candidate) {
    if (lCase(arguments.subject) == lCase(arguments.candidate)) {
      return false;
    }
    var subjectMap = mapWord(arguments.subject);
    var candidateMap = mapWord(arguments.candidate);
    for (var key in candidateMap) {
      if (!structKeyExists(subjectMap, key)) {
        return false;
      }
      if (subjectMap[key] != candidateMap[key]) {
        return false;
      }
    }
		return true;
	}

  /**
	* @subject word we're looking the anagrams of
	* @candidates an array of candidates of anagrams of the @subject
	* @returns an array of anagrams of one word from an array of candidates
	*/
  public array function anagrams(required string subject, required array candidates) {
    var arrayOfAnagrams = [];
    for (var candidate in arguments.candidates) {
      if (isAnagram(arguments.subject, candidate)) {
        arrayOfAnagrams.append(candidate);
      }
    }
    return arrayOfAnagrams;
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?