Table of Contents
ToggleNote : If you directly copy – paste this solution , you may get any error.
Question 1
Write a Prolog program that takes in two lists, List1 and List2. It should return the first Element of the List2 that is not present in the List1. If all elements of the second list are present In the first list, then nothing should be returned.
% Base case: When the second list is empty, there is no element to check. First_missing([], _, _).
% Case 1: If the first element of the second list is not in the first list, return it.
First_missing([H2|_], List1, H2) :-
\+ member(H2, List1).
% Case 2: If the first element of the second list is in the first list, continue checking the rest of the list.
First_missing([_|T2], List1, Missing) :-
First_missing(T2, List1, Missing).
Question 2
Write a Prolog program that takes in a list of integers and returns the second smallest Element in the list. If the list has less than two distinct elements, the program should not return Anything.
% Base case: When the list has less than two distinct elements, there is no second smallest element.
Second_smallest([], _).
% Case 1: When the first element is smaller than the second element, it is the smallest element.
Second_smallest([X, Y|_], X) :-
X < Y.
% Case 2: When the second element is smaller than the first element, it is the smallest element.
Second_smallest([X, Y|_], Y) :-
Y < X.
% Case 3: When the first and second elements are equal, continue checking the rest of the list.
Second_smallest([X, X|T], Smallest) :-
Second_smallest([X|T], Smallest).
% Case 4: When the first and second elements are distinct, compare the rest of the list to find the second smallest element.
Second_smallest([X, Y|T], Smallest) :-
X \= Y,
(X < Y ->
Second_smallest([X|T], Smallest)
;
Second_smallest([Y|T], Smallest)
).
Question 3
Suppose you are a teacher and want to assign grades to your students based on their performance in the class. You want to assign an “A” grade to students who score an average of 90 or above, a “B” grade to students who score an average of 80 to 89, a “C” grade to students who score an average of 70 to 79, and a “D” grade to students who score an average of 60 to 69. Write a Prolog program that takes in a list of student names and their scores on assignments and exams, and assigns a grade to each student. If a student’s average score falls below 60, the program should not assign a grade to that student.
% Base case: When the list is empty, there are no students to assign grades to.
Assign_grades([], []).
% Recursive case: Assign grades to each student based on their average score.
Assign_grades([Name-Scores|T], [Name-Grade|Grades]) :- Calculate_average(Scores, Average), Assign_grade(Average, Grade),
Assign_grades(T, Grades).
% Calculate the average score for a student. Calculate_average(Scores, Average) :-
Sum_list(Scores, Total),
Length(Scores, Count),
Average is Total / Count.
% Assign grades based on the average score.
Assign_grade(Average, ‘A’) :-
Average >= 90.
Assign_grade(Average, ‘B’) :-
Average >= 80,
Average < 90.
Assign_grade(Average, ‘C’) :-
Average >= 70,
Average < 80.
Assign_grade(Average, ‘D’) :-
Average >= 60,
Average <70
% If the average score falls below 60, no grade is assigned.
Assign_grade(Average, _) :-
Average < 60.
If you find anything wrong in this Solution, feel free to reach us in the comment section.