QCM in prolog

i am totally a beginner in prolog please help me

This is a prolog problem that I have to solve. I can’t seem to find a starting point.

In a MCQ test where:

each question has 4 choices [a,b,c,d]
each question has only one correct answer (choice)
there are 10 questions
all questions have the same grade value (1 point, totalling 10 points)

4 students have taken this test and we have their grades:

student1: [b, c, b, a, c, c, c, d, c, c] Grade: 7/10
student2: [b, d, c, a, d, d, c, c, a, b] Grade: 6/10
student3: [d, a, b, b, d, d, c, d, a, b] Grade: 5/10
student4: [c, d, c, b, d, b, b, c, a, a] Grade: 3/10

From the informations above I need to write a prolog script that can determine the set of questions that are correct to get a 10/10 grade

please guys i really need your help as soon as you can

The starting point for most programming problems you get in school is: forget for a moment about the language, and figure out the logic of the problem and the solution.

Once you have written down the solution as a mathematical formula, in pseudo-code, or in free text, you start thinking about translating it to a programming language so that you can solve it on a computer.

In the third step you read about the language and attempt to implement the smallest and easiest parts of your solution. As you gain experience and confidence, you combine the little bits (fixing them if necessary) into bigger pieces, until you have a complete implementation.

Finally, you need to convince yourself that your solution works. This means running it on a real computer and checking the results. If you are very lucky or just unnaturally good, it works right away and the results are exactly as you expect. In practice, this is never the case.

You will struggle to run your code, your results will be off, the computer will bug out in ways you’ve never seen before. You will move, in rapid succession, through denial, anger, bargaining, depression and hopefully acceptance that you will have to go back to the starting point.

This is where it finally gets interesting. In the Dark Ages of Computing, when many of us did not have access to the Internet or did not know how to use it, going back to the starting point was the beginning of a journey that rarely takes 10 years but still feels like an Odyssey. “[…] A long way from the baroque community of revelers, the user is closed within the loneliness of his own inner torment.” You find yourself in a world in which the computer “allows free interpretation of scripture, demands difficult personal decisions, imposes a subtle hermeneutics upon the user, and takes for granted the idea that not all can reach salvation.” citation

This is of course the past; today, once you have iterated through the four steps a few times, you have the option of asking online. How do you do that?

  • You take a look at what you have and isolate the most glaring inconsistency between what you know must happen and what you observe. You make sure you can consistently reproduce this inconsistency.
  • You describe the inconsistency you have observed so that others can reproduce it on their computer; an MCVE, in the parlance of our time.
  • You post the MCVE you have prepared on a forum, Stackoverflow, mailing list, discourse, and so on, and hope that someone pays enough attention. Keep in mind that most people are not themselves on the Internet, so be prepared for all kinds of questions and even criticism.

But please keep in mind this is just a starting point.

2 Likes

The answer is:

[b, d, b, a, d, c, c, d, a, c]

A simple backtracking solution took 0.4 seconds of CPU time on my computer, so no need to try anything fancy.

Hints:

Arithmetic is done with is/2.

There are 16 possible combinations for giving a single answer’s grade.

foldl is a generic pattern that can be used to compute the grade for a list of answers. Or you could use maplist/4 and sum_list/2.

A couple more hints.

It’s often easier to write a predicate that verifies a result than one that computes a result. A small modification can often change a predicate that verifies into one that computes (sometimes, no change is needed; sometimes it’s much more difficult to write a predicate that computes than one that checks).

There are many ways the data can be represented. OP’s representation [b,d,b,a,d,c,c,d,a,c] makes both the student and the question number implicit. It’s also possible to be explicit, e.g., by a predicate that lists facts:

master(1, b). % the answer for question #1 is b
master(2, d).
  % ... etc.
student1(1, b). % student1' answer to question #1 is b

Or:

answer(master, 1, b).    % the master answer for question #1 is b.
answer(student4, 10, a). % student4's answer for question #10 is a.

There are also many ways to enumerate facts.

possible_answer(a).
possible_answer(b).
possible_answer(c).
possible_answer(d).

or:

possible_answer(Answer) :- member(Answer, [a,b,c,d]).

And the grade for a single question can be computed in many ways. In general, you want “If the student’s answer for a question equals the master answer for the same question, then the grade is 1, else 0.” So, if you’re using the answer/3 predicate above, you can compute this by:

grade(Student, QuestionNumber, Grade) :-
    answer(Student, QuestionNumber, StudentAnswer),
    answer(master, QuestionNumber, MasterAnswer),
    answer_check(StudentAnswer, MasterAnswer, Grade).
%  answer_check/3 has Grade=1 if both the StudentAnswer and MasterAnswer are the same, and 0 if they're different.
```[spoiler]This text will be blurred[/spoiler]

A word of warning: you are entering a minefield. I learned this the hard way. Some of the issues:

  • OP might get into trouble for getting that kind of help. It depends on the policies of their educational institution. This is why those who post that kind of questions try to delete their posts, and then it gets even weirder if someone resurrects the post. When that happens, this person is punished for their stupidity, but this leaves a bitter taste in my mouth :frowning:
  • Often the exercise comes with strings attached (that we are not told about), like, “you cannot use any library predicates”, sometimes to an absurd degree. Like, “member/2 is not allowed”. So, in an attempt to be consistent and help, one ends up typing up the definition of member/2 or explaining the meaning of L = [H|T] and so on.
  • Which brings me to my next point: I am not sure if this is helping. This person has a teacher (professor/instructor/…) and if they indeed need that kind of help, there is a failure somewhere, and we are only seeing the reflection of it. So how do you meaningfully help? I still haven’t found an answer to that question that satisfies me.

I would like to wrap this up, even though there are many other things to be said.

PS: This is not an attempt to tell you what to post and not to post. This is just me ranting :frowning:

Yes, I realize that … I’m still trying to figure out how to give a hint without revealing the answer (or, at least, my answer). On the other hand, it seems that there are some courses that don’t do a good job of teaching Prolog, and the students are left to flail around – at least, that’s the impression I get from reading some of their questions.

As for this particular problem – at first, I thought it was too difficult for a beginner, but when I tried programming it, I realized that it could easily be done in 10 minutes. However, the solution required knowing how to iterate over a list and had some mildly interesting representation issues. As you say, a suitable response requires knowing what the instructor has said, and I shouldn’t presume. My apologies.

By using the options under the gear image in the menu for each edit.

  1. Use a Blur Spoiler

This text will be blurred

  1. Use a Hide Details
Details

This text will be hidden

This way the answer is there for anyone who wants or needs it. Also it puts the responsibility on the reader, they have to knowingly click on it to see it.

Does this also apply when receiving the discussion list by email?

I can’t find anything about blurs and emails that show examples on the Discourse site, and I don’t know how to force an email with this post to may e-mail account.

My guess is that like other e-mail, the user has the option to see HTML in which case I take it the text will be blurred, or if it is straight text, then it will be readable.

At this point the best I can do is to wait for an e-mail to arrive in a few days in one of my accounts and take a look.

I checked my e-mail on this topic. So far it only has the first post and then gives me a link to click on to see the rest of this topic.