info@meritcampus.com    +91-85006-22255
...
`

# Weekend Hack - 09 Mar 2013 - Results

Thanks to Raghu Ram Bharadwaj Diddigi, Srihari Adelli, Deepika Koduri 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. 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 09 Mar 2013 - 1 is Deepika Koduri. She will get a recharge or gift voucher worth Rs. 125.
The winner of the Weekend Hack 09 Mar 2013 - 2 is Srihari Adelli. He will get a recharge or gift voucher worth Rs. 175.

# To print the characters at the given location in a matrix

Write a program to print the characters at the given location in a matrix. If the point is 5-1-T, we need to print T at the row 5 and the column 1. If it is 4-3-M, we need to print M at row 4 and column 3. Rows and columns are counted starting from 1.

Input (List) Output (String)
`[1-1-M, 2-1-E, 3-1-R, 4-1-I, 5-1-T, 1-5-C, 2-5-A, 3-5-M, 4-5-P, 5-5-U, 6-5-S]` `M---CE---AR---MI---PT---U----S`
`[1-1-M, 1-2-E, 1-3-R, 1-4-I, 1-5-T, 4-1-C, 4-2-A, 4-3-M, 4-4-P, 4-5-U, 4-6-S]` `MERIT-------------CAMPUS`
`[1-1-A, 1-2-Z, 1-3-B, 1-4-Y, 1-5-C, 1-6-X, 2-1-W, 2-2-D, 2-3-V, 2-4-E, 2-5-U, 2-6-F, 3-1-G, 3-2-T, 3-3-H, 3-4-S, 3-5-I, 3-6-R]` `AZBYCXWDVEUFGTHSIR`
`[1-2-M, 5-18-I, 5-9-V, 1-6-B, 1-10-E, 1-12-N, 5-3-K, 3-8-I, 5-6-A, 3-5-A, 5-12-A, 1-8-E, 5-15-L, 1-4-O, 3-1-S, 3-3-H, 3-11-K]` `-M-O-B-E-E-N------------------------S-H-A--I--K---------------------------K--A--V--A--L--I`
`[5-9-S, 11-7-A, 1-7-K, 10-3-B, 11-11-T, 8-10-C, 6-6-M, 4-3-N, 2-2-P, 1-5-U]` `----U-K-----P----------------------N----------------S-------M-------------------------C--------------B--------------A---T`

## Solution 1 - Submitted on Sat Mar 9 10:49:20 2013

``` import java.util.ArrayList;class PrintCharactersInMatrix {    public static void main(String s[]) {        ArrayList points = new ArrayList();        points.add(new Point(1, 1, 'M'));        points.add(new Point(2, 1, 'E'));        points.add(new Point(3, 1, 'R'));        points.add(new Point(4, 1, 'I'));        points.add(new Point(5, 1, 'T'));        points.add(new Point(1, 5, 'C'));        points.add(new Point(2, 5, 'A'));        points.add(new Point(3, 5, 'M'));        points.add(new Point(4, 5, 'P'));        points.add(new Point(5, 5, 'U'));        points.add(new Point(6, 5, 'S'));        printCharacterMatrix(points);    }    public static void printCharacterMatrix(ArrayList points) {        int a = 0, b = 0;        for (int i = 0; i < points.size(); i++) {            if (points.get(i).x > a)                a = points.get(i).x;            if (points.get(i).y > b)                b = points.get(i).y;        }        char[][] arr = new char[a][b];        for (int i = 0; i < a; i++)            for (int j = 0; j < b; j++)                arr[i][j] = '-';        for (int i = 0; i < points.size(); i++)            arr[points.get(i).x - 1][points.get(i).y - 1] = points.get(i).c;        for (int i = 0; i < a; i++) {            for (int j = 0; j < b; j++) {                System.out.print(arr[i][j]);            }            System.out.println();        }    }}class Point {    int x;    int y;    char c;    Point(int x, int y, char c) {        this.x = x;        this.y = y;        this.c = c;    }    @Override    public String toString() {        return x + "-" + y + "-" + c;    }} ```

1. The logic is simple to read and understand.
2. Could have used the for-each loops while iterating through the points.
3. Better names could be given to variables a and b.

## Solution 2 - Submitted on Sat Mar 9 10:49:35 2013

``` import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;public class PrintCharactersInMatrix {    public static void main(String args[]) {        ArrayList points = new ArrayList();        points.add(new Point(1, 1, 'M'));        points.add(new Point(2, 1, 'E'));        points.add(new Point(3, 1, 'R'));        points.add(new Point(4, 1, 'I'));        points.add(new Point(5, 1, 'T'));        points.add(new Point(1, 5, 'C'));        points.add(new Point(2, 5, 'A'));        points.add(new Point(3, 5, 'M'));        points.add(new Point(4, 5, 'P'));        points.add(new Point(5, 5, 'U'));        points.add(new Point(6, 5, 'S'));        printCharacterMatrix(points);    }    public static void printCharacterMatrix(ArrayList points) {        ArrayList listpoints = new ArrayList(points);        class sortrow implements Comparator {            @Override            public int compare(Point a, Point b) {                return b.x - a.x;            }        }        Collections.sort(points, new sortrow());        Point p = points.get(0);        //To get the row size .        int rowSize = p.x;        class sortcolumn implements Comparator {            @Override            public int compare(Point a, Point b) {                return b.y - a.y;            }        }        Collections.sort(points, new sortcolumn());        p = points.get(0); //To get the Column Size.        int columnSize = p.y;        char Array[][] = new char[rowSize][columnSize];        //creating the character Array.        for (Point k : listpoints)            Array[k.x - 1][k.y - 1] = k.c;        for (char[] element : Array) {            for (char element2 : element) {                if (Character.isLetter(element2))                    System.out.print(element2);                else                    System.out.print("-");            }            System.out.println();        }    }}class Point {    int x;    int y;    char c;    Point(int x, int y, char c) {        this.x = x;        this.y = y;        this.c = c;    }    @Override    public String toString() {        return x + "-" + y + "-" + c;    }} ```

1. The logic is easy to understand.
2. The names of the classes like sortrow, sortcolumn should have been SortRow and SortColumn.
3. Variables should not be named starting with capital letters. For eg Array should be array.
4. Probably we could have obtained the max column size and row size simply by using the for loop than by using comparators.

## Solution 3 - Submitted on Sun Mar 10 17:50:35 2013

``` import java.util.ArrayList;public class PrintCharactersInMatrix {    public static void main(String args[]) {        ArrayList points = new ArrayList();        points.add(new Point(1, 1, 'M'));        points.add(new Point(2, 1, 'E'));        points.add(new Point(3, 1, 'R'));        points.add(new Point(4, 1, 'I'));        points.add(new Point(5, 1, 'T'));        points.add(new Point(1, 5, 'C'));        points.add(new Point(2, 5, 'A'));        points.add(new Point(3, 5, 'M'));        points.add(new Point(4, 5, 'P'));        points.add(new Point(5, 5, 'U'));        points.add(new Point(6, 5, 'S'));        printCharacterMatrix(points);    }    public static void printCharacterMatrix(ArrayList points) {        int maxx, maxy;        maxx = maxy = 0;        for (Point p : points) {            if (p.x > maxx)                maxx = p.x;            if (p.y > maxy)                maxy = p.y;        }        char array[][] = new char[maxx][];        for (int i = 0; i < maxx; i++)            array[i] = new char[maxy];        for (int i = 0; i < maxx; i++)            for (int j = 0; j < maxy; j++)                array[i][j] = '-';        for (Point p : points)            array[p.x - 1][p.y - 1] = p.c;        for (int i = 0; i < maxx; i++) {            for (int j = 0; j < maxy; j++)                System.out.print(array[i][j]);            System.out.println();        }    }}class Point {    int x;    int y;    char c;    Point(int x, int y, char c) {        this.x = x;        this.y = y;        this.c = c;    }    @Override    public String toString() {        return x + "-" + y + "-" + c;    }} ```

1. The logic is very clear and simple to understand.
2. Effective use of for-each loops.
3. We could have created the two dimensional statement by passing maxy as the size of the second array. We need not have a separate for loop for creating the arrays again.
4. Arrays.fill can be used to initialize all the elements to '-', than using two for loops.

# Get the relations between family members

Write a program to get the relations between family members.
Given the following family hierarchies.

Family Name Family Hierarchy
Rajeswara Rao's Family `Rajeswara Rao-75M+--Suresh-55M|  +--Mahesh-35M|  +--Lakshmi-32F+--Ramesh-53M|  +--Srinivas-30M|     +--Kamala-8F|     +--Raj-10M`
Ramu's Family `Ramu-60M+--Rama-40M|  +--Krishna-25M|     +--Kumari-10F|     +--Siva-12M+--Ravi-43M|  +--Anand-26M|  +--Kishore-27M|  +--Raghava-29M|  +--Priya-28F+--Rani-42F|  +--Raghu-23M|  +--Ramani-25F|  +--Ramakrishna-24M|     +--Raviteja-3M|     +--Ramya-1F`
Krishna's Family `Krishna-75M+--Sahithi-50F|  +--Anandh-30M|      +--Prem-3M|  +--Rahul-27M|      +--Kavya-1F+--Soundarya-48F|  +--Kavitha-26F|      +--Puja-6F|  +--Kumar-23M+--Mahesh-42M|  +--Roja-23F|  +--Raja-20M+--Sumanth-35M|  +--Sravan-8M`
Ganesh's Family `Ganesh-50M+--Arjun-25M|  +--Ram-4M|  +--Lakshman-4M+--Kalyani-22F|   +--Rupa-1F|   +--Sreekanth-19M|   +--Rajini-19F|   +--Sulochana-19F|   +--Shakti-19M|   +--Lokesh-19M|   +--Vishva-19M|   +--Poorna-19F+--Sirisha-20F`
Charan's Family `Charan-70M+--Sukumar-48M|  +--Priya-24F|  +--Chandrika-22F|  +--Kumar-19M|  +--Mahesh-24M+--Santhosh-45M|  +--Ravi-22M|  +--Raja-20M+--Sakshi-40F|  +--Vennela-20F`
Ramana's Family `Ramana-80M+--Ravi-55M|  +--Santhosh-30M|     +--Sudha-1F|  +--Pavan-20M+--Kumar-53M|  +--Sakshi-25F|  +--Preethi-20F+--Sowjanya-50F|  +--Lavanya-35F|     +--Suma-17F|        +--Chanti-3F`

## Solution 1 - Submitted on Sun Mar 10 15:11:38 2013

``` import java.util.ArrayList;import java.util.List;public class FindFamilyRelations {    public static void main(String args[]) {        // Input as Rajeswara Rao's family as shown in the requirements        Person rajeswaraRao = new Person("Rajeswara Rao", 'M', 75);        Person suresh = new Person("Suresh", 'M', 55);        Person ramesh = new Person("Ramesh", 'M', 53);        rajeswaraRao.children.add(suresh);        rajeswaraRao.children.add(ramesh);        Person mahesh = new Person("Mahesh", 'M', 35);        Person lakshmi = new Person("Lakshmi", 'F', 32);        suresh.children.add(mahesh);        suresh.children.add(lakshmi);        Person srinivas = new Person("Srinivas", 'M', 30);        ramesh.children.add(srinivas);        Person kamala = new Person("Kamala", 'F', 8);        Person raj = new Person("Raj", 'M', 10);        srinivas.children.add(kamala);        srinivas.children.add(raj);        System.out.println("Lakshmi's parent is " + getParent(rajeswaraRao, "Lakshmi"));        System.out.println("Youngest child in family is " + getYoungestChild(rajeswaraRao));        System.out.println("Grandparent of Kamala is " + getGrandParent(rajeswaraRao, "Kamala"));        System.out.println("The parent who has maximum number of children is " + getParentWithMaximumChildren(rajeswaraRao));        System.out.println("The parent who has maximum sum of ages of the children is " + getParentWithMaximumSumOfChildrenAges(rajeswaraRao));    }    private static Person getYoungestChild(Person root) {        if (root == null)            return null;        return helper(root, root, 1);    }    public static Person getParent(Person root, String name) {        if (root == null || name.equals(root.name) || name == null)            return null;        for (Person p : root.children)            if (p.name.equals(name))                return root;        for (Person p : root.children)            if (getParent(p, name) != null)                return getParent(p, name);        return null;    }    private static Person getParentWithMaximumChildren(Person root) {        if (root == null)            return null;        return helper(root, root, 2);    }    private static Person getParentWithMaximumSumOfChildrenAges(Person root) {        if (root == null)            return null;        return helper(root, root, 3);    }    public static Person getGrandParent(Person root, String name) {        if (root == null || name == null)            return null;        Person parent;        parent = getParent(root, name);        return parent == null ? null : getParent(root, parent.name);    }    public static Person helper(Person result, Person root, int ch) {        switch (ch) {            case 1 :                if (root.age < result.age)                    result = root;                break;            case 2 :                if (root.children.size() > result.children.size())                    result = root;                break;            case 3 :                int sum = 0,                sumr = 0;                for (Person p1 : root.children)                    sum += p1.age;                for (Person p2 : result.children)                    sumr += p2.age;                if (sum > sumr)                    result = root;                break;        }        if (root.children != null)            for (Person p : root.children)                result = helper(result, p, ch);        return result;    }}class Person {    String name;    char gender;    int age;    List children = new ArrayList();    public Person(String name, char gender, int age) {        this.name = name;        this.gender = gender;        this.age = age;    }    @Override    public String toString() {        return name + "-" + age + gender;    }} ```

1. The methods are clearly separated depending upon the responsibility.
2. The method helper is efficient, but using codes like 1, 2, 3 will be maintenance nightmare. Since the helper method does too many things, there are high risks of failure.
3. The coding conventions are properly followed and the implemented null checks are helpful.
4. Even if only one statement is present in the body of for loop or if condition, it is better to enclose them in flower brackets.

## Solution 2 - Submitted on Sun Mar 10 24:04:11 2013

``` import java.util.ArrayList;import java.util.List;public class FindFamilyRelations {    public static void main(String args[]) {        // Input as Rajeswara Rao's family as shown in the requirements        Person rajeswaraRao = new Person("Rajeswara Rao", 'M', 75);        Person suresh = new Person("Suresh", 'M', 55);        Person ramesh = new Person("Ramesh", 'M', 53);        rajeswaraRao.children.add(suresh);        rajeswaraRao.children.add(ramesh);        Person mahesh = new Person("Mahesh", 'M', 35);        Person lakshmi = new Person("Lakshmi", 'F', 32);        suresh.children.add(mahesh);        suresh.children.add(lakshmi);        Person srinivas = new Person("Srinivas", 'M', 30);        ramesh.children.add(srinivas);        Person kamala = new Person("Kamala", 'F', 8);        Person raj = new Person("Raj", 'M', 10);        srinivas.children.add(kamala);        srinivas.children.add(raj);        System.out.println("Lakshmi's parent is " + getParent(rajeswaraRao, "Lakshmi"));        System.out.println("Youngest child in family is " + getYoungestChild(rajeswaraRao));        System.out.println("Grandparent of Kamala is " + getGrandParent(rajeswaraRao, "Kamala"));        System.out.println("The parent who has maximum number of children is " + getParentWithMaximumChildren(rajeswaraRao));        System.out.println("The parent who has maximum sum of ages of the children is " + getParentWithMaximumSumOfChildrenAges(rajeswaraRao));    }    private static Person getYoungestChild(Person root) {        List listOfFamily = new ArrayList();        listOfFamily.add(root);        getTheFamilyMemebers(root, listOfFamily);        Person child = getMinimumAge(listOfFamily);        return child;    }    public static Person getParent(Person root, String name) {        List ListOfFamily = new ArrayList();        ListOfFamily.add(root);        getTheFamilyMemebers(root, ListOfFamily);        Person child = null;        Person parent = null;        ListOfFamily.add(root);        child = getTheperson(ListOfFamily, name);        for (Person sampleparent : ListOfFamily) {            if (sampleparent.children.contains(child))                parent = sampleparent;        }        return parent;    }    private static Person getParentWithMaximumChildren(Person root) {        List listOfParents = new ArrayList();        getlistofparent(root, listOfParents);        Person maxchildrenParent = null;        int maximumChildren = 0;        for (Person parent : listOfParents) {            if (parent.children.size() > maximumChildren) {                maximumChildren = parent.children.size();                maxchildrenParent = parent;            }        }        return maxchildrenParent;    }    private static Person getParentWithMaximumSumOfChildrenAges(Person root) {        List listOfParents = new ArrayList();        getlistofparent(root, listOfParents);        Person maxSumChildrenAgeParent = null;        int maximumAge = 0;        for (Person parent : listOfParents) {            if (maximumAge < getSumOfages(parent)) {                maximumAge = getSumOfages(parent);                maxSumChildrenAgeParent = parent;            }        }        return maxSumChildrenAgeParent;    }    public static Person getGrandParent(Person root, String name) {        List listOfFamily = new ArrayList();        listOfFamily.add(root);        getTheFamilyMemebers(root, listOfFamily);        Person grandparent = null;        Person child = getTheperson(listOfFamily, name);        if (listOfFamily.contains(child)) {            Person parent = FindingTheParentOfChild(listOfFamily, child);            if (listOfFamily.contains(parent))                grandparent = FindingTheParentOfChild(listOfFamily, parent);        }        return grandparent;    }    public static Person getTheperson(List ListOfFamily, String name) {        Person child = null;        for (Person person : ListOfFamily) {            if (name.equals(person.name))                child = person;        }        return child;    }    public static int getSumOfages(Person parent) {        int sumAge = 0;        for (Person child : parent.children)            sumAge += child.age;        return sumAge;    }    public static Person FindingTheParentOfChild(List listOfFamily, Person child) {        Person result = null;        for (Person parent : listOfFamily) {            if (parent.children.contains(child)) {                result = parent;                break;            }        }        return result;    }    public static void getTheFamilyMemebers(Person root, List listOfFamily) {        if (!root.children.isEmpty()) {            for (Person child : root.children)                listOfFamily.add(child);            for (Person parent : root.children)                getTheFamilyMemebers(parent, listOfFamily);        }    }    public static void getlistofparent(Person root, List listOfFamily) {        if (!root.children.isEmpty()) {            listOfFamily.add(root);            for (Person parent : root.children)                getlistofparent(parent, listOfFamily);        }    }    public static Person getMinimumAge(List listOfFamily) {        Person child = null;        boolean flag = true;        int minimumAge = 0;        for (Person parent : listOfFamily) {            if (minimumAge > parent.age || flag) {                flag = false;                minimumAge = parent.age;                child = parent;            }        }        return child;    }}class Person {    String name;    char gender;    int age;    List children = new ArrayList();    public Person(String name, char gender, int age) {        this.name = name;        this.gender = gender;        this.age = age;    }    @Override    public String toString() {        return name + "-" + age + gender;    }} ```

1. The methods are clearly separated depending upon the responsibilty.
2. There is some duplicate code while getting all the family members. We could have created a method for doing it.
3. Collections.min could have been used to identify the person with minimum age.
4. The method names could have been : getTheperson -> getPerson, getSumOfages -> getSumOfAges, FindingTheParentOfChild -> findParentOfChild, getTheFamilyMemebers -> addFamilyMembers
5. getGrandParent method could have been improved by calling getParent twice.

## Solution 3 - Submitted on Mon Mar 11 08:01:01 2013

``` import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class FindFamilyRelations {    public static void main(String args[]) {        // Input as Rajeswara Rao's family as shown in the requirements        Person rajeswaraRao = new Person("Rajeswara Rao", 'M', 75);        Person suresh = new Person("Suresh", 'M', 55);        Person ramesh = new Person("Ramesh", 'M', 53);        rajeswaraRao.children.add(suresh);        rajeswaraRao.children.add(ramesh);        Person mahesh = new Person("Mahesh", 'M', 35);        Person lakshmi = new Person("Lakshmi", 'F', 32);        suresh.children.add(mahesh);        suresh.children.add(lakshmi);        Person srinivas = new Person("Srinivas", 'M', 30);        ramesh.children.add(srinivas);        Person kamala = new Person("Kamala", 'F', 8);        Person raj = new Person("Raj", 'M', 10);        srinivas.children.add(kamala);        srinivas.children.add(raj);        System.out.println("Lakshmi's parent is " + getParent(rajeswaraRao, "Lakshmi"));        System.out.println("Youngest child in family is " + getYoungestChild(rajeswaraRao));        System.out.println("Grandparent of Kamala is " + getGrandParent(rajeswaraRao, "Kamala"));        System.out.println("The parent who has maximum number of children is " + getParentWithMaximumChildren(rajeswaraRao));        System.out.println("The parent who has maximum sum of ages of the children is " + getParentWithMaximumSumOfChildrenAges(rajeswaraRao));    }    private static Person getYoungestChild(Person root) {        List<p> list = new ArrayList<p>();        findyoung(root, list);        Collections.sort(list, new Comparator<p>() {            public int compare(p o1, p o2) {                return (o1.size - o2.size);            }        });        return list.get(0).p1;    }    public static Person getParent(Person root, String name) {        for (Person temp : root.children)            if (temp.name.equals(name))                return root;        if (root.children.size() > 0)            for (Person temp1 : root.children) {                Person p = getParent(temp1, name);                if (p != null)                    return p;            }        return null;    }    private static Person getParentWithMaximumChildren(Person root) {        List<p> list = new ArrayList<p>();        find(root, list);        Collections.sort(list, new Comparator<p>() {            public int compare(p o1, p o2) {                return (o2.size - o1.size);            }        });        return list.get(0).p1;    }    private static Person getParentWithMaximumSumOfChildrenAges(Person root) {        List<p> list = new ArrayList<p>();        findage(root, list);        Collections.sort(list, new Comparator<p>() {            public int compare(p o1, p o2) {                return (o2.size - o1.size);            }        });        return list.get(0).p1;    }    public static Person getGrandParent(Person root, String name) {        Person parent = getParent(root, name);        if (parent != null)            parent = getParent(root, parent.name);        return parent;    }    private static void find(Person root, List<p> list) {        list.add(new p(root, root.children.size()));        if (root.children != null)            for (Person temp : root.children)                find(temp, list);    }    private static void findage(Person root, List<p> list) {        int age = 0;        if (root.children != null) {            for (Person temp : root.children)                age += temp.age;            list.add(new p(root, age));            for (Person temp : root.children)                findage(temp, list);        }    }    private static void findyoung(Person root, List<p> list) {        list.add(new p(root, root.age));        if (root.children != null)            for (Person temp : root.children)                findyoung(temp, list);    }}class p {    Person p1;    int size;    public p(Person p1, int size) {        this.p1 = p1;        this.size = size;    }}class Person {    String name;    char gender;    int age;    List children = new ArrayList();    public Person(String name, char gender, int age) {        this.name = name;        this.gender = gender;        this.age = age;    }    @Override    public String toString() {        return name + "-" + age + gender;    }} ```