Menu
Topics Index
...
`

Weekend Hack - 19 Jan 2013 - Results

Thanks to Sulekha Metta, Srihari Adelli and Tejaswini Rao 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 (two of them are only in 2nd year CSE and they have not yet completed the Java course), 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 19 Jan 2013 - 1 is Srihari Adelli and the winner of the Weekend Hack 19 Jan 2013 - 2 is Sulekha Metta. They both will get a recharge of Rs. 150 each. Since the problems were complex and Tejaswini Rao Potlapally solved both of them, she will get a recharge of Rs. 80 (40 + 40) for submitting it correctly.

1. Print all the possible ages of children

Write a program to print all the possible ages given the number of children and product of their ages. The possibilities should be sorted as shown below. Use System.out.print or System.out.println for printing.

Input (Integer, Integer) Printed Output
Number = 1, Product = 6 6
Number = 2, Product = 3 1 3
Number = 3, Product = 24 1 1 24
1 2 12
1 3 8
1 4 6
2 2 6
2 3 4
Number = 3, Product = 12 1 1 12
1 2 6
1 3 4
2 2 3
Number = 2, Product = 72 1 72
2 36
3 24
4 18
6 12
8 9

Solution 1 - Submitted on Sat Jan 19 13:08:37 2013

public class PrintPossibleAgesOfChildren {

    public static void main(String s[]) {
        printPossibleAges(3, 24);
    }

    public static void printPossibleAges(int numberOfChildren, int productOfTheirAges) {
        int n, p;
        n = numberOfChildren;
        p = productOfTheirAges;
        int a[] = new int[20];
        int array[] = new int[20];
        int s = 1;
        int i, j = 0, l = 0;
        for (i = 1; i <= p; i++) {
            if (p % i == 0) {
                a[j] = i;
                j++;
            }
        }
        if (n == 1) {
            System.out.println(a[j - 1]);
            return;
        }
        for (i = 0; i < j - 1; i++) {
            int k = a[i];
            int t = i;
            while (t < j) {
                int k1 = 1;
                while (k1 < n) {
                    s = k * a[t];
                    t++;
                    k1++;
                    if (n > 2) {
                        for (int x = t; x < j; x++) {
                            int pro = s * a[x];
                            if (pro == p) {
                                array[l] = k;
                                array[++l] = a[t - 1];
                                array[++l] = a[x];
                                l++;
                            }
                        }
                    } else {
                        if (s == p) {
                            array[l] = k;
                            array[++l] = a[t - 1];
                            l = l + 1;
                        }
                    }
                }
            }
        }
        for (int m = 0; m < l; m++) {
            if (n == 2) {
                System.out.println(array[m] + " " + array[m + 1]);
                m++;
            }
            if (n == 3) {
                System.out.println(array[m] + " " + array[m + 1] + " " + array[m + 2]);
                m = m + 2;
            }
        }
        return;
    }
}

Review Comments

  1. The solution will not work for number of children greater than 3.
  2. Better variable names could be given to n, p, a, s, k and t
  3. In the lines int a[] = new int[20]; and int array[] = new int[20];, the assumption that the number of possibilities are less than 20 could be wrong.

Solution 2 - Submitted on Mon Jan 21 02:34:51 2013

public class PrintPossibleAgesOfChildren {

    public static void main(String s[]) {
        printPossibleAges(5, 108);
    }

    public static void printPossibleAges(int numberOfChildren, int productOfTheirAges) {
        int arraysample[] = new int[numberOfChildren];
        int size = numberOfChildren, result;
        boolean flag = true;
        int factor[] = getFactors(productOfTheirAges);
        first : for (int i = factor.length - 1; i >= 0; i--) {
            arraysample[--size] = factor[i];
            second : for (int j = 1; j <= numberOfChildren - 1; j++) {
                result = factor[i];
                for (int k = i; k >= 0; k--) {
                    if (result * factor[k] == productOfTheirAges && factor[k] < factor[i] && size > 0) {
                        arraysample[--size] = factor[k];
                        while (size > 0)
                            arraysample[--size] = 1;
                        for (int m = k - 1; m >= 1; m--) {
                            if (productOfTheirAges % (result * factor[m]) == 0) {
                                print(arraysample);
                                result *= factor[m];
                                size = numberOfChildren - 1;
                                arraysample[--size] = factor[m];
                                if (size == 0)
                                    flag = checking(arraysample, productOfTheirAges);
                                if (!flag)
                                    break second;
                            }
                        }
                    } else {
                        if (productOfTheirAges % (result * factor[k]) == 0 && size > 0) {
                            if (productOfTheirAges / (result * factor[k]) > factor[i])
                                break first;
                            arraysample[--size] = factor[k];
                            result *= factor[k];
                            k++;
                            if (size == 0)
                                flag = checking(arraysample, productOfTheirAges);
                            if (!flag)
                                break second;
                        }
                    }
                }// end of the third for loop
            } //end of the second for loop
            if (size == 0 && flag)
                flag = checking(arraysample, productOfTheirAges);
            size = numberOfChildren;
        } //end of the first for loop     
    }

    public static boolean checking(int arraysample[], int productOfTheirAges) {
        int temp = 1;
        for (int n : arraysample)
            temp *= n;
        if (temp != productOfTheirAges)
            return false;
        else
            print(arraysample);
        return true;
    } //end of the checking function. 

    public static void print(int arraysample[]) {
        for (int n : arraysample)
            System.out.print(n + " ");
        System.out.println();
    } // end of the print function. 

    public static int[] getFactors(int number) {
        int factors[] = new int[number];
        int size = 0;
        for (int i = 1; i <= number; i++) {
            if (number % i == 0)
                factors[size++] = i;
        }
        int Factor[] = new int[size];
        System.arraycopy(factors, 0, Factor, 0, size);
        return Factor;
    }// end of the getFactors function.
}

Review Comments

  1. Although the solution is a bit complex, the way the variables are named, the methods are created to simplify things is good.
  2. The usage of the labels for breaking multiple for loops and the way the comments are included is good.
  3. The solution could have been better, if recursive methods are used.
  4. The number of lines in the main method could have been further reduced, by using methods where possible.

2. Find all the pawns which the camel can kill

In chess, assuming that the columns are numbered A to H and the rows are numbered 1 to 8. Write a program to find all the pawns which the camel can kill. Assume that there are only the given pawns on the chess board.
NOTE: The list returned should be first sorted by columns and then by rows, so A4 comes before B1 and C7 comes before D5 etc.,

Input (Position, List Of Positions) Output (Sorted List of Positions)
Camel = C1 pawns = [A3, B2, B1, H6, D3, E3, G3, E2] [A3, B2, E3, H6]
Camel = G1 pawns = [H1, C5, A8, D4, F1, E1, H2] [C5, D4, F1, H2]
Camel = D5 pawns = [A5, B7, C8, C6, A2, B3, H1, G4, F7, D6] [A2, B3, B7, C6, F7, H1]
Camel = B3 pawns = [H7, D5, A2, C2, D3, F7, D7, G8, A6] [A2, C2, D5, F7, G8]

Solution 1 - Submitted on Sat Jan 19 13:01:04 2013

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

public class FindKillablePawnsByCamel {

    public static void main(String s[]) {
        ChessPosition camel = new ChessPosition('C', 1);
        List pawnPositions = new ArrayList();
        pawnPositions.add(new ChessPosition('A', 3));
        pawnPositions.add(new ChessPosition('B', 2));
        pawnPositions.add(new ChessPosition('B', 1));
        pawnPositions.add(new ChessPosition('H', 6));
        pawnPositions.add(new ChessPosition('D', 3));
        pawnPositions.add(new ChessPosition('E', 3));
        pawnPositions.add(new ChessPosition('G', 3));
        pawnPositions.add(new ChessPosition('E', 2));
        System.out.println("The pawn positions which camel can kill are: " + getPossiblePawnPositions(camel, pawnPositions));
    }

    public static List getPossiblePawnPositions(ChessPosition camelPosition, List pawnPositions) {
        if (camelPosition == null || camelPosition.column < 'A' && camelPosition.column > 'E' || camelPosition.row < 1 && camelPosition.row > 8 || pawnPositions.isEmpty())
            return (null);
        List result = new ArrayList();
        Iterator it = pawnPositions.iterator();
        while (it.hasNext()) {
            ChessPosition y = (ChessPosition) it.next();
            if (Math.abs(camelPosition.column - y.column) - Math.abs(camelPosition.row - y.row) == 0)
                result.add(y);
        }
        int k = result.size();
        while (k > 0) {
            Iterator i = result.iterator();
            ChessPosition t = (ChessPosition) result.get(k - 1);
            int f = 0;
            while (i.hasNext() && f < k) {
                f++;
                ChessPosition t1 = (ChessPosition) i.next();
                if (t1.column > t.column) {
                    ChessPosition temp = new ChessPosition(t1.column, t1.row);
                    t1.column = t.column;
                    t1.row = t.row;
                    t.column = temp.column;
                    t.row = temp.row;
                }
            }
            k--;
        }
        return (result);
    }
}

class ChessPosition {

    char column;
    int row;

    public ChessPosition(char column, int row) {
        this.column = column;
        this.row = row;
    }

    @Override
    public boolean equals(Object obj) {
        ChessPosition position = (ChessPosition) obj;
        return column == position.column && row == position.row;
    }

    @Override
    public String toString() {
        return column + "" + row;
    }
}

Review Comments

  1. The solution is simple and easy to understand.
  2. Not clear on what purpose does the first if serve.
  3. Could have used the for-each loop instead of iterators
  4. Better variable names could be given to t, t1, f and k

Solution 2 - Submitted on Sun Jan 20 10:10:51 2013

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

public class FindKillablePawnsByCamel {

    public static void main(String s[]) {
        ChessPosition camel = new ChessPosition('C', 1);
        List pawnPositions = new ArrayList();
        pawnPositions.add(new ChessPosition('A', 3));
        pawnPositions.add(new ChessPosition('B', 2));
        pawnPositions.add(new ChessPosition('B', 1));
        pawnPositions.add(new ChessPosition('H', 6));
        pawnPositions.add(new ChessPosition('D', 3));
        pawnPositions.add(new ChessPosition('E', 3));
        pawnPositions.add(new ChessPosition('G', 3));
        pawnPositions.add(new ChessPosition('E', 2));
        System.out.println("The pawn positions which camel can kill are: " + getPossiblePawnPositions(camel, pawnPositions));
    }

    public static List getPossiblePawnPositions(ChessPosition camelPosition, List pawnPositions) {
        List diagonals = new ArrayList();
        char j;
        int a;
        j = camelPosition.column; /*finding the diagonal positions of camel*/
        --j;
        a = camelPosition.row;
        for (int i = a + 1; i <= 8 && j >= 'A'; i++, j--) /*bottom left diagonal*/
        {
            int r;
            char c;
            r = i;
            c = j;
            diagonals.add(new ChessPosition(c, r));
        }
        j = camelPosition.column;
        --j;
        a = camelPosition.row;
        for (int i = a - 1; i >= 1 && j >= 'A'; i--, j--) /*top left diagonal*/
        {
            int r;
            char c;
            r = i;
            c = j;
            diagonals.add(new ChessPosition(c, r));
        }
        j = camelPosition.column;
        ++j;
        a = camelPosition.row;
        for (int i = a + 1; i <= 8 && j <= 'H'; i++, j++) /*bottom right diagonal*/
        {
            int r;
            char c;
            r = i;
            c = j;
            diagonals.add(new ChessPosition(c, r));
        }
        j = camelPosition.column;
        ++j;
        a = camelPosition.row;
        for (int i = a - 1; i >= 1 && j <= 'H'; i--, j++) /*top right diagonal*/
        {
            int r;
            char c;
            r = i;
            c = j;
            diagonals.add(new ChessPosition(c, r));
        }
        int k = diagonals.size();
        int p = pawnPositions.size();
        List out = new ArrayList(); /*code for finding the positions which camel can kill*/
        for (ChessPosition a1 : pawnPositions) {
            for (ChessPosition b : diagonals) {
                if (b.equals(a1)) {
                    out.add(b);
                }
            }
        }
        String[] s = new String[20];
        for (int index = 0; index < out.size(); index++) /*for arranging elements in array list in ascending order*/
        {
            s[index] = out.get(index).toString();
        }
        for (int c = 0; c < out.size(); c++) {
            for (int d = c; d < out.size(); d++) {
                if (s[c].compareTo(s[d]) > 0) {
                    String t = s[d];
                    s[d] = s[c];
                    s[c] = t;
                }
            }
        }
        List out1 = new ArrayList();
        for (int q = 0; q < out.size(); q++) {
            char ch, ch1;
            ch = s[q].charAt(0);
            ch1 = s[q].charAt(1);
            int value = ch1 - '0';
            out1.add(new ChessPosition(ch, value));
        }
        return out1;
    }
}

class ChessPosition {

    char column;
    int row;

    public ChessPosition(char column, int row) {
        this.column = column;
        this.row = row;
    }

    @Override
    public boolean equals(Object obj) {
        ChessPosition position = (ChessPosition) obj;
        return column == position.column && row == position.row;
    }

    @Override
    public String toString() {
        return column + "" + row;
    }
}

Review Comments

  1. The comments included to explain the code are good.
  2. Lots of duplicate code, which could be avoided using methods.
  3. Could have given better variable names for j, a, r, c, k and p
  4. In the line String[] s = new String[20];, the assumption that the output list size will be less than 20 could be wrong.
  5. Could have used the Comparator interface and Collections.sort for sorting

© meritcampus 2019

All Rights Reserved.

Open In App