Menu
Topics Index
...
`

Weekend Hack - 02 Mar 2013 - Results

Thanks to Srihari Adelli for spending so much time, coming up with original and innovative answer 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 02 Mar 2013 - 1 is Srihari Adelli. He will get a recharge or gift voucher worth Rs. 150.

To get the best pattern for getting maximum marks in multiple choice questions exam

Write a program to get the best pattern for getting maximum marks in multiple choice questions exam. Assume that the choices for every question are A, B, C and D. Also note that the number of questions in each exam will be exact multiples of four.
There are three types of patterns to choose from.
1. Single means use the same choice for all the questions.
2. Block means dividing the questions into four blocks and use one choice for each block. If there are 16 questions in the exam, first four could be D and second four could be B and so on.
3. Round Robin means using the a pattern repeatedly. For eg. if the chosen pattern is DACB, then the answers will be D, A, C, B, D, A, C, B and so on.

If two patterns give the same number of marks, order of pattern type preference is Single, Block and RoundRobin and order for the patterns is alphabetical.

Input (String) Output (PatternDetails)
AAAAADDDDDCCCCBCBBBB Block - ADCB - 18
Input   = AAAAADDDDDCCCCBCBBBB
Pattern = AAAAADDDDDCCCCCBBBBB
Matches = AAAAADDDDDCCCC**BBBB 
CDBBCADBADBC Round_Robin - ADBC - 6
Input   = CDBBCADBADBC
Pattern = ADBCADBCADBC
Matches = *DB*****ADBC
CCCCCBCBBCAA Single - C - 7
Input    = CCCCCBCBBCAA
Pattern  = CCCCCCCCCCCC
Matches  = CCCCC*C**C**
BACABCACADDBCDBBAAAC Block - BCDA - 9
ABDCDACBABDCDACB Round_Robin - ABDC - 8

Solution 1 - Submitted on Sun Mar 3 22:07:41 2013

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

class UnpreparedStudentBestPattern {

    public static void main(String s[]) {
        String answers = "AAAAADDDDDCCCCBCBBBB";
        System.out.println("The best pattern to get maximum marks is : " + getBestPattern(answers));
    }

    private static PatternDetails getBestPattern(String answers) {
        int previousmarks = 0, marks = 0, count = 0, size = 0;
        boolean flag = true;
        char character = '\0';
        String samplestring = null;
        PatternDetails result = null;
        PatternDetails result1 = null;
        PatternDetails result2 = null;
        PatternDetails result3 = null;
        //single.
        for (char i = 'A'; i <= 'D'; i++) {
            samplestring = concatenationOfString("" + i, answers.length());
            marks = Evaluating(samplestring, answers);
            if (previousmarks < marks || flag) {
                previousmarks = marks;
                character = i;
                flag = false;
            }
        }
        result1 = new PatternDetails(PatternType.Single, "" + character, previousmarks);
        //*blocks
        marks = 0;
        size = answers.length();
        count = 0;
        List list = Stringformation();
        char char1, char2, char3, char4;
        for (String substring : list) {
            char1 = substring.charAt(0);
            char2 = substring.charAt(1);
            char3 = substring.charAt(2);
            char4 = substring.charAt(3);
            samplestring = "" + char1 + char2 + char3 + char4;
            marks = Evaluating(concatenationOfString("" + char1, size / 4) + concatenationOfString("" + char2, size / 4) + concatenationOfString("" + char3, size / 4) + concatenationOfString("" + char4, size / 4), answers);
            if (count == 0)
                result2 = new PatternDetails(null, samplestring, marks);
            result2 = comparing(marks, result2.marks, samplestring, result2.pattern);
            count++;
        }
        result2.patternType = PatternType.Block;
        if (result1.marks > result2.marks)
            result = result1;
        else {
            if (result1.marks == result2.marks)
                result = result1;
            else
                result = result2;
        }
        //*Round_Robin.
        marks = 0;
        count = 0;
        for (String substring : list) {
            marks = Evaluating(concatenationOfString(substring, size / 4), answers);
            if (count == 0)
                result3 = new PatternDetails(null, substring, marks);
            result3 = comparing(marks, result3.marks, substring, result3.pattern);
            count++;
        }
        result3.patternType = PatternType.Round_Robin;
        if (result.marks > result3.marks)
            result = result;
        else {
            if (result.marks == result3.marks)
                result = result1;
            else
                result = result3;
        }
        return result;
    }

    //comparing the marks.And if marks are equal then strings are compared to get the alphabeticalorder.
    public static PatternDetails comparing(int marks, int previousmarks, String substring, String previousstring) {
        PatternDetails result = null;
        if (marks > previousmarks) {
            previousmarks = marks;
            previousstring = substring;
        } else {
            if (marks == previousmarks) {
                if (previousstring.compareTo(substring) > 0)
                    previousstring = substring;
            }
        }
        result = new PatternDetails(null, previousstring, previousmarks);
        return result;
    }//End of the comparing method.

    public static List Stringformation()//This method is used to form the character strings with A,B,C,D.
    {
        List result = new ArrayList();
        for (char char1 = 'A'; char1 <= 'D'; char1++) {
            for (char char2 = 'A'; char2 <= 'D'; char2++) {
                for (char char3 = 'A'; char3 <= 'D'; char3++) {
                    for (char char4 = 'A'; char4 <= 'D'; char4++) {
                        if (char1 != char2 && char1 != char3 && char1 != char4 && char2 != char3 && char2 != char4 && char3 != char4) {
                            result.add("" + char1 + char2 + char3 + char4);
                        }
                    }
                }
            }
        }
        return result;
    }//End of the Stringformation.

    public static int Evaluating(String out, String input)// Evaluating with respect to the input.
    {
        int marks = 0;
        for (int i = 0; i < input.length(); i++) {
            if (out.charAt(i) == input.charAt(i))
                marks++;
        }
        return marks;
    }//End of the Evaluating method.

    public static String concatenationOfString(String input, int size) {
        int i = 0;
        String result = "";
        while (i < size) {
            result += input;
            i++;
        }
        return result;
    } //End of the concatenationOfString method.
}

class PatternDetails {

    PatternType patternType;
    String pattern;
    int marks;

    public PatternDetails(PatternType patternType, String pattern, int marks) {
        this.patternType = patternType;
        this.pattern = pattern;
        this.marks = marks;
    }

    @Override
    public String toString() {
        return patternType + " - " + pattern + " - " + marks;
    }
}

enum PatternType {
    Single(1), Block(2), Round_Robin(3);

    int preference;

    private PatternType(int preference) {
        this.preference = preference;
    }
}

Review Comments

  1. Could have given better names for result, result1, result2, result3, flag.
  2. Comments are good and informative but probably the comment at the end of each method is not needed.
  3. We could create separate methods for checking single, blocks and Round_Robin patterns instead of separating code with comments.

© meritcampus 2019

All Rights Reserved.

Open In App