Guidance of PHP multi-choice scoring algorithm

Source: Internet
Author: User
PHP algorithm

3 points for a multi-choice question
The principle is: choose less to 2 points, multi-choice does not give points, choose the wrong not to give points
If: Correct answer 123
Submit an Answer 123 get 3 points
Submit the answer 12,13,23, you can get 2 points
Submit Answer 1234,234,134,124 no points
There's a better way to do it.


Reply to discussion (solution)

According to your description, your multi-choice is not a checkbox but a text input.
And no more than 9 alternative answers.

So I scored to write.
Count (Array_intersect (str_split ($ correct answer), Str_split ($ submit answer));

is: Cut the array separately, then take the intersection

With a checkbox alternative answer 4 1 2 3 4

If you are using a checkbox
Then the result is an array, which intersects with the correct answer array.

How to judge the specific code after the intersection?

$ correct answer = Array (1, 2, 3);
$ Submit Answer = Array (1, 2);
$ score = count (array_intersect ($ correct answer, $ submit answer));
echo $ score; 2

$correct = Array (1, 2, 3);
$submit = Array (1, 2);
$score = Count (Array_intersect ($correct, $submit));
Echo $score; 2


Direct enough!!!

$ correct answer = Array (1, 2, 3);
$ Submit Answer = Array (1, 2);
$ score = count (array_intersect ($ correct answer, $ submit answer));
echo $ score; 2
The intersection and the landlord's topic requirements do not meet, such as the submission of the answer to an array (1,2,4), then the intersection is also (0), the actual should be a point.

Yes, not exactly.
But it is illogical to choose 1,2,4 without scoring. Unless 4 is an anti-option, such as: none
If this is the case, the person who makes the question is deliberately misleading the answer.

If you have to do this, you can write it like this.

$ correct answer = Array (1, 2, 3); $ error Answer = array (4); $ Submit Answer = Array (1, 2, 4); $ score = Array_intersect ($ wrong answer, $ submit answer)? 0:count (array_intersect ($ correct answer, $ submit answer)); echo $ score; 0



Reference 5 Floor xuzuning reply: $ Correct answer = Array (1, 2, 3);
$ Submit Answer = Array (1, 2);
$ score = count (array_intersect ($ correct answer, $ submit answer));
echo $ score; 2
The intersection and the landlord's topic requirements do not meet, such as the submission of the answer to an array (1,2,4), then the intersection is also (0), the actual should be a point.
......

Orthogonal is bad.
The answer is in the back.
There is a difference in the set, do not give points
No intersection, no points (excluding the last after this is considered blank answer case, if the confirmation will not have blank this sentence can not be used)
No difference set, there are intersections, number of intersections! = answer, half
No difference set, there is intersection, the number of intersections = answer, full mark (this can not write logic, directly with else also line, because the exclusion of the above several cases is the intersection of equal)

Attention
1. The above to order judgment, is the else if incompatible logic instead of the switch can tolerate logic, because the province has some judgment conditions
2. Answer questions and answers to ensure that there is no duplicate value, or the number of intersections can be judged problematic

Yes, not exactly.
But it is illogical to choose 1,2,4 without scoring. Unless 4 is an anti-option, such as: none
If this is the case, the person who makes the question is deliberately misleading the answer.

If this is not the case, you can write PHP code?12345$ correct answer = Array (1, 2, 3); $ error Answer = array (4); $ Submit Answer = Array (1, 2, 4); $ score = Array_interse ...

Well, I wrote it wrong.
Intersection Quantity! = Answer/Intersection quantity = Answer
Should be
Intersection Quantity! = Answer/Intersection quantity = Answer

You can use bitwise arithmetic to calculate
A:1 b:2 C:4 D:8
How to choose AB for 3
How to choose ABCD for 15

For example, the correct 4 is the result of the selection of N and 4 ratio
if (n==4) 3 min
If 4&n==n 2 min
Other O-Points



You can use bitwise arithmetic to calculate
A:1 b:2 C:4 D:8
How to choose AB for 3
How to choose ABCD for 15

For example, the correct 4 is the result of the selection of N and 4 ratio
if (n==4) 3 min
If 4&n==n 2 min
Other O-Points

Location is a good choice, select limb <32 (due to the system and PHP version may be as much as 63), very convenient
But it's not very convenient to be bigger.

According to the boss's ideas, you can do this.

$correct = Array (1, 2, 3), $submit = Array (1, 2), $score = count (Array_intersect ($correct, $submit)), if ($score < count ($s Ubmit) {//if 0 points $score = 0 If multiple options are chosen  ;} Echo $score; 2

The landlord #11 the map and the landlord #2 Supplement does not match
Since it was submitted with a checkbox, the submission answer would not be in the form of array ([0] = 12)
Instead, the array ([control name] = = Array ([0] = 1 [2] = 2)) is the form
Otherwise, you need to split the array into arrays before you can use the array function.

That's the answer to every question.

Post your form snippet and I'll explain it to you in detail.



#11的图 I'm dealing with this, each number corresponds to a question.

Yes, your name= "answer66[."
So after commit (assuming all selected) $_post[' Answer66 ' is an array (1, 2, 3, 4)
At this point you also need to process the answer into arrays (1, 2, 3) to apply array functions
For this, I #1 reply has been said

function fen ($a, $b) {//$a answer $b correct answer if ($a = = $b) {return 3;} foreach ($a as $value) {if (!in_array ($value, $b)) {return 0;}} return 2;}

I just think of a stupid method.

Finally, the teacher said that more choice and less choice are not to score--! But I still learn a lot of things.
The code that #11的图 uses is # # But the problem is slow.

Multi-choice and less-choice are not points
Just use the difference set to empty

Orthogonal is bad.
The answer is in the back.
There is a difference in the set, do not give points
No intersection, no points (excluding the last after this is considered blank answer case, if the confirmation will not have blank this sentence can not be used)
No difference set, there are intersections, number of intersections! = answer, half
No difference set, there is intersection, the number of intersections = answer, full mark (this can not write logic, directly with else also line, because the exclusion of the above several cases is the intersection of equal)

Attention
1. The above to order judgment, is the else if incompatible logic instead of the switch can tolerate logic, because the province has some judgment conditions
2. Answer questions and answers to ensure that there is no duplicate value, or the number of intersections can be judged problematic

There is a difference in the set will not give points, if the correct answer is: 1 2 4 The answer is: 1 2
Which of their differences is 4,, according to reason should have half of the points.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.