Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Aim: Write a program to implement Water Jug Problem.
- Source code
- Domains
- A,B=integer
- Predicates
- go
- water(A,B)
- clauses
- go:-
- write("enter capacity of jug A"),
- readint(A),nl,
- A<=4,
- write("enter capacity of jug B"),
- readint(B),nl,
- B<=3,
- write(A," ",B),nl,
- water(A,B).
- water(2,0):-
- write("goal achieved"),nl.
- water(2,_):-
- write("2_0"),nl,
- water(2,0).
- water(0,2):-
- write("2_0"),nl,
- water(2,0).
- water(_,2):-
- write("0_2"),nl,
- water(0,2).
- water(4,2):-
- write("0_2"),nl,
- water(0,2).
- water(3,3):-
- write("4_2"),nl,
- water(4,2).
- water(3,0):-
- write("3_3"),nl,
- water(3,3).
- water(0,3):-
- write("3_0"),nl,
- water(3,0).
- water(4,3):-
- write("0_3"),nl,
- water(0,3).
- Output
- Program 3
- Aim: Write a program to find a disease from given symptoms.
- Source Code
- domians
- A,B,C=string
- predicates
- go
- disease(A,B,C)
- clauses
- go:-
- write("Enter the first symptoms: "),
- readln(A),nl,
- write("Enter the second symptoms: "),
- readln(B),nl,
- write("Enter the third symptoms: "),
- readln(C),
- disease(A,B,C).
- disease("cough","runny nose","fever"):-
- write("common cold"),nl.
- disease("sore throat","cold","congestion"):-
- write("Seasonal Allergy"),nl.
- disease("dizzy","dehydration","hyperthermia"):-
- write("sun stroke"),nl.
- disease("cold","hypothermia","fever"):-
- write("frostbite"),nl.
- Program 4
- Aim: Write a program to implement Monkey Banana Problem.
- Source Code
- domains
- Height,Jump,Stick,Table=integer
- predicates
- go
- attempt(integer,integer,integer,integer,integer)
- task(integer)
- clauses
- go:-
- write("1.monkey jump"),nl,
- readint(Jump),
- write("2.Stick Length"),nl,
- readint(Stick),
- write("3.Height of ceiling"),nl,
- readint(Height),
- write("4.Height of table"),nl,
- readint(Table),
- attempt(_,Height,Jump,Stick,Table),
- readint(_).
- attempt(0,Height,Jump,,):-
- write("monkey jumped to catch banana"),nl,
- Height<=Jump,
- task(1).
- attempt(1,Height,Jump,Stick,_):-
- write("monkey jumped with stick to catch banana"),nl,
- Height<=Jump+Stick,
- task(1).
- attempt(2,Height,Jump,Stick,Table):-
- write("monkey jumped with table having stick to catch banana"),nl,
- Height<=Jump+Stick+Table,
- task(1).
- attempt(3,Height,Jump,_,Table):-
- write("monkey jumped with table to catch banana"),nl,
- Height<=Jump+Table,
- task(1).
- attempt(4,Height,Jump,Stick,Table):-
- write("monkey jumped with table having stick to catch banana"),nl,
- Height>=Jump+Table+Stick,
- write("unsuccessful").
- task(1):-
- write("successful").
- Output
- Program 5
- Aim: Write a program to implement Travelling Salesman Problem.
- Source Code
- domains
- A,B,C,D=symbol
- predicates
- go
- route(symbol,symbol,integer)
- direct(symbol,symbol,integer)
- indirect(symbol,symbol,integer)
- clauses
- route(a,a,0).
- route(a,b,5).
- route(a,c,6).
- route(b,b,0).
- route(c,c,0).
- route(a,d,7).
- route(d,d,0).
- route(b,d,2).
- route(c,d,1).
- go:-
- write("enter first city"),nl,
- readln(A),
- write("enter destination city"),nl,
- readln(B),
- direct(A,B,X),
- indirect(A,B,Y),
- X>Y,
- write("choose indirect path"),nl,
- Y>X,
- write("choose direct path"),nl.
- direct(A,B,X):-
- route(A,B,X),
- write("successfull"),nl.
- indirect(A,B,Y):-
- route(A,R,T),
- route(R,B,Z),
- write("successfull"),
- Y=T+Z.
- Output
- Program 6
- Aim: Write a program to implement Towers of Hanoi Problem.
- Source Code
- domains
- N,J=integer
- FROM,TEMP,TO=char
- predicates
- go
- transfer(integer,char,char,char)
- clauses
- go:-
- write("enter no of disks"),nl,
- readint(N),
- transfer(N,'A','B','C').
- transfer(N,FROM,TO,TEMP):-
- N>0,
- J=N-1,
- transfer(J,FROM,TEMP,TO),
- write("add disk",J,"from",FROM,"to",TO),nl,
- readchar(_),
- transfer(J,TEMP,TO,FROM),
- true.
- transfer(N,FROM,TEMP,TO):-
- true.
- Output
- Program 7
- Aim: Write a program to implement Depth First Search.
- Source Code
- domains
- A,B,X,Y=symbol
- L=symbol*
- predicates
- go
- childnode(X,Y)
- child(X,Y,L)
- path(X,Y,L)
- clauses
- go:-
- write("Enter point: "),
- readln(A),nl,
- write("Exit point: "),
- readln(B),nl,
- childnode(a,b).
- childnode(a,c).
- childnode(c,d).
- childnode(c,e).
- path(A,B,[A|L]):-
- child(A,B,L).
- child(A,B,[B|[]]):-
- childnode(A,B),!.
- child(A,B,[X|L]):-
- childnode(A,X),
- child(X,B,L).
- Output
- AIM: Write a python program to implement hangman game.
- SOURCE CODE
- #importing the time module
- import time
- import random
- #welcoming the user
- name = input("What is your name? ")
- print("Hello, " + name, "Time to play hangman!")
- #wait for 1 second
- time.sleep(1)
- print("Start guessing...")
- time.sleep(0.5)
- # List of words to choose from
- words = ["python", "hangman", "programming", "computer", "science", "algorithm"]
- # Randomly select a word from the list
- word = random.choice(words)
- #creates an variable with an empty value
- guesses = ''
- #determine the number of turns
- turns = 10
- # Create a while loop
- #check if the turns are more than zero
- while turns > 0:
- # make a counter that starts with zero
- failed = 0
- # for every character in secret_word
- for char in word:
- # see if the character is in the players guess
- if char in guesses:
- # print then out the character
- print(char, end=" ")
- else:
- # if not found, print a dash
- print("_", end=" ")
- # and increase the failed counter with one
- failed += 1
- # if failed is equal to zero
- # print You Won
- if failed == 0:
- print("\nYou won")
- break # exit the script
- # ask the user to guess a character
- guess = input("\nguess a character: ").lower()
- # set the players guess to guesses
- guesses += guess
- # if the guess is not found in the secret word
- if guess not in word:
- # turns counter decreases with 1 (now 9)
- turns -= 1
- # print wrong
- print("Wrong")
- # how many turns are left
- print("You have", turns, 'more guesses' )
- # if the turns are equal to zero
- if turns == 0:
- # print "You Lose"
- print("You Lose")
- Write a program for Face Detection using machine learning.
- SOURCE CODE
- import cv2
- import mediapipe as mp
- mp_face_detection = mp.solutions.face_detection
- mp_drawing = mp.solutions.drawing_utils
- # For static images:
- IMAGE_FILES = []
- with mp_face_detection.FaceDetection(
- model_selection=1, min_detection_confidence=0.5) as face_detection:
- for idx, file in enumerate(IMAGE_FILES):
- image = cv2.imread(file)
- # Convert the BGR image to RGB and process it with MediaPipe Face Detection.
- results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
- # Draw face detections of each face.
- if not results.detections:
- continue
- annotated_image = image.copy()
- for detection in results.detections:
- print('Nose tip:')
- print(mp_face_detection.get_key_point(
- detection, mp_face_detection.FaceKeyPoint.NOSE_TIP))
- mp_drawing.draw_detection(annotated_image, detection)
- cv2.imwrite('/tmp/annotated_image' + str(idx) + '.png', annotated_image)
- # For webcam input:
- cap = cv2.VideoCapture(0)
- with mp_face_detection.FaceDetection(
- model_selection=0, min_detection_confidence=0.5) as face_detection:
- while cap.isOpened():
- success, image = cap.read()
- if not success:
- print("Ignoring empty camera frame.")
- continue
- # To improve performance, optionally mark the image as not writeable to
- # pass by reference.
- image.flags.writeable = False
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
- results = face_detection.process(image)
- # Draw the face detection annotations on the image.
- image.flags.writeable = True
- image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
- if results.detections:
- for detection in results.detections:
- mp_drawing.draw_detection(image, detection)
- # Flip the image horizontally for a selfie-view display.
- cv2.imshow('MediaPipe Face Detection', cv2.flip(image, 1))
- if cv2.waitKey(5) & 0xFF == 27:
- break
- cap.release()
- PROGRAM NO – 10
- AIM: Write a program for Text Classification for the given sentence using NTLK Library.
- SOURCE CODE
- import nltk
- from nltk.corpus import stopwords
- from nltk.tokenize import word_tokenize
- from nltk.stem import WordNetLemmatizer
- from nltk import NaiveBayesClassifier
- from nltk.classify import accuracy
- # Sample training data
- training_data = [
- ("I love this sandwich.", "positive"),
- ("This is an amazing place!", "positive"),
- ("I feel very good about these beers.", "positive"),
- ("This is my best work.", "positive"),
- ("What an awesome view", "positive"),
- ("I do not like this restaurant", "negative"),
- ("I am tired of this stuff.", "negative"),
- ("I can't deal with this", "negative"),
- ("He is my sworn enemy!", "negative"),
- ("My boss is horrible.", "negative")
- ]
- # Preprocessing function
- def preprocess(text):
- lemmatizer = WordNetLemmatizer()
- stop_words = set(stopwords.words('english'))
- words = word_tokenize(text.lower())
- filtered_words = [lemmatizer.lemmatize(w) for w in words if w.isalnum() and w not in stop_words]
- return dict([(word, True) for word in filtered_words])
- # Preprocess and label the training data
- processed_training_data = [(preprocess(text), label) for text, label in training_data]
- # Train the Naive Bayes classifier
- classifier = NaiveBayesClassifier.train(processed_training_data)
- # Test sentence
- test_sentence = "This sandwich is not good."
- # Preprocess the test sentence
- processed_test_sentence = preprocess(test_sentence)
- # Classify the test sentence
- classification = classifier.classify(processed_test_sentence)
- print("Test Sentence:", test_sentence)
- print("Classification:", classification)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement