Menu
Topics Index
...
`

Weekend Hack - 05 Oct 2013 - Results

Thanks to Nishan K Sharma 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 05 Oct 2013 - 1 is Nishan K Sharma. He will get a recharge or gift voucher worth Rs. 175.

Print the network using the connections

Write a program to print the network using the given connections. The network nodes are letters like A, B, C, D etc. Every connection is defined by two nodes and the direction. The valid directions are L for Left, R for Right, T for Top and B for Bottom. If the connection is A-B-R, it means B is to the right of A, similarly C-E-B, it means E is to the bottom of C.

Input (List) Printed Output
[A-B-R, B-C-R, C-D-R, D-E-R, E-F-R] A----B----C----D----E----F
[D-B-L, A-B-R, C-A-T, C-G-B, H-G-L, H-I-R, I-F-T] A----B----D
|         |
|         |
C         F
|         |
|         |
G----H----I
[A-B-R, C-A-T, C-D-R, E-D-L, E-G-B, G-F-L] A----B
|    |
|    |
C----D----E
     |    |
     |    |
     F----G
[A-B-R, C-A-T, C-G-B, H-G-L, H-I-R, I-F-T] A----B
|
|
C         F
|         |
|         |
G----H----I
[A-B-R, C-A-T, C-G-B, C-M-R, B-Z-T, H-G-L, H-I-R, I-F-T]      Z
     |
     |
A----B
|    |
|    |
C----M----F
|    |    |
|    |    |
G----H----I

Solution 1 - Submitted on Sat Oct 5 22:25:34 2013



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

class PrintNetwork {

    public static void main(String s[]) {
        List connections = new ArrayList();
        connections.add(new Connection('A', 'B', 'B'));
        connections.add(new Connection('C', 'B', 'R'));
        connections.add(new Connection('B', 'D', 'R'));
        connections.add(new Connection('D', 'E', 'R'));
        connections.add(new Connection('D', 'F', 'B'));
        printConnections(connections);
    }

    public static void printConnections(List connections) {
        int size = connections.size();
        char[][] array = new char[2 * size + 3][2 * size + 3];
        array = fillArray(array);
        array = getArray(array, connections);
        print(array);
    }

    //fills the array with space character
    public static char[][] fillArray(char output[][]) {
        for (int i = 0; i < output.length; i++)
            for (int j = 0; j < output[i].length; j++)
                output[i][j] = ' ';
        return output;
    }

    //enters the nodes into the array
    public static char[][] getArray(char output[][], List connections) {
        //enters the initial nodes into the array
        int size = output.length / 2;
        Connection one = connections.get(0);
        output[size][size] = one.start;
        //if direction is right
        if (one.direction == 'R') {
            output[size][size + 1] = one.end;
        }
        //if direction is left
        else if (one.direction == 'L') {
            output[size][size - 1] = one.end;
        }
        //if direction is bottom
        else if (one.direction == 'B') {
            output[size + 1][size] = one.end;
        }
        //if direction is top
        else if (one.direction == 'T') {
            output[size - 1][size] = one.end;
        }
        //enters the remaining nodes
        for (int i = 1; i < connections.size(); i++) {
            Connection c = connections.get(i);
            //if direction is right
            if (c.direction == 'R') {
                for (int a = 0; a < output.length; a++) {
                    for (int b = 0; b < output[a].length; b++) {
                        if (output[a][b] == c.start) {
                            output[a][b + 1] = c.end;
                            break;
                        } else if (output[a][b] == c.end) {
                            output[a][b - 1] = c.start;
                            break;
                        }
                    }
                }
            }
            //if direction is left
            else if (c.direction == 'L') {
                for (int a = 0; a < output.length; a++) {
                    for (int b = 0; b < output[a].length; b++) {
                        if (output[a][b] == c.start) {
                            output[a][b - 1] = c.end;
                            break;
                        } else if (output[a][b] == c.end) {
                            output[a][b + 1] = c.start;
                            break;
                        }
                    }
                }
            }
            //if direction is bottom
            else if (c.direction == 'B') {
                for (int a = 0; a < output.length; a++) {
                    for (int b = 0; b < output[a].length; b++) {
                        if (output[a][b] == c.start) {
                            output[a + 1][b] = c.end;
                            break;
                        } else if (output[a][b] == c.end) {
                            output[a - 1][b] = c.start;
                            break;
                        }
                    }
                }
            }
            //if direction is top
            else if (c.direction == 'T') {
                for (int a = 0; a < output.length; a++) {
                    for (int b = 0; b < output[a].length; b++) {
                        if (output[a][b] == c.start) {
                            output[a - 1][b] = c.end;
                            break;
                        } else if (output[a][b] == c.end) {
                            output[a + 1][b] = c.start;
                            break;
                        }
                    }
                }
            }
        }
        return output;
    }

    //prints according to the output format
    public static void print(char output[][]) {
        int minRow = output.length, maxRow = 0, minCol = output.length, maxCol = 0;
        //finding minRow, maxRow, minCol, maxCol
        for (int a = 0; a < output.length; a++) {
            for (int b = 0; b < output.length; b++) {
                if (output[a][b] != ' ') {
                    if (a < minRow)
                        minRow = a;
                    if (a > maxRow)
                        maxRow = a;
                    if (b < minCol)
                        minCol = b;
                    if (b > maxCol)
                        maxCol = b;
                }
            }
        }
        //prints the required network
        for (int i = minRow; i <= maxRow; i++) {
            for (int j = minCol; j <= maxCol; j++) {
                System.out.print(output[i][j]);
                if (output[i][j] != ' ' && output[i][j + 1] != ' ') {
                    System.out.print("----");
                } else {
                    System.out.print("    ");
                }
            }
            for (int c = 0; c < 2; c++) {
                System.out.println();
                for (int j = minCol; j <= maxCol; j++) {
                    if (output[i][j] != ' ' && output[i + 1][j] != ' ') {
                        System.out.print("|    ");
                    } else {
                        System.out.print("     ");
                    }
                }
            }
            System.out.println();
        }
    }
}

class Connection {

    char start;
    char end;
    char direction;

    public Connection(char start, char end, char direction) {
        this.start = start;
        this.end = end;
        this.direction = direction;
    }

    @Override
    public String toString() {
        return start + "-" + end + "-" + direction;
    }
}

Comments :

© meritcampus 2019

All Rights Reserved.

Open In App