Menu
Topics Index
...
`

Weekend Hack - 05 Jan 2013 - Results

Thanks to Rasagna Veeramallu, Aruna Gunda, Sulekha Metta, Deepika Koduri and Drakshayani Alavalapati 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 Jan 2013 is Drakshayani Alavalapati. She will get a recharge or gift voucher worth Rs. 150. Since this is the first weekend hack, we would also like to give a recharge of Rs. 40 for all the others who submitted it correctly.

Solution 1 - Submitted at Sat Jan 5 11:11:48 2013

class FindLoveIndex {

    public static void main(String s[]) {
        char person1[] = {'R', 'o', 'm', 'e', 'o'};
        char person2[] = {'J', 'u', 'l', 'i', 'e', 't'};
        System.out.println("The love index of Romeo and Juliet  : " + getLoveIndex(person1, person2));
    }

    public static double getLoveIndex(char person1[], char person2[]) {
        int p1check[] = new int[person1.length];
        int p2check[] = new int[person2.length];
        for (int i = 0; i < person1.length; i++) {
            if (p1check[i] != 0)
                continue;
            for (int j = 0; j < person2.length; j++) {
                if (p2check[j] == 0) {
                    if (Character.toLowerCase(person1[i]) == Character.toLowerCase(person2[j])) {
                        p1check[i]--;
                        p2check[j]--;
                        break;
                    }
                }
            }
        }
        int p1, p2;
        p1 = p2 = 0;
        if (p1check[0] == 0)
            p1 += person1[0] - 'A' + 1;
        if (p2check[0] == 0)
            p2 += person2[0] - 'A' + 1;
        for (int i = 1; i < p1check.length; i++) {
            if (p1check[i] == 0)
                p1 += person1[i] - 'a' + 1;
        }
        for (int i = 1; i < p2check.length; i++) {
            if (p2check[i] == 0)
                p2 += person2[i] - 'a' + 1;
        }
        double check;
        if (p1 < p2)
            check = (double) p1 * 100 / p2;
        else
            check = (double) p2 * 100 / p1;
        return check;
    }
}

Review Comments

  1. Order of the algorithm assuming N1 characters in first word, N2 characters in second word - O(N1) + O(N2) + O(N1 * N2)
  2. Additional Memory used - (N1 + N2 + 2) integers + 1 double
  3. Will not work if the capitals are present in the name apart from first character.
  4. Could have given better names for p1 and p2.

Solution 2 - Submitted at Sat Jan 5 13:35:10 2013

class FindLoveIndex {

    public static void main(String s[]) {
        char person1[] = {'R', 'o', 'm', 'e', 'o'};
        char person2[] = {'J', 'u', 'l', 'i', 'e', 't'};
        System.out.println("The love index of Romeo and Juliet  : " + getLoveIndex(person1, person2));
    }

    public static double getLoveIndex(char person1[], char person2[]) {
        boolean result[] = new boolean[person2.length];
        int person1num = 0;
        int person2num = 0;
        for (char letter : person1) {
            boolean matchFound = false;
            for (int i = 0; i < person2.length; i++) {
                if (!result[i] && Character.toUpperCase(letter) == Character.toUpperCase(person2[i])) {
                    matchFound = true;
                    result[i] = true;
                    break;
                }
            }
            if (!matchFound) {
                person1num += (Character.toUpperCase(letter) - 'A' + 1);
            }
        }
        for (int i = 0; i < person2.length; i++) {
            if (!result[i]) {
                person2num += (Character.toUpperCase(person2[i]) - 'A' + 1);
            }
        }
        return person1num > person2num ? 100.0 * person2num / person1num : 100.0 * person1num / person2num;
    }
}

Review Comments

  1. Order of the algorithm assuming N1 characters in first word, N2 characters in second word - O(N1 * N2) + O(N2)
  2. Additional Memory used - N2 booleans + 2 integers + 1 double
  3. person1num and person2num could be named as person1count and person2count
  4. Effective use of ternary operator instead of if-else while returning

Solution 3 - Submitted at Sat Jan 5 17:46:21 2013

class FindLoveIndex {

    public static void main(String s[]) {
        char person1[] = {'R', 'o', 'm', 'e', 'o'};
        char person2[] = {'J', 'u', 'l', 'i', 'e', 't'};
        System.out.println("The love index of Romeo and Juliet  : " + getLoveIndex(person1, person2));
    }

    public static double getLoveIndex(char person1[], char person2[]) {
        double sum1 = 0, sum2 = 0;
        int[] xyz = new int[person1.length];
        int[] xyz1 = new int[person2.length];
        for (int i = 0; i < person1.length; i++) {
            xyz[i] = Character.toLowerCase(person1[i]) - 'a' + 1;
        }
        for (int i = 0; i < person2.length; i++) {
            xyz1[i] = Character.toLowerCase(person2[i]) - 'a' + 1;
        }
        for (int i = 0; i < xyz.length; i++) {
            for (int j = 0; j < xyz1.length; j++) {
                if (xyz[i] == xyz1[j]) {
                    xyz[i] = 0;
                    xyz1[j] = 0;
                }
            }
        }
        for (int element : xyz) {
            System.out.println(element);
            sum1 = sum1 + element;
        }
        for (int element : xyz1) {
            System.out.println(element);
            sum2 = sum2 + element;
        }
        double index = 0;
        if (sum1 > sum2) {
            index = (sum2 / sum1) * 100;
        } else {
            index = (sum1 / sum2) * 100;
        }
        return index;
    }
}

Review Comments

  1. Order of the algorithm assuming N1 characters in first word, N2 characters in second word - 2 * O(N1) + 2 * O(N2) + O(N1 * N2).
  2. Additional Memory used - (N1 + N2) integers + 3 doubles
  3. The idea of converting all characters to lower case is good, but only disadvantage is it takes unnecessary memory.
  4. Naming coventions should be followed. Variables like xyz and xyz1 should be avoided.
  5. Before submitting the final code, remove any unwanted System.out.println's
  6. Original code contained lots of commented code. Before submitting the final code, please remove the unwanted comments

Solution 4 - Submitted at Sat Jan 5 23:01:17 2013

class FindLoveIndex {

    public static void main(String s[]) {
        char person1[] = {'R', 'o', 'm', 'e', 'o'};
        char person2[] = {'J', 'u', 'l', 'i', 'e', 't'};
        System.out.println("The love index of Romeo and Juliet  : " + getLoveIndex(person1, person2));
    }

    public static double getLoveIndex(char person1[], char person2[]) {
        int index1[] = new int[26];
        int index2[] = new int[26];
        int s;
        if (person1[0] <= 97) {
            s = person1[0] + 32;
            person1[0] = (char) s;
            s = person2[0] + 32;
            person2[0] = (char) s;
        }
        double t = 0, t1 = 0;
        int k = person1.length;
        int j = person2.length;
        while (k > 0) {
            index1[person1[k - 1] - 96] += 1;
            k--;
        }
        while (j > 0) {
            index2[person2[j - 1] - 96] += 1;
            j--;
        }
        k = 0;
        j = 0;
        int e1 = person1.length;
        int e2 = person2.length;
        while (k < e1) {
            if (index2[person1[k] - 96] > 0) {
                if (index1[person1[k] - 96] > index2[person1[k] - 96]) {
                    index1[person1[k] - 96] = index1[person1[k] - 96] - index2[person1[k] - 96];
                    index2[person1[k] - 96] = 0;
                } else {
                    index2[person1[k] - 96] = index2[person1[k] - 96] - index1[person1[k] - 96];
                    index1[person1[k] - 96] = 0;
                }
            }
            k++;
        }
        int r = 0;
        while (e1 > r) {
            if (index1[person1[r] - 96] != 0) {
                t = t + (person1[r] - 96) * index1[person1[r] - 96];
                index1[person1[r] - 96] = 0;
            }
            r++;
        }
        while (e2 > j) {
            if (index2[person2[j] - 96] != 0) {
                t1 = t1 + (person2[j] - 96) * index2[person2[j] - 96];
                index2[person2[j] - 96] = 0;
            }
            j++;
        }
        double u;
        if (t < t1)
            u = t / t1 * 100;
        else
            u = t1 / t * 100;
        return (u);
    }
}

Review Comments

  1. Order of the algorithm assuming N1 characters in first word, N2 characters in second word - 3 * O(N1) + 2 * O(N2)
  2. Additional Memory used - 57 integers + 3 doubles
  3. Will not work if the capitals are present in the name apart from first character, or if the name contains special characters
  4. The original input array is getting modified, this is harmful since we use the same input for calling some other method later, it will not work since the data changed.
  5. Could have created a variables for 'person1[k] - 96' and 'person2[j] - 96', since it will become easier to read.

Solution 5 - Submitted at Sun Jan 6 23:35:47 2013

class FindLoveIndex {

    public static void main(String s[]) {
        char person1[] = {'R', 'o', 'm', 'e', 'o'};
        char person2[] = {'J', 'u', 'l', 'i', 'e', 't'};
        System.out.println("The love index of Romeo and Juliet  : " + getLoveIndex(person1, person2));
    }

    public static double getLoveIndex(char person1[], char person2[]) {
        /*converting to uppercase so that exact comparision can be done*/
        String result1 = (new String(person1)).toUpperCase();
        String result2 = (new String(person2)).toUpperCase();
        /*variable for holding result*/
        double index = 0;
        /*variables for holding sums of the ranks of remaining characters*/
        int c1 = 0, c2 = 0;
        /*comparing each character of the first name with each char of the second and cancelling by setting the same value with '@'.Purpose of choosing @ for replacing is ASCII value of @ is 64 so that while subtracting it from 64,it is nullified*/
        for (int i = 0; i < result1.length(); i++) {
            for (int j = 0; j < result2.length(); j++) {
                if (result1.charAt(i) == result2.charAt(j)) {
                    result1 = result1.replaceFirst(result1.substring(i, i + 1), "@");
                    result2 = result2.replaceFirst(result2.substring(j, j + 1), "@");
                }
            }
        }
        /*Because the characters are now in uppercase the ASCII values starts from 65.So,subtracting each character's value and 64 gives the rank.Purpose of choosing @ for replacing is ASCII value of @ is 64 so that while subtracting it from 64,it is nullified*/
        for (int i = 0; i < result1.length(); i++) {
            c1 += (result1.charAt(i) - 64);//Getting rank and summing up
        }
        for (int i = 0; i < result2.length(); i++) {
            c2 += (result2.charAt(i) - 64);//Getting rank and summing up
        }
        /*Checking the sums and determining the index based on which is smaller*/
        if (c1 >= c2) {
            index = (double) (c2 * 100) / c1;
        }
        if (c1 < c2) {
            index = (double) (c1 * 100) / c2;
        }
        return index;
    }
}

Review Comments

  1. Order of the algorithm assuming N1 characters in first word, N2 characters in second word - O(N1) + O(N2) + O(N1 * N2) + atleast [4 * (N1+N2)] for the string operations
  2. Additional Memory used - (N1 + N2) characters + 1 double + 2 integers + lots of strings which get created as part of the substring, replaceFirst operations
  3. Comments are very well written. They help in explaining the flow of the program.
  4. Could have used else for (c1 >= c2) instead of putting one more if condition.
  5. It would have been better if we created variables with good names for @ and 64 and use them, than hard coding those variables at multiple places.

© meritcampus 2019

All Rights Reserved.

Open In App