Sha256: b56bb35c6ab24552fd5a2a068dc34498a3fdbd0367ebbed1d6d8800736691119

Contents?: true

Size: 1.24 KB

Versions: 102

Compression:

Stored size: 1.24 KB

Contents

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

final class DiamondPrinter {

    private static final int A_INT = (int) 'A';

    private static String blank(final int length) {
        return String.join("", Collections.nCopies(length, " "));
    }

    private static String reverse(final String string) {
        return new StringBuilder(string).reverse().toString();
    }

    List<String> printToList(final char chr) {
        final int nRows = 2 * ((int) chr - A_INT) + 1;

        final List<String> topRows = new ArrayList<>();

        // Populate the top rows.
        for (int nRow = 0; nRow < (nRows + 1) / 2; nRow++) {
            final char rowChr = (char) (A_INT + nRow);

            final String leftHalfOfRow  = blank((nRows - 1) / 2 - nRow) + rowChr + blank(nRow);
            final String rightHalfOfRow = reverse(leftHalfOfRow.substring(0, leftHalfOfRow.length() - 1));
            final String fullRow        = leftHalfOfRow + rightHalfOfRow;

            topRows.add(fullRow);
        }

        // Populate the bottom rows by 'reflecting' the top rows.
        for (int nRow = (nRows - 1) / 2 - 1; nRow >= 0; nRow--) {
            topRows.add(topRows.get(nRow));
        }

        return topRows;
    }

}

Version data entries

102 entries across 102 versions & 1 rubygems

Version Path
trackler-2.0.6.44 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.43 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.42 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.41 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.40 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.39 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.38 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.37 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.36 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.35 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.34 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.33 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.32 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.31 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.30 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.29 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.28 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.27 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.26 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.6.25 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java