Menu
Topics Index
...
`

Weekend Hack - 07 Sep 2013 - Results

Thanks to Zaheed Hussain P 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 07 Sep 2013 - 1 is Zaheed Hussain P. He will get a recharge or gift voucher worth Rs. 150.

To book the seats in the given bus service

Write a program to print a mosquito coil of the given length. If String is passed, every alternate word should be reversed and then printed as a coil. The spaces need to be ignored. Please see the requirements for better understanding.

Input (Object) Printed Output
4  4 3
 1 2
5  5 4 3
   1 2
10  5  4  3
 6  1  2
 7  8  9 10
16  16 15 14 13
  5  4  3 12
  6  1  2 11
  7  8  9 10
100  100  99  98  97  96  95  94  93  92  91
  65  64  63  62  61  60  59  58  57  90
  66  37  36  35  34  33  32  31  56  89
  67  38  17  16  15  14  13  30  55  88
  68  39  18   5   4   3  12  29  54  87
  69  40  19   6   1   2  11  28  53  86
  70  41  20   7   8   9  10  27  52  85
  71  42  21  22  23  24  25  26  51  84
  72  43  44  45  46  47  48  49  50  83
  73  74  75  76  77  78  79  80  81  82
HAPPY VINAYAKA CHATURDHI  T A H C V
 U Y P P I
 R A H A N
 D K A Y A
 H I
MOBEEN IS A SOFTWARE ENGINEER  S O F T W
 E E E B A
 N N M O R
 G S I A E
 I N E E R

Solution 1 - Submitted on Mon Sep 9 05:49:05 2013

class MosquitoCoil {

    public static void main(String s[]) {
        printMosquitoCoil(16);
        System.out.println("\n----------------------------\n");
        printMosquitoCoil("HAPPY VINAYAKA CHATURDHI");
    }

    private static void printMosquitoCoil(Object input) {
        int inputLength = input.toString().length();
        boolean convNeeded = false;
        Double dInput = new Double(0.0);
        String sInput = new String();
        int iInput;
        if (input instanceof String) {
            convNeeded = true;
            // Input as String with odd words reversed
            String[] arrOfDelimtedStrings = input.toString().split("\\s+");
            sInput = arrOfDelimtedStrings[0];
            for (int k = 1; k < arrOfDelimtedStrings.length; k++)
                if (k % 2 != 0)
                    sInput += new StringBuffer(arrOfDelimtedStrings[k]).reverse().toString();
                else
                    sInput += arrOfDelimtedStrings[k];
            inputLength = inputLength - arrOfDelimtedStrings.length + 1;
            dInput = (double) inputLength; // String length is the number of characters to be output
            iInput = inputLength;
            inputLength = 1; // Each input now is a single character
        } else {
            dInput = Double.valueOf(input.toString());
            iInput = dInput.intValue();
        }
        // Find Array Size
        int noOfCols = (int) (Math.ceil(Math.sqrt(dInput)));
        int noOfRows = (int) (Math.ceil(dInput / noOfCols));
        // Create an array and initialize it to ' ' (Single Space)
        int[][] arrayToBeOutput = new int[noOfRows][noOfCols];
        // Where to Start??
        int startRow = noOfRows / 2;
        int startCol = startRow;
        int toBeOutput = 0;
        // Adjustment to startCol when number of columns and rows are equal
        if (noOfCols == noOfRows && noOfCols % 2 == 0)
            startCol--;
        // When to change direction i.e. Decrement Vs Increment ? We need an indicator
        int dirChngIndicator = 1;
        boolean inc = true;
        int zeroForRowAndOneToModifyCol = 1;
        int limitingCounterForRowOrColRepeat = 1;
        int curCounter = 0;
        int tboCopy = 1;
        if (convNeeded)
            toBeOutput = sInput.charAt(tboCopy - 1);
        // Indices are held in a 2-element array
        int[] rowColIndexHolder = new int[]{startRow, startCol};
        if (!convNeeded)
            arrayToBeOutput[rowColIndexHolder[0]][rowColIndexHolder[1]] = tboCopy;
        else
            arrayToBeOutput[rowColIndexHolder[0]][rowColIndexHolder[1]] = toBeOutput;
        tboCopy++;
        while (tboCopy <= iInput) {
            // For number of elements before changing direction
            while (curCounter < limitingCounterForRowOrColRepeat && tboCopy <= iInput) {
                // Go to next index (either row / column)
                if (inc)
                    rowColIndexHolder[zeroForRowAndOneToModifyCol]++;
                else
                    rowColIndexHolder[zeroForRowAndOneToModifyCol]--;
                // Put the element There
                // System.out.println(rowColIndexHolder[0]+", "+rowColIndexHolder[1]);
                if (!convNeeded)
                    arrayToBeOutput[rowColIndexHolder[0]][rowColIndexHolder[1]] = tboCopy;
                else {
                    toBeOutput = sInput.charAt(tboCopy - 1);
                    arrayToBeOutput[rowColIndexHolder[0]][rowColIndexHolder[1]] = toBeOutput;
                }
                tboCopy++;
                curCounter++;
            }
            // Decision on Direction Change 
            dirChngIndicator = (dirChngIndicator + 1) % 2;
            // if decrement / increment is done on both row and column
            if (dirChngIndicator == 0)
                // Toggle Increment / Decrement mechanism
                inc = !inc;
            else
                // Increment the number of elements to be output
                limitingCounterForRowOrColRepeat++;
            // Reset curCounter
            curCounter = 0;
            // Choose Row / Column to be modified
            zeroForRowAndOneToModifyCol = (zeroForRowAndOneToModifyCol == 1) ? 0 : 1;
        }
        // Display output
        if (!convNeeded)
            for (int i = 0; i < noOfRows; i++) {
                for (int j = 0; j < noOfCols; j++) {
                    System.out.print(String.format("%" + (inputLength + 1) + "s", (arrayToBeOutput[i][j] == 0) ? " " : Integer.toString(arrayToBeOutput[i][j])));
                }
                System.out.println();
            }
        else
            for (int i = 0; i < noOfRows; i++) {
                if (i == noOfRows - 1 && noOfRows * noOfCols != iInput)
                    noOfCols = rowColIndexHolder[1] + 1;
                for (int j = 0; j < noOfCols; j++) {
                    System.out.print(String.format("%" + (inputLength + 1) + "s", Character.toString((char) arrayToBeOutput[i][j])));
                }
                System.out.println();
            }
    }
}

Comments :

© meritcampus 2019

All Rights Reserved.

Open In App