🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # freddie2025's solution

## to Diamond in the C# Track

Published at Jan 29 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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····
``````

## Hints

The tests in this exercise are different from your usual tests. Normally, a test checks if for a given input, the output matches the expected value. This is called value-based testing. However, this exercise uses property-based testing, where the tests check if for a range of inputs, the output has a specific property. The two key differences that differentiate property-based testing from value-based testing are:

1. A property-based test works not with a single input value, but with many.
2. A property-based test verifies properties, not concrete values.

For this exercise, the tests all verify a property of the diamond shape your code should be producing. Furthermore, all tests check if the property they test holds for all valid input letters ('A' to 'Z').

In order to facilitate propery-based testing, the tests in this exercise leverage the FsCheck and FsCheck.Xunit packages. To learn more about FsCheck, the documentation on the framework can be found here.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit Diamond.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

### DiamondTest.cs

``````﻿using Xunit;
using FsCheck.Xunit;
using FsCheck;
using System;
using System.Linq;

public class DiamondTest
{
public static readonly char[] AllLetters = GetLetterRange('A', 'Z');
private static string[] Rows(string x) => x.Split(new[] { '\n' }, StringSplitOptions.None);

private static string LeadingSpaces(string x) => x.Substring(0, x.IndexOfAny(AllLetters));
private static string TrailingSpaces(string x) => x.Substring(x.LastIndexOfAny(AllLetters) + 1);
private static char[] GetLetterRange(char min, char max) => Enumerable.Range(min, max - min + 1).Select(i => (char)i).ToArray();

[DiamondProperty]
public void Diamond_is_not_empty(char letter)
{
var actual = Diamond.Make(letter);

Assert.NotEmpty(actual);
}

[DiamondProperty(Skip = "Remove to run test")]
public void First_row_contains_a(char letter)
{
var actual = Diamond.Make(letter);
var rows = Rows(actual);
var firstRowCharacters = rows.First().Trim();

Assert.Equal("A", firstRowCharacters);
}

[DiamondProperty(Skip = "Remove to run test")]
public void All_rows_must_have_symmetric_contour(char letter)
{
var actual = Diamond.Make(letter);
var rows = Rows(actual);

Assert.All(rows, row =>
{
});
}

[DiamondProperty(Skip = "Remove to run test")]
public void Top_of_figure_has_letters_in_correct_order(char letter)
{
var actual = Diamond.Make(letter);
var rows = Rows(actual);

var expected = GetLetterRange('A', letter);
var firstNonSpaceLetters = rows.Take(expected.Length).Select(row => row.Trim());

Assert.Equal(firstNonSpaceLetters, expected);
}

[DiamondProperty(Skip = "Remove to run test")]
public void Figure_is_symmetric_around_the_horizontal_axis(char letter)
{
var actual = Diamond.Make(letter);

var rows = Rows(actual);
var top = rows.TakeWhile(row => !row.Contains(letter));
var bottom = rows.Reverse().TakeWhile(row => !row.Contains(letter));

Assert.Equal(bottom, top);
}

[DiamondProperty(Skip = "Remove to run test")]
public void Diamond_has_square_shape(char letter)
{
var actual = Diamond.Make(letter);

var rows = Rows(actual);
var expected = rows.Length;

Assert.All(rows, row =>
{
Assert.Equal(expected, row.Length);
});
}

[DiamondProperty(Skip = "Remove to run test")]
public void All_rows_except_top_and_bottom_have_two_identical_letters(char letter)
{
var actual = Diamond.Make(letter);

var rows = Rows(actual).Where(row => !row.Contains('A'));

Assert.All(rows, row =>
{
var twoCharacters = row.Replace(" ", "").Length == 2;
var identicalCharacters = row.Replace(" ", "").Distinct().Count() == 1;
Assert.True(twoCharacters && identicalCharacters, "Does not have two identical letters");
});
}

[DiamondProperty(Skip = "Remove to run test")]
public void Bottom_left_corner_spaces_are_triangle(char letter)
{
var actual = Diamond.Make(letter);

var rows = Rows(actual);

var cornerSpaces = rows.Reverse().SkipWhile(row => !row.Contains(letter)).Select(LeadingSpaces);
var spaceCounts = cornerSpaces.Select(row => row.Length).ToList();
var expected = Enumerable.Range(0, spaceCounts.Count).Select(i => i).ToList();

Assert.Equal(expected, spaceCounts);
}
}

public class DiamondPropertyAttribute : PropertyAttribute
{
public DiamondPropertyAttribute()
{
Arbitrary = new[] { typeof(LettersOnlyStringArbitrary) };
}
}

public static class LettersOnlyStringArbitrary
{
public static Arbitrary<char> Chars()
{
return Arb.Default.Char().Filter(x => x >= 'A' && x <= 'Z');
}
}``````
``````﻿using System;
using System.Text;
using System.Linq;

public static class Diamond
{
public static string Make(char target)
{
if(target == 'A') return "A";

var sb = new StringBuilder();
var targetPostion = (int)target - 64;
var emptyRow = new String(' ', (targetPostion*2) -1);
var rowLength = emptyRow.Length;
var leftIndex = (rowLength / 2);
var rightIndex = (rowLength / 2);

for(int i = 0; i <= (int)target - 65; i++)
{
char[] row = emptyRow.ToCharArray();
row[leftIndex--] = (char)(i + 65);
row[rightIndex++] = (char)(i + 65);
sb.Append(row);
sb.Append('\n');
}

sb.Append(new string(sb.ToString().Trim('\n').Reverse().ToArray()[(rowLength+1)..]));

return sb.ToString().Trim('\n');
}
}``````