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

damiankorz's solution

to Anagram in the CFML Track

Published at Dec 26 2018 · 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' );
	}

}
component {
	/**
	* @returns
	*/
	public array function anagrams( string subject, array candidates ) {
		if( subject != candidates[1]){
			subject = listSort( subject, "textnocase", "asc", "");
			var output = [];
			for( var i = 1; i <= arrayLen(candidates); i++ ){
				var candidate = listSort( candidates[i], "textnocase", "asc", "");
				if( candidate == subject ){
					arrayAppend(output, candidates[i]);
				}
			}
			return output;
		}else{
			return [];
		}
	}
}

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?