Menu
Topics Index
...
`

Weekend Hack - 20 Jul 2013 - Results

Thanks to Sreeram Ajay, Prathyusha Ponnam, Keerthi K, Pramod Jain and Karteek Paruchuri for spending so much time, coming up with original and innovative answers and for submitting them. Thanks to many others who have tried to get the solution.
We have reviewed the solutions as per the criteria mentioned at Weekend Hack and included the comments below. Given the experience of the participants, they were really innovative and clean. Few minor comments found are included below in the order of the submission. These comments are not intended to point out the mistakes in the solutions but to learn from them.

The winner of the Weekend Hack 20 Jul 2013 - 1 is Pramod Jain. He will get a recharge or gift voucher worth Rs. 125.

To draw alphabet rectangles

Write a program to draw alphabet rectangles using given input. In the input the rectangles are given as start-x, start-y, width and height. Eg., [{1,4,2,3}] In this the rectangle starts at (1,4) positions having 2 columns and 3 rows. The rectangle should print alphabets from A-Z and restart from A when it crosses Z. When two rectangles intersect the alphabets should be added and the resultant alphabet should be printed. For eg. 'A' + 'A' = 'B', 'M' + 'C' = 'P', 'X' + 'D' = 'B'

Input (int[][]) Output
{{0,0,3,4}} ABC
DEF
GHI
JKL
{{0,0,6,6}} ABCDEF
GHIJKL
MNOPQR
STUVWX
YZABCD
EFGHIJ
{{1,0,2,3}} .AB
.CD
.EF
{{0,5,10,1}, {5,0,1,10}} .....A....
.....B....
.....C....
.....D....
.....E....
ABCDELGHIJ
.....G....
.....H....
.....I....
.....J....
{{0,3,4,4}, {5,0,3,3}} .....ABC
.....DEF
.....GHI
ABCD....
EFGH....
IJKL....
MNOP....
{{0, 0, 3, 3}, {1, 1, 3, 3}} ABC.
DFHC
GLNF
.GHI

Solution 1 - Submitted on Sat Jul 20 17:11:18 2013

class DrawAlphabetRectangles {

    public static void main(String s[]) {
        int[][] rects = {{0, 3, 4, 4}, {5, 0, 3, 3}};
        drawRects(rects);
    }

    public static void drawRects(int[][] rects) {
        int a[][] = rects;
        int i, j;
        int maxx, maxy;
        maxx = a[0][0] + a[0][2];
        maxy = a[0][1] + a[0][3];
        for (i = 1; i < a.length; i++) {
            if ((a[i][0] + a[i][2]) > maxx)
                maxx = a[i][0] + a[i][2];
            if ((a[i][1] + a[i][3]) > maxy)
                maxy = a[i][1] + a[i][3];
        }
        int p[][] = new int[maxx][maxy];
        for (i = 0; i < maxx; i++)
            for (j = 0; j < maxy; j++)
                p[i][j] = 0;
        for (int[] element : a) {
            int count = 65;
            for (i = element[1]; i < element[1] + element[3]; i++) {
                for (j = element[0]; j < element[0] + element[2]; j++) {
                    if (count > 90)
                        count = 65;
                    if (p[j][i] == 0)
                        p[j][i] = count;
                    else {
                        p[j][i] = (p[j][i] - 64) + (count - 64);
                        p[j][i] = p[j][i] + 64;
                    }
                    while (p[j][i] > 90)
                        p[j][i] = p[j][i] - 26;
                    count++;
                }
            }
        }
        for (i = 0; i < maxy; i++) {
            for (j = 0; j < maxx; j++) {
                if (p[j][i] != 0)
                    System.out.print((char) p[j][i]);
                else
                    System.out.print(".");
            }
            System.out.println();
        }
    }
}

Comments :

Solution 2 - Submitted on Sat Jul 20 23:56:46 2013

class DrawAlphabetRectangles {

    public static void main(String s[]) {
        int[][] rects = {{0, 3, 4, 4}, {5, 0, 3, 3}};
        drawRects(rects);
    }

    public static void drawRects(int[][] rects) {
        int l = rects.length, i, j, k;
        int b, c;
        {
            int[] x = new int[l];
            int[] y = new int[l];
            int[] m = new int[l];
            int[] n = new int[l];
            char[] ch = new char[l];
            int m1 = 0, n1 = 0;
            for (i = 0; i < l; i++) {
                x[i] = rects[i][0];
                y[i] = rects[i][1];
                m[i] = x[i] + rects[i][2];
                n[i] = y[i] + rects[i][3];
                ch[i] = 'A';
            }
            for (i = 0; i < l; i++) {
                if (m[i] > m1)
                    m1 = m[i];
                if (n[i] > n1)
                    n1 = n[i];
            }
            char[][] a = new char[n1][m1];
            for (i = 0; i < n1; i++)
                for (j = 0; j < m1; j++)
                    a[i][j] = '.';
            for (k = 0; k < l; k++)
                for (i = y[k]; i < n[k]; i++) {
                    for (j = x[k]; j < m[k]; j++) {
                        if (a[i][j] == '.')
                            a[i][j] = ch[k];
                        else {
                            c = ch[k] - 64;
                            c = c % 26;
                            b = a[i][j] + c;
                            if (b <= 90)
                                a[i][j] = (char) b;
                            else {
                                b = b - 26;
                                a[i][j] = (char) b;
                            }
                        }
                        if (ch[k] == 'Z')
                            ch[k] = 'A';
                        else
                            (ch[k])++;
                    }
                }
            for (i = 0; i < n1; i++) {
                for (j = 0; j < m1; j++)
                    System.out.print(a[i][j]);
                System.out.println();
            }
        }
    }
}

Comments :

Solution 3 - Submitted on Sun Jul 21 15:41:09 2013

class DrawAlphabetRectangles {

    public static void main(String s[]) {
        int[][] rects = {{0, 3, 4, 4}, {5, 0, 3, 3}};
        drawRects(rects);
    }

    public static void drawRects(int[][] rects) {
        int height, width;
        int size = rects.length;
        if (size == 1) {
            width = rects[0][2] + rects[0][0];
            height = rects[0][3] + rects[0][1];
        } else {
            height = max_h(rects, size);
            width = max_w(rects, size);
        }
        int[][] display = new int[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                display[i][j] = 46;
            }
        }
        int counter = 65;
        for (int i = rects[0][1]; i < rects[0][3] + rects[0][1]; i++) {
            for (int j = rects[0][0]; j < rects[0][2] + rects[0][0]; j++) {
                display[i][j] = counter;
                counter++;
                if (counter == 91) {
                    counter = counter - 26;
                }
            }
        }
        if (size != 1) {
            for (int rect = 1; rect < size; rect++) {
                counter = 65;
                for (int i = rects[rect][1]; i < rects[rect][3] + rects[rect][1]; i++) {
                    for (int j = rects[rect][0]; j < rects[rect][2] + rects[rect][0]; j++) {
                        if (display[i][j] == 46) {
                            display[i][j] = counter;
                            counter++;
                        } else {
                            display[i][j] = (display[i][j] + counter - 128);
                            display[i][j] = (display[i][j] % 26) + 64;
                            counter++;
                        }
                    }
                }
            }
        }
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                System.out.print((char) (display[i][j]));
            }
            System.out.println();
        }
    }

    public static int max_h(int[][] rects, int size) {
        int maxim = rects[0][3] + rects[0][1];
        for (int i = 1; i < size; i++) {
            maxim = Math.max(maxim, rects[i][3] + rects[i][1]);
        }
        return maxim;
    }

    public static int max_w(int[][] rects, int size) {
        int maxim = rects[0][2] + rects[0][0];
        for (int i = 1; i < size; i++) {
            maxim = Math.max(maxim, rects[i][0] + rects[i][2]);
        }
        return maxim;
    }
}

Comments :

Solution 4 - Submitted on Sun Jul 21 17:18:05 2013

class DrawAlphabetRectangles {

    public static void main(String s[]) {
        int[][] rects = {{0, 3, 4, 4}, {5, 0, 3, 3}};
        drawRects(rects);
    }

    public static void drawRects(int[][] rects) {
        int totalRows = 0;
        int totalColumns = 0;
        // Total number of rows is maximum of sum of starting row and number of values to be printed in a row i.e, max of (rects[i][1]+ rects[i][3])
        // Total number of columns is maximum of sum of starting column and number of values to be printed in a column i.e, max of (rects[i][0]+ rects[i][2])
        // Loop for calculating number of rows and columns required
        for (int[] rect : rects) {
            if (rect[1] + rect[3] > totalRows)
                totalRows = rect[1] + rect[3];
            if (rect[0] + rect[2] > totalColumns)
                totalColumns = rect[0] + rect[2];
        }
        char[][] result = new char[totalRows][totalColumns];
        for (int[] rect : rects) {
            char char_incrementor = 'A';
            for (int j = rect[1], rowCount = 0; rowCount < rect[3]; rowCount++) {
                for (int k = rect[0], columnCount = 0; columnCount < rect[2]; columnCount++) {
                    int row = j + rowCount;
                    int column = k + columnCount;
                    // If no value is present before , insert the present character i.e, char_incrementor
                    if (!(result[row][column] >= 'A' && result[row][column] <= 'Z'))
                        result[row][column] = char_incrementor;
                    // If value was already present then we need to add the ascii value of difference between char_incrementor and 'A' to previous character
                    // If the resultant value is below or equal to 'Z' then insert that value 
                    else if (result[row][column] + char_incrementor + 1 - 'A' <= 'Z')
                        result[row][column] = (char) (result[row][column] + char_incrementor + 1 - 'A');
                    // Else if resultant value is greater than z then we need to rotate it back from A so simply subtract 26 from the resultant value
                    else
                        result[row][column] = (char) (result[row][column] + char_incrementor + 1 - 'A' - 26);
                    // If character is 'Z' then make it 'A'
                    if (char_incrementor != 'Z')
                        char_incrementor++;
                    // else increment it to its next character
                    else
                        char_incrementor = 'A';
                }
            }
        }
        // Printing the values
        for (char[] element : result) {
            for (int j = 0; j < element.length; j++) {
                if (element[j] >= 'A' && element[j] <= 'Z')
                    System.out.print(element[j]);
                else
                    System.out.print(".");
            }
            System.out.println();
        }
    }
}

Comments :

Solution 5 - Submitted on Sun Jul 21 22:43:54 2013

class DrawAlphabetRectangles {

    public static void main(String s[]) {
        int[][] rects = {{0, 3, 4, 4}, {5, 0, 3, 3}};
        drawRects(rects);
    }

    public static void drawRects(int[][] rects) {
        if (rects.length == 1) {
            drawRectsArr(rects);
        }
        int val1 = 65;
        int val2 = 65;
        int x1 = rects[0][0] + rects[0][2];
        int x2 = rects[1][0] + rects[1][2];
        int y1 = rects[0][1] + rects[0][3];
        int y2 = rects[1][1] + rects[1][3];
        int row, column;
        if (x1 > x2) {
            column = x1;
        } else {
            column = x2;
        }
        if (y1 > y2) {
            row = y1;
        } else {
            row = y2;
        }
        int res[][] = new int[row][column];
        for (int p = 0; p < row; p++) {
            for (int z = 0; z < column; z++) {
                res[p][z] = 46;
            }
        }
        for (int i = rects[0][1]; i < rects[0][1] + rects[0][3]; i++) {
            for (int j = rects[0][0]; j < rects[0][0] + rects[0][2]; j++) {
                res[i][j] = val1;
                val1++;
            }
        }
        for (int i = rects[1][1]; i < rects[1][1] + rects[1][3]; i++) {
            for (int j = rects[1][0]; j < rects[1][0] + rects[1][2]; j++) {
                int temp = 0;
                int temp1 = 0;
                if (res[i][j] == 46) {
                    res[i][j] = val2;
                    val2++;
                } else {
                    temp = res[i][j] + val2;
                    if (temp < 90) {
                        res[i][j] = temp + 1;
                    } else {
                        temp1 = temp % 26;
                        res[i][j] = temp1 + 66;
                    }
                    val2++;
                }
            }
        }
        for (int p = 0; p < row; p++) {
            for (int z = 0; z < column; z++) {
                System.out.print((char) res[p][z]);
            }
            System.out.println();
        }
    }

    public static void drawRectsArr(int[][] rects) {
        int val1 = 65;
        int columns = rects[0][0] + rects[0][2];
        int rows = rects[0][1] + rects[0][3];
        int res[][] = new int[rows][columns];
        for (int p = 0; p < rows; p++) {
            for (int z = 0; z < columns; z++) {
                res[p][z] = 46;
            }
        }
        for (int j = 0; j < rects[0][3]; j++) {
            for (int k = rects[0][0]; k < columns; k++) {
                if (val1 > 90) {
                    val1 = 65;
                }
                res[j][k] = val1;
                val1++;
            }
        }
        for (int p = 0; p < rows; p++) {
            for (int z = 0; z < columns; z++) {
                System.out.print((char) res[p][z]);
            }
            System.out.println();
        }
        System.exit(0);
    }
}

Comments :

© meritcampus 2019

All Rights Reserved.

Open In App