[go: up one dir, main page]

0% found this document useful (0 votes)
43 views6 pages

Ai7 8

This document contains code and instructions for three Python programs: 1) A water jug problem program that finds the steps to transfer a given amount of water between two jugs. 2) A Breadth-first Search program to find the path between two nodes in a graph. 3) A basic chatbot program that can discuss movie genres and say goodbye.

Uploaded by

ankit pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

Ai7 8

This document contains code and instructions for three Python programs: 1) A water jug problem program that finds the steps to transfer a given amount of water between two jugs. 2) A Breadth-first Search program to find the path between two nodes in a graph. 3) A basic chatbot program that can discuss movie genres and say goodbye.

Uploaded by

ankit pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Faculty of Engineering & Technology

Subject Name:- Artificial Intelligence Lab.


Subject Code :- 203105323
B. Tech. IT Year :- 3rd, Semester :- 6th

PRACTICAL:-8

AIM : Write a python program to implement water jug problem.

CODE:
from collections import defaultdict

A = int(input("Enter litre of water in jug1:"))


B = int(input("Enter litre of water in jug2:"))
C = int(input("Enter amount aim:"))

visited = defaultdict(lambda: False)


def waterjugprolem(capacity1, capacity2):

if (capacity1 == C and capacity2 == 0) or (capacity2 == C and capacity1== 0):

print(capacity1, capacity2)
return True
if visited[(capacity1, capacity2)] == False:
print(capacity1, capacity2)
visited[(capacity1, capacity2)] = True

return (waterjugprolem(0, capacity2) or


waterjugprolem(capacity1, 0) or
waterjugprolem(A, capacity2) or
waterjugprolem(capacity1, B) or
waterjugprolem(capacity1 + min(capacity2, (A-capacity1)),

Enrollment No.- 200303108159 Page |


Faculty of Engineering & Technology
Subject Name:- Artificial Intelligence Lab.
Subject Code :- 203105323
B. Tech. IT Year :- 3rd, Semester :- 6th

capacity2 - min(capacity2, (A-capacity1))) or


waterjugprolem(capacity1 - min(capacity1, (B-capacity2)),
capacity2 + min(capacity1, (B-capacity2))))
else:
return False
print("Steps for tranferring water from Jug1 to Jug2: ")

waterjugprolem(0, 0)

OUTPUT :

Enrollment No.- 200303108159 Page |


Faculty of Engineering & Technology
Subject Name:- Artificial Intelligence Lab.
Subject Code :- 203105323
B. Tech. IT Year :- 3rd, Semester :- 6th

PRACTICAL:-7

AIM : Write a python program to implement Breadth first Search(BFS).

CODE:
def createGraph(graph):
n = int(input("Enter no. nodes required:-"))
for _ in range(n):
node = input("enter nodes and connected nodes in following format:
a:b,c(here b and c are connected nodes)").split(":")
graph[node[0]] = node[1].split(",")
return graph

def bfs(graph,start,dest):
result = ["Path not found",list()]
visited = list()
queue = list()
queue.append(start)
visited.append(start)
while queue:
currentNode = queue.pop(0)
if(currentNode not in graph.keys() ):
continue
for node in graph[currentNode]:
if( node not in graph.keys() ):
continue

Enrollment No.- 200303108159 Page |


Faculty of Engineering & Technology
Subject Name:- Artificial Intelligence Lab.
Subject Code :- 203105323
B. Tech. IT Year :- 3rd, Semester :- 6th

if(node==dest):
result[0] = "Reachable"
break
if(node not in visited):
visited.append(node)
queue.append(node)
result[1] = visited
return result
graph = dict()
graph = createGraph(graph)
start = input("point to start from:-")
end = input("point to reach upon:-")
result = bfs(graph,start,end)
print("Founded path is as follows",result[0])
print("Path traversal" ,result[1])

OUTPUT :

Enrollment No.- 200303108159 Page |


Faculty of Engineering & Technology
Subject Name:- Artificial Intelligence Lab.
Subject Code :- 203105323
B. Tech. IT Year :- 3rd, Semester :- 6th

PRACTICAL:-3

AIM : Write a python program to implement simple Chatbot?

CODE:
def chatbot():
print("\nwelcome to chat box. How may I help You?")

lst=["\nMovies","superhero,drama,thriller,comedy"]

while True:
user_input = input("\nYou: ")

if "movies" in user_input.lower():
print("\nChatbot: Sure, what kind of movie do you like to watch?")
a = input("\nYou: ")
print("\nWhat do you like about" + a + "?")

elif "have you watched titanic" in user_input.lower():


print("\nChatbot: ?")
elif user_input.lower() in lst:
print("\nYOU:",lst[0])
elif "bye" in user_input.lower():
print("\nChatbot: have a great day.")
break
else:
print("\nChatbot: Sorry, I didn't understand what you meant?")

Enrollment No.- 200303108159 Page |


Faculty of Engineering & Technology
Subject Name:- Artificial Intelligence Lab.
Subject Code :- 203105323
B. Tech. IT Year :- 3rd, Semester :- 6th

chatbot()
OUTPUT :

Enrollment No.- 200303108159 Page |

You might also like