Sha256: c0674e5b423f35c1206d6a605f92acc4baa1583d8acc2eb8d7a382da7b0b8a6d

Contents?: true

Size: 1.23 KB

Versions: 193

Compression:

Stored size: 1.23 KB

Contents

public class Transpose {
    public String transpose(String toTranspose) {

        StringBuilder transposedForm = new StringBuilder();
        String[] rows = toTranspose.split("\n");

        int maxRowSize = 0;

        for (int i = 0; i < rows.length; i++) {
            if (rows[i].length() > maxRowSize) {
                maxRowSize = rows[i].length();
            }
        }

        padRows(rows);

        for (int newRowOldCol = 0; newRowOldCol < maxRowSize; newRowOldCol++) {
            for (int newColOldRow = 0; newColOldRow < rows.length; newColOldRow++) {
                if (newRowOldCol < rows[newColOldRow].length()) {
                    transposedForm.append(rows[newColOldRow].charAt(newRowOldCol));
                }
            }

            if (newRowOldCol != maxRowSize - 1) {
                transposedForm.append("\n");
            }
        }

        return transposedForm.toString();
    }

    private void padRows(String[] rows) {
        for (int i = 0; i < rows.length; i++) {
            for (int j = i; j < rows.length; j++) {
                if(rows[j].length() > rows[i].length()) {
                    rows[i] = String.format("%-" + rows[j].length() + "s", rows[i]);
                }
            }
        }
    }
}

Version data entries

193 entries across 193 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.179 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.178 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.177 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.176 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.175 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.174 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.173 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.172 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.171 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.170 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.169 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.167 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.166 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.165 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.164 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.163 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.162 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.161 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java
trackler-2.2.1.160 tracks/java/exercises/transpose/.meta/src/reference/java/Transpose.java