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.8.40 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.39 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.38 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.37 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.36 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.35 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.34 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.33 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.32 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.31 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.30 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.29 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.28 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.27 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.26 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.24 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.23 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.22 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.21 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java
trackler-2.0.8.20 tracks/java/exercises/diamond/src/example/java/DiamondPrinter.java