Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

Prolog program to read details about your classmates and display pairs of classmates with highest number of hobbies matching earlier then the rest

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 2640

Write a prolog program to read details about your classmates. One of the
important properties to store with the details of your classmate is the list
of hobbies. Find out how many classmates have similar hobbies. Display pairs
of classmates with highest number of hobbies matching earlier then the rest.

Code for Prolog program to read details about your classmates and display pairs of classmates with highest number of hobbies matching earlier then the rest in Artificial Intelligence

%trace
DOMAINS
   hobbies=string*
   file=xoutput
DATABASE
   studrecord(string,integer,hobbies)
   hobbies_db(hobbies)
   studpair(string,string)
   flag(integer)
   pairs(string,string,integer)
   count(integer)
PREDICATES
   start
   read_hobbies(hobbies,hobbies)
   store_stud_details(integer)
   hobbies
   get_student_hobbies
   print_hobbies(hobbies)
   hobby_distribute
   update_hobbies(string)
   search_hobbies(hobbies,string)
   print_hobbywise_details(hobbies)
   search_stud_with_hobby(string)
   member(string,hobbies)
   pair_students
   count_common_hobby(hobbies,hobbies,integer)
   find_pair(string)
   highest_common
   find_highest_count
CLAUSES
   start:-
       openwrite(xoutput,"Classdet_out.txt"),
       writedevice(xoutput),
       write("\nHow many records do you want to enter? : "),
       readint(NumRec),
       store_stud_details(NumRec).
   
   store_stud_details(TotalRec):-
       TotalRec<>0,
       write("\nEnter details of students...\n"),
       write("Name : "),
       readln(Stud_name),
       write("\nRoll No. : "),
       readint(Stud_rno),
       write("\nEnter your hobbies 1 at a time : 'stop' to end..."),
       read_hobbies([],Hobbies),
       assert(studrecord(Stud_name,Stud_rno,Hobbies)),
       NewTotal=TotalRec-1,
       store_stud_details(NewTotal).
       
   store_stud_details(TempTotal):-
       !,
       TempTotal<>-1,
       write("\n---------------------------------------\n"),
       hobbies.
   
   hobbies:-
       clearwindow,
       write("\nHobbies of each student\n"),
       write("-----------------------------\n"),
       get_student_hobbies,
       write("\nHobby-wise Student Distribution\n"),
       write("------------------------------------"),
       hobby_distribute,
       write("\n\nPairs with total no. of common hobbies"),
       write("\n------------------------------------------"),
       pair_students,
       write("\n\nPair of students with highest common hobbies matching earlier than the rest"),
       write("\n------------------------------------------------------------------------------"),
       highest_common.
   
   get_student_hobbies:-
       studrecord(Name,_,HobbyList),
       write(Name," ==> "),
       print_hobbies(HobbyList),
       fail.
       
   get_student_hobbies.
   
   print_hobbies([Head|HobbyLst]):-
       write(Head,", "),
       print_hobbies(HobbyLst).

   print_hobbies([]):-
       nl.

   hobby_distribute:-
       hobbies_db(Hob),
       print_hobbywise_details(Hob).
       
   hobby_distribute.
   
   print_hobbywise_details([Head|Tail]):-
       write("\nHobby : ",Head,"==> "),
       search_stud_with_hobby(Head),
       print_hobbywise_details(Tail).

   print_hobbywise_details([]).
       
   search_stud_with_hobby(Hobby):-
       studrecord(Name,_,HobList),
       member(Hobby,HobList),
       write(Name,", "),
       fail.
   
   search_stud_with_hobby(Hobby):-
       Hobby=Hobby.    
  
   member(X,[X|_]).
   member(X,[_|Tail]):-
       member(X,Tail).
         
   read_hobbies(Hob,New):- 
       write("\nHobby : "),
       readln(Hobby),
       Hobby<>"stop",
       update_hobbies(Hobby),
       H=[Hobby|Hob],
       read_hobbies(H,New).

   read_hobbies(Hob,Hob).       
   
   update_hobbies(Hobby):-
       hobbies_db(Hob),
       search_hobbies(Hob,Hobby),
       retract(hobbies_db(Hob)),
       NewHob=[Hobby|Hob],
       assert(hobbies_db(NewHob)).
   
   update_hobbies(Hobby):-
       Hobby=Hobby.
   
   search_hobbies([Head|Hob],Hobby):-
       Head<>Hobby,
       search_hobbies(Hob,Hobby).
   search_hobbies([],Hobby):-
       Hobby=Hobby.
       
   pair_students:-
       studrecord(Name,_,_),
       find_pair(Name),
       fail.
   
   pair_students.
   
   find_pair(Name):-
       studrecord(Name,_,Hob1),
       studrecord(Studname,_,Hob2),
       Name<>Studname,
       not(studpair(Name,Studname)),
       not(studpair(Studname,Name)),
       assert(studpair(Name,Studname)),
       Cnt=0,
       write("\nPair : [",Name,"-",Studname,"] : "),
       assert(pairs(Name,Studname,-5)),
       assert(flag(0)),
       count_common_hobby(Hob1,Hob2,Cnt),       
       fail.
   
   find_pair(Name):-
       Name=Name.    
       
   count_common_hobby([Head|Tail],Hobbylst,Cnt):-
       member(Head,Hobbylst),
       Newcnt=Cnt+1,
       count_common_hobby(Tail,Hobbylst,Newcnt).
       
   count_common_hobby([Head|Tail],Hobbylst,Cnt):-
       Head=Head,
       count_common_hobby(Tail,Hobbylst,Cnt).
       
   count_common_hobby([],[Head|_],Cnt):-
       flag(0),
       pairs(X,Y,-5),       
       retract(pairs(X,Y,-5)),
       assert(pairs(X,Y,Cnt)),
       Head=Head,
       write(Cnt," common"),
       retract(flag(0)).    
       
   highest_common:-
       find_highest_count,
       count(Cnt),
       pairs(X,Y,Cnt),
       write("\n[",X,"-",Y,"] : ==> ",Cnt).
   
   find_highest_count:-
       count(Cnt),
       pairs(_,_,No),
       Cnt<No,
       retract(count(Cnt)),
       assert(count(No)),
       find_highest_count.
   
   find_highest_count.
        
          
GOAL
   makewindow(1,2,3,"Student Details",0,0,25,80),
   assert(count(-1)),
   assert(hobbies_db([])),
   start,
   closefile(xoutput),
   writedevice(screen),
   save("rec1.txt").


Output

How many records do you want to enter? : 3
Enter details of students...
Name : Nikhil
Roll No. : 21
Enter your hobbies 1 at a time : 'stop' to end...
Hobby : riding
Hobby : singing
Hobby : poems
Hobby : dancing
Hobby : stop
Enter details of students...
Name : Nitin
Roll No. : 63
Enter your hobbies 1 at a time : 'stop' to end...
Hobby : surfing
Hobby : dancing
Hobby : driving
Hobby : riding
Hobby : talking
Hobby : walking
Hobby : stop
Enter details of students...
Name : Kunal
Roll No. : 61
Enter your hobbies 1 at a time : 'stop' to end...
Hobby : cricket 
Hobby : football 
Hobby : riding
Hobby : swimming
Hobby : stop
---------------------------------------

Hobbies of each student
-----------------------------
Nikhil ==> dancing, poems, singing, riding, 
Nitin ==> walking, talking, riding, driving, dancing, surfing, 
Kunal ==> swimming, riding, football, cricket, 

Hobby-wise Student Distribution
------------------------------------
Hobby : swimming==> Kunal, 
Hobby : football==> Kunal, 
Hobby : cricket==> Kunal, 
Hobby : walking==> Nitin, 
Hobby : talking==> Nitin, 
Hobby : driving==> Nitin, 
Hobby : surfing==> Nitin, 
Hobby : dancing==> Nikhil, Nitin, 
Hobby : poems==> Nikhil, 
Hobby : singing==> Nikhil, 
Hobby : riding==> Nikhil, Nitin, Kunal, 

Pairs with total no. of common hobbies
------------------------------------------
Pair : [Nikhil-Nitin] : 2 common
Pair : [Nikhil-Kunal] : 1 common
Pair : [Nitin-Kunal] : 1 common

Pair of students with highest common hobbies matching earlier than the rest
------------------------------------------------------------------------------
[Nikhil-Nitin] : ==> 2
  
Share: 



Milind Mishra
Milind Mishra author of Prolog program to read details about your classmates and display pairs of classmates with highest number of hobbies matching earlier then the rest is from India.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!