How do I use brute force to solve and store solutions to multiple choice questions?

928 Views Asked by At

I am having some trouble understanding this brute force approach. I am using Perl to extract questions and their choices. All of the questions right now are being stored inside of an array. I am not sure what to do with the answers.

How should I store the answers and organize my code so it does something similar to this...

  1. Select the first option for the question. (So pass one, all the questions should have option A selected).

  2. Submit to check for correct answers.

  3. Parse answers, if the given answer was correct mark that as "correct" answer and forget trying to select any other choice for that question.

    Otherwise, continue through the list of answers for that question on the next pass.

So the next pass would pick the second answer for the that question, until after multiple submits it finds all of the "correct" answers via brute force.

I am having trouble how to store the answers and associate them with the question, cross them off as they are wrong or tag it "found".

I was thinking about using a hash. Please let me know any suggestions on how I should structure the code.

Thanks!

EDIT

Sample data --

So I am using a HASH method... my hash looks something like this:

print out of the hash :

question_123 => a,b,c,d
question_155 => a,b,c
question_234 => T,F

Now I have to find a way to go through each option until I find the correct answer for that question.

EDIT

To clarify somethings let us assume there is a pool of 10 questions. The user clicks "Begin Practice" which generates 4 random questions from the pool of 10. Thus, at this current state I have four questions with their answers. I go through and add these questions and their answers to a data structure (one from below... or using files to store them). Next I being to pick an answer. Then the user must submit these questions for review. Once the submit button is hit, the prompt says if question x is correct or incorrect. Based on this, the data structure must update which is the correct answer for that question.

Now to rinse and repeat. This time another four set of random questions is generated from that same pool. This time, two new questions are found so these must be added to the data structure. Similar logic should be used to find the answers. Also, each answer option (selection) always has an unique numeric value attached to it so my server can check the answer ID with the value. On my server side each question just has an ID and it's associated right answer. Using my table would defeat the purpose of this experiment.

Visual of what is happening:

Pass One -- 4/10 Random Questions

1. Who is the US president?
21. Barrak
22. Chap
23. Jim
24. Nivea

2. How many states are there?
25. 99
26. 90
27. 51

3. What is the color of the sky?
28. blue
29. black
30. none

4. Is time relative?
31. False
32. True

So, on the first pass the selections should be like the following:

1 => a - 21
2 => a - 99
3 => a - 28
4 => False - 31

Hit the submit button. Server responds with :

1. Correct
2. Incorrect
3. Correct
4. Incorrect

Now update the data structures with correct answer found.

Program now requests new set of questions at the prompt. This time the server returns:

Pass Two -- 4/10 Random Questions

4. Is time relative?
31. False
32. True

6. What is not a plant?
65. Cow
66. Rose
67. Tree

1. Who is the US president?
21. Barrack
22. Chap
23. Jim
24. Nivea

8. What is a programming language?
99. C++
100. Tylenol
101. Mr.Monster

See, now on this pass two new questions arose. For these guys the first options have to be selected but for the repeated ones, the next up should be picked unless the correct answer has already been found.

So this will be sent to the server:

4. True - 32
6. Cow - 65
1. Barrack - 21
8. C++ - 99

The server responds with:

4. Correct
6. Correct
1. Correct
8. Correct

Same deal with the response. Hope this really clears things up. Also note that each answer will always have an unique numerical value attached to it.

3

There are 3 best solutions below

1
On BEST ANSWER

Associate the right answer and all possible options to each question:

my %question = (

    question_123 => {
                      options => [ 'a', 'b', 'c', 'd' ],
                      answer  => 'b',
                    },
    question_155 => {
                      options => [ 'a', 'b', 'c' ],
                      answer  => 'b',
                    },
    question_234 => {
                      options => [ 'T', 'F' ],
                      answer  => 'F',
                    },
);

Then cycle through the options with a simple script:

my %answers;
QUESTION: foreach my $q ( keys %question ) {        # Loop over questions

    for my $option ( @{ $questions{$q}{options} } ) { # Try different options

        $answers{$q} = $option;
        next QUESTION if $option eq $questions{$q}{answer}; # Move on if correct
    }
}

# Print the answers
print "$_ : $answers{$_}\n" foreach sort keys %answers;
0
On

Other answes are OK, but I'll add one...

If you want to represent some "state" or "flags" for probing process, you can simply add more fields to your data structures, like this:

my @questions = (
    {
        text => '2 x 2?',
        answers => [
            {
                text => '2',
                tried => False,
            },
            {
                text => '3',
                tried => True,
                correct => False,
            },
            {
                text => '4',
                tried => False,
            },
        ],
    },
    ...
);

For basics of handling nested data structures in Perl, see code examples in http://perldoc.perl.org/perldsc.html

0
On

I think you need a structure to store complex data. I might suggest something like this.

{ question => 'Of the Marx Brothers who appeared on screen, who is reputed '
            . 'to have evoked resentment for being the funniest offscreen?'
, answers => 
    { A => 'Groucho' 
    , B => 'Chico'
    , C => 'Harpo'
    , D => 'Zeppo'
    }
, answer       => 'D'
, user_guessed => {}
}