Docsity

Artificial intelligence documents and lab manual,, Lab Reports of Artificial Intelligence

Toggle sidebar
bg1
AI Lab Manual
Course Code: 15CS3111
ARTIFICIAL INTELLIGENCE
LAB MANUAL
[ACADEMIC YEAR: 2018-19]
SEMESTER - I
COURSE NAME: ARTIFICIAL INTELLIGENCE
COURSE CODE: 15CS3111
YEAR: III/IV B. TECH
DEPT: COMPUTER SCIENCE & ENGINEERING
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Artificial intelligence documents and lab manual, and more Lab Reports Artificial Intelligence in PDF only on Docsity!

ARTIFICIAL INTELLIGENCE

LAB MANUAL

[ACADEMIC YEAR: 201 8 - 19 ]

SEMESTER - I

COURSE NAME: ARTIFICIAL INTELLIGENCE

COURSE CODE: 15CS311 1

YEAR: III/IV B. TECH

DEPT: COMPUTER SCIENCE & ENGINEERING

List of Experiments

  1. (a). Write a python program to print the multiplication table for the given number? (b). Write a python program to check whether the given number is prime or not? (c) Write a python program to find factorial of the given number?
  2. Write a python program to implement simple Chatbot?
  3. (a) Write a python program to implement List operations (Nested List, Length, Concatenation, Membership, Iteration, Indexing and Slicing)? (b) Write a python program to implement List methods (Add, Append, Extend & Delete).
  4. (a). Write a python program to Illustrate Different Set Operations? (b). Write a python program to generate Calendar for the given month and year? (c). Write a python program to implement Simple Calculator program?
  5. (a). Write a python program to Add Two Matrices. (b). Write a python program to Transpose a Matrix.
  6. Write a python program to implement Breadth First Search Traversal?
  7. Write a python program to implement Water Jug Problem?
  8. (a) Write a python program to remove punctuations from the given string? (b) Write a python program to sort the sentence in alphabetical order?
  9. Write a program to implement Hangman game using python.
  10. Write a program to implement Tic-Tac-Toe game using python. 11.(a) Write a python program to remove stop words for a given passage from a text file using NLTK? (b) Write a python program to implement stemming for a given sentence using NLTK? (c) Write a python program to POS (Parts of Speech) tagging for the give sentence using NLTK?
  11. (a) Write a python program to implement Lemmatization using NLTK? (b) Write a python program to for Text Classification for the give sentence using NLTK?

if rem == 0: print(num,"is not a prime number") else: print(num,"is a prime number")

if input number is less than

or equal to 1, it is not prime

else: print(num,"is not a prime number") Output: Enter a number: 5 5 is a prime number (c). Write a python program to find factorial of the given number? Code: def recur_factorial(n): """Function to return the factorial of a number using recursion""" if n == 1: return n else: return n*recur_factorial(n-1)

Change this value for a different result

#num = 5

uncomment to take input from the user

num = int(input("Enter a number: "))

check is the number is negative

if num < 0: print("Sorry, factorial does not exist for negative numbers") elif num == 0: print("The factorial of 0 is 1") else:

print("The factorial of",num,"is",recur_factorial(num)) Output: Enter a number: 5 The factorial of 5 is 120

2. Write a python program to implement simple Chatbot? Code: print("Simple Question and Answering Program") print("=====================================") print(" You may ask any one of these questions") print("Hi") print("How are you?") print("Are you working?") print("What is your name?") print("what did you do yesterday?") print("Quit") while True: question = input("Enter one question from above list:") question = question.lower() if question in ['hi']: print("Hello") elif question in ['how are you?','how do you do?']: print("I am fine") elif question in ['are you working?','are you doing any job?']: print("yes. I'am working in KLU") elif question in ['what is your name?']: print("My name is Emilia") name=input("Enter your name?")

Nice name and Nice meeting you sachin Enter one question from above list:quit

3. (a) Write a python program to implement list operations (Nested List, Length, Concatenation,Membership,Iteration,Indexing and Slicing)? Code: my_list = ['p','r','o','b','e'] # Output: p print(my_list[0]) print("Length",my_list,":",len(my_list)) # Output: o print(my_list[2]) # Output: e print(my_list[4]) # Error! Only integer can be used for indexing #my_list[4.2] # Nested List n_list = [[1,3,5,7], [2,4,6,8,10]] # Nested indexing # Output: 3 print(n_list[0][1]) # Output: 8 print(n_list[1][3]) # Nested List n_list2 = ["Happy", [2,0,1,5]]

Nested indexing

Output: a

print(n_list2[0][1])

Output: 5

print(n_list2[1][3]) concat1=[1,3,5] concat2=[7,8,9] print("Concatenation:",concat1,concat2,":",concat1+concat2) repetion_string="Hello" print("Repetition:",repetion_string,repetion_string * 4) Output: p Length ['p', 'r', 'o', 'b', 'e']: 5 o e 3 8 a 5 Concatenation: [1, 3, 5] [7, 8, 9] : [1, 3, 5, 7, 8, 9] Repetition: Hello HelloHelloHelloHello

print(myList) print(len(myList)) #you can append a sublist to the current list using append myList.append(["The","earth","rotates"]) print(myList) print(len(myList)) #delete a element in the list using element myList.remove("The") #delete a element in the list using index myList.remove(myList[3]) print(myList) Output ['Earth', 'revolves', 'around', 'Sun'] 4 Earth Sun ['revolves', 'around'] Sun ['The', 'Earth', 'revolves', 'around', 'Sun'] 5 ['The', 'Earth', 'revolves', 'around', 'the', 'Sun'] ['The', 'Earth', 'revolves', 'around', 'the', 'Sun', 'continuously'] 7 ['The', 'Earth', 'revolves', 'around', 'the', 'Sun', 'continuously', 'for', 'sure'] 9 ['The', 'Earth', 'revolves', 'around', 'the', 'Sun', 'continuously', 'for', 'sure', ['The', 'earth', 'rotates']]

['Earth', 'revolves', 'around', 'Sun', 'continuously', 'for', 'sure', ['The', 'earth', 'rotates']] 4 (a). Write a python program to Illustrate Different Set Operations? Code:

Program to perform different set operations like in mathematics

define three sets

E = {0, 2, 4, 6, 8}; N = {1, 2, 3, 4, 5};

set union

print("Union of E and N is",E | N)

set intersection

print("Intersection of E and N is",E & N)

set difference

print("Difference of E and N is",E - N)

set symmetric difference

print("Symmetric difference of E and N is",E ^ N) Output: Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8} Intersection of E and N is {2, 4} Difference of E and N is {0, 8, 6} Symmetric difference of E and N is {0, 1, 3, 5, 6, 8} (b). Write a python program to generate Calendar for the given month and year? Code:

Python program to display calendar of given month of the year

import module

import calendar

return x + y def subtract(x, y): ""This function subtracts two numbers""" return x - y def multiply(x, y): """This function multiplies two numbers""" return x * y def divide(x, y): """This function divides two numbers""" return x / y

take input from the user

print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") choice = input("Enter choice(1/2/3/4):") num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if choice == '1': print(num1,"+",num2,"=", add(num1,num2)) elif choice == '2': print(num1,"-",num2,"=", subtract(num1,num2)) elif choice == '3': print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4': print(num1,"/",num2,"=", divide(num1,num2)) else: print("Invalid input") Output: Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): Enter first number: 21 Enter second number: 22 21 + 22 = 43

5. (a). Write a python program to Add Two Matrices. Code: # Program to add two matrices using nested loop X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]] Z = [[0,0,0], [0,0,0], [0,0,0]]

print(r) Output: [12, 4, 3] [7, 5, 8] 6.Write a python program to implement Breadth first search Traversal? Code: """ This file contains an implementation of breadth-first search (BFS) for traversing a graph, and for getting the shortest path between 2 nodes of a graph. """

visits all the nodes of a graph (connected component) using BFS

def bfs_connected_component(graph, start):

keep track of all visited nodes

explored = []

keep track of nodes to be checked

queue = [start]

keep looping until there are nodes still to be checked

while queue:

pop shallowest node (first node) from queue

node = queue.pop(0) if node not in explored:

add node to list of checked nodes

explored.append(node) neighbours = graph[node]

add neighbours of node to queue

for neighbour in neighbours: queue.append(neighbour) return explored

finds shortest path between 2 nodes of a graph using BFS

def bfs_shortest_path(graph, start, goal):

keep track of explored nodes

explored = []

keep track of all the paths to be checked

queue = [[start]]

return path if start is goal

if start == goal: return "That was easy! Start = goal"

keeps looping until all possible paths have been checked

while queue:

pop the first path from the queue

path = queue.pop(0)

get the last node from the path

node = path[-1] if node not in explored: neighbours = graph[node]

go through all neighbour nodes, construct a new path and

push it into the queue

for neighbour in neighbours: new_path = list(path) new_path.append(neighbour) queue.append(new_path)

bfs_connected_component(graph, 'A')) print("\nHere's the shortest path between nodes 'D' and 'F':", bfs_shortest_path(graph, 'D', 'F')) OUTPUT: - HERE'S THE NODES OF THE GRAPH VISITED BY BREADTH-FIRST SEARCH, STARTING FROM NODE 'A': ['A', 'B', 'C', 'D', 'E', 'F'] HERE'S THE SHORTEST PATH BETWEEN NODES 'D' AND 'F': ['D', 'B', 'A', 'C', 'F']

7. Write a python program to implement Water Jug Problem? A Water Jug Problem: You are given two jugs, a 4-gallon one and a 3-gallon one, a pump which has unlimited water which you can use to fill the jug, and the ground on which water may be poured. Neither jug has any measuring markings on it. How can you get exactly 2 gallons of water in the 4-gallon jug? Let X represent the content of the water in 4-gallon jug. Let Y represent the content of the water in 3-gallon jug. Write a program in python to define a set of operators (Rules) that will take us from one state to another: Start from initial state (X=0, Y=0) Reach any of the Goal states (X=2, Y=0) (X=2, Y=1) (X=2, Y=2) (X=2, Y=3) Find the minimum number of steps to reach any the above mentioned goal states. Code: print("Water Jug Problem") x=int(input("Enter X:")) y=int(input("Enter Y:")) while True: rno=int(input("Enter the Rule No")) if rno==1: if x<4:x= if rno==2: if y<3:y=

if rno==5: if x>0:x= if rno==6: if y>0:y= if rno==7: if x+y>= 4 and y>0:x,y=4,y-(4-x) if rno==8: if x+y>=3 and x>0:x,y=x-(3-y), if rno==9: if x+y<=4 and y>0:x,y=x+y, if rno==10: if x+y<=3 and x>0:x,y=0,x+y print("X =" ,x) print("Y =" ,y) if (x==2): print(" The result is a Goal state") break Output: Water Jug Problem Enter X: Enter Y: Enter the Rule No X = 0 Y = 3 Enter the Rule No X = 3 Y = 0 Enter the Rule No X = 3 Y = 3 Enter the Rule No X = 4 Y = 2 Enter the Rule No X = 0 Y = 2