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

damiankorz's solution

to Diamond in the CFML Track

Published at Dec 20 2018 · 0 comments
Instructions
Test suite
Solution

The diamond kata takes as its input a letter, and outputs it in a diamond shape. Given a letter, it prints a diamond starting with 'A', with the supplied letter at the widest point.

Requirements

  • The first row contains one 'A'.
  • The last row contains one 'A'.
  • All rows, except the first and last, have exactly two identical letters.
  • All rows have as many trailing spaces as leading spaces. (This might be 0).
  • The diamond is horizontally symmetric.
  • The diamond is vertically symmetric.
  • The diamond has a square shape (width equals height).
  • The letters form a diamond shape.
  • The top half has the letters in ascending order.
  • The bottom half has the letters in descending order.
  • The four corners (containing the spaces) are triangles.

Examples

In the following examples, spaces are indicated by characters.

Diamond for letter 'A':

A

Diamond for letter 'C':

路路A路路
路B路B路
C路路路C
路B路B路
路路A路路

Diamond for letter 'E':

路路路路A路路路路
路路路B路B路路路
路路C路路路C路路
路D路路路路路D路
E路路路路路路路E
路D路路路路路D路
路路C路路路C路路
路路路B路B路路路
路路路路A路路路路

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

Seb Rose http://claysnow.co.uk/recycling-tests-in-tdd/

Submitting Incomplete Solutions

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

DiamondTest.cfc

component extends="testbox.system.BaseSpec" {

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

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

			it( 'Degenerate case with a single ''A'' row', function(){
				expect( SUT.rows( letter='A' ) ).toBe( ["A"] );
			});

			it( 'Degenerate case with no row containing 3 distinct groups of spaces', function(){
				expect( SUT.rows( letter='B' ) ).toBe( [" A ","B B"," A "] );
			});

			it( 'Smallest non-degenerate case with odd diamond side length', function(){
				expect( SUT.rows( letter='C' ) ).toBe( ["  A  "," B B ","C   C"," B B ","  A  "] );
			});

			it( 'Smallest non-degenerate case with even diamond side length', function(){
				expect( SUT.rows( letter='D' ) ).toBe( ["   A   ","  B B  "," C   C ","D     D"," C   C ","  B B  ","   A   "] );
			});

			it( 'Largest possible diamond', function(){
				expect( SUT.rows( letter='Z' ) ).toBe( ["                         A                         ","                        B B                        ","                       C   C                       ","                      D     D                      ","                     E       E                     ","                    F         F                    ","                   G           G                   ","                  H             H                  ","                 I               I                 ","                J                 J                ","               K                   K               ","              L                     L              ","             M                       M             ","            N                         N            ","           O                           O           ","          P                             P          ","         Q                               Q         ","        R                                 R        ","       S                                   S       ","      T                                     T      ","     U                                       U     ","    V                                         V    ","   W                                           W   ","  X                                             X  "," Y                                               Y ","Z                                                 Z"," Y                                               Y ","  X                                             X  ","   W                                           W   ","    V                                         V    ","     U                                       U     ","      T                                     T      ","       S                                   S       ","        R                                 R        ","         Q                               Q         ","          P                             P          ","           O                           O           ","            N                         N            ","             M                       M             ","              L                     L              ","               K                   K               ","                J                 J                ","                 I               I                 ","                  H             H                  ","                   G           G                   ","                    F         F                    ","                     E       E                     ","                      D     D                      ","                       C   C                       ","                        B B                        ","                         A                         "] );
			});

		});
		
	}
 
}

SolutionTest.cfc

component extends="DiamondTest" {

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

}
component {
	/**
	* @returns
	*/
	public string function generateSpaces( required numeric number ){
		var spaceString = "";
		for( var i = 1; i <= number; i++ ){
			spaceString = spaceString & " ";
		}
		return spaceString;
	}
	public array function rows( required string letter ) {
		var values = {
			"A" = 1,
			"B" = 2,
			"C" = 3,
			"D" = 4,
			"E" = 5,
			"F" = 6,
			"G" = 7,
			"H" = 8,
			"I" = 9,
			"J" = 10,
			"K" = 11,
			"L" = 12,
			"M" = 13,
			"N" = 14,
			"O" = 15,
			"P" = 16,
			"Q" = 17,
			"R" = 18,
			"S" = 19,
			"T" = 20,
			"U" = 21,
			"V" = 22,
			"W" = 23,
			"X" = 24,
			"Y" = 25,
			"Z" = 26
		};
		var firstHalf = [];
		var secondHalf = [];
		var output = [];
		var center = 1;
		for( var i = 1; i <= values[letter]; i++){
			var alpha = structFindValue(values, i, "one")[1].key;
			var leftRight = values[letter] - i;
			if( i == 1 ){
				arrayAppend(firstHalf, generateSpaces( leftRight ) & alpha & generateSpaces( leftRight  ) );
			}
			else{
				arrayAppend(firstHalf, generateSpaces( leftRight  ) & alpha & generateSpaces(center) & alpha & generateSpaces( leftRight  ) );
				center += 2;
			}

		}
		for( var i = arrayLen(firstHalf) - 1; i >= 1; i--){
			arrayAppend(secondHalf, firstHalf[i]);
		}
		for( var i = 1; i <= arrayLen(firstHalf); i++){
			arrayAppend(output, firstHalf[i] );
		}
		for( var i = 1; i <= arrayLen(secondHalf); i++){
			arrayAppend(output, secondHalf[i]);
		}
		return output;
	}
}

What can you learn from this solution?

A huge amount can be learned from reading other people鈥檚 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?