Php multi-choice question scoring algorithm

Source: Internet
Author: User
Php multi-choice question scoring algorithm guidance PHP algorithm

Multiple choice questions, one question, three points
The principle is: 2 points for less selection, no points for more selection, and no points for the selection
Suppose: The correct answer is 123
Answers submitted: 123 + 3 points
You can score 2 points for answers 12, 13, and 23.
No score for answers submitted: 1234,234,134,124
There are still better ways to achieve this.


Reply to discussion (solution)

According to your description, your multi-choice questions are not input using checkbox but text.
And there will be no more than 9 alternative answers

Therefore, scores can be written.
Count (array_intersect (str_split ($ correct answer), str_split ($ submit answer ));

That is, split them into arrays, and then take the intersection.

Use the checkbox option to answer four questions: 1 1 2 3 4

If you are using checkbox
After submission, an array is obtained, and the intersection with the correct answer array is obtained.

How can I judge the specific code after the intersection is obtained?

$ 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 is not in line with the author's question requirements. for example, if the submitted answer is an array (1, 2, 4), then the intersection is (1, 2). actually, it should be 0 points.

Yes, not exactly.
However, it is unreasonable to choose 1, 2, and 4. Unless 4 is an inverse option, for example: None
In this case, the answer is intentionally misleading.

If you have to write the question like this, you can write it like this.

$ Correct answer = array (1, 2, 3); $ incorrect answer = array (4); $ submitted answer = array (1, 2, 4 ); $ score = array_intersect ($ incorrect answer, $ submit answer )? 0: count (array_intersect ($ correct answer, $ submit answer); echo $ score; // 0



Reference reply from xuzuning on the 5th floor: $ correct answer = array (1, 2, 3 );
$ Submit answer = array (1, 2 );
$ Score = count (array_intersect ($ correct answer, $ submit answer ));
Echo $ score; // 2
The intersection is not in line with the author's question requirements. for example, if the submitted answer is an array (1, 2, 4), then the intersection is (1, 2). actually, it should be 0 points.
......

Just make the merge.
Answer in front, answer in back
Difference set. no score is given.
There is no intersection and no score is given (excluding the blank answer after the previous one. If you confirm that there will be no white-paper answer, you do not need to use it)
No difference set, intersection, number of intersections! = Answer, half a minute
No difference set, intersection exists, number of intersections = answer sheet, full score (this can be done without writing logic and using else directly, because the preceding situations are excluded and the intersection is equal)

Note:
1. the above sequence judgment is performed, because else if incompatible logic is used instead of switch redundancy logic, which saves some judgment conditions.
2. make sure there are no duplicate values for both the answer and answer, otherwise there will be problems in determining the number of intersections

Yes, not exactly.
However, it is unreasonable to choose 1, 2, and 4. Unless 4 is an inverse option, for example: None
In this case, the answer is intentionally misleading.

If this is the case, you can write the PHP code like this? 12345 $ correct answer = array (1, 2, 3); $ incorrect answer = array (4); $ submitted answer = array (1, 2, 4 ); $ score = array_interse ......

Er, I wrote an error.
Intersection quantity! = Answer sheet/intersection quantity = answer sheet
It should be
Intersection quantity! = Answer/intersection quantity = answer

It can be computed using bitwise operations.
A: 1 B: 2 C: 4 D: 8
How to select AB is 3
How to Select ABCD is 15

For example, if the value is 4, check that the selected result is N to 4.
If (N = 4) 3 points
If 4 & N = N 2 points
Other O points



It can be computed using bitwise operations.
A: 1 B: 2 C: 4 D: 8
How to select AB is 3
How to Select ABCD is 15

For example, if the value is 4, check that the selected result is N to 4.
If (N = 4) 3 points
If 4 & N = N 2 points
Other O points

Bit operations are a good choice. you can select <32 limbs (because the number of system and php versions may be as high as 63), which is very convenient.
But it is not convenient if it is bigger.

Follow the boss's ideas to do this.

$ Correct = array (1, 2, 3); $ submit = array (1, 2); $ score = count (array_intersect ($ correct, $ submit )); if ($ score <count ($ submit) {// if you have selected more than 0 points, $ score = 0;} echo $ score; // 2

The texture of the poster #11 is inconsistent with that of the poster #2.
Since it is submitted using checkbox, the submitted answer will not be in the array ([0] => 12) form
Instead, array ([control name] => array ([0] => 1 [2] => 2)
Otherwise, you must split the data into arrays before using the array function.

That is the answer to each question

Paste your form snippet for detailed explanation



# In the figure 11, each number I have processed corresponds to a question.

Yes, your name = "answer66 []"
So after Submission (assuming all selected) $ _ POST ['answer66'] is an array like array (1, 2, 3, 4)
At this point, you also need to process the answer into an array (1, 2, 3) to apply the array function.
For this, I have already replied to #1.

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 ;}

Just think of a stupid method

In the end, the teacher said that no score is given for multiple or fewer choices --! But I still learned a lot.
# The code used in the figure 11 is #9, but this problem is slow down first.

No score is given for multiple or fewer selections.
The set is empty.

Just make the merge.
Answer in front, answer in back
Difference set. no score is given.
There is no intersection and no score is given (excluding the blank answer after the previous one. If you confirm that there will be no white-paper answer, you do not need to use it)
No difference set, intersection, number of intersections! = Answer, half a minute
No difference set, intersection exists, number of intersections = answer sheet, full score (this can be done without writing logic and using else directly, because the preceding situations are excluded and the intersection is equal)

Note:
1. the above sequence judgment is performed, because else if incompatible logic is used instead of switch redundancy logic, which saves some judgment conditions.
2. make sure there are no duplicate values for both the answer and answer, otherwise there will be problems in determining the number of intersections

If there is a difference set, no score is given. if the correct answer is: 1 2 4, the submitted answer is: 1 2.
Which of them has a difference set of 4, which 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.