Artificial Intelligence Lab
Artificial Intelligence Lab
Artificial Intelligence Lab
1 a) Write a program to print the multiplication table for the given number
b) Write a program to find factorial of a number
c) Write a program to check whether the given number is prime or not
a) Write program to implement Simple Calculator program
2 b) Write a program to generate Calendar for the given month and year
c) Write a program to Illustrate Different Set Operations
1A) Aim: Write a program to print the multiplication table for the given number
Program:
# Python program to find the multiplication table (from 1 to 10) of a number input by the
user
# Get input from the user
for i in range(1,11):
print(num,'x',i,'=',num*i)
Output:
Display multiplication table of?
55x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
PROGRAM-1(B)
Program:
def recur_factorial(n):
if n == 1:
return n else:
return n*recur_factorial(n-1)
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
PROGRAM-1(C)
1C) Aim: Write a program to check whether the given number is prime or not?
Program:
# Python program to check if the input number is prime or not
rem=1
for i in range(2,num):
rem=num%i
if rem == 0:
break
if rem == 0:
print(num,"is not a prime number")
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
Output:
Enter a number: 5
5 is a prime number
Program:
# Program make a simple calculator that can add, subtract, multiply and divide using
functions
# define functions
return x + y
return x - y
return x * y
return x / y
Output:
Select operation.
1.Add
2. Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4):1
21 + 22 = 43
PROGRAM-2(B)
2B) Aim: Write a program to generate Calendar for the given month and year
Program:
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
PROGRAM-2(C)
Program:
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};
# set union
# set difference
print("Difference of E and N is",E - N)
Output:
Aim:
else:
print("I don't understand what you said")
Output:
Simple Question and Answering Program
==============================
You may ask any one of these
questions Hi
How are you?
Are you
working?
What is your name?
what did you do
yesterday? Quit
Enter one question from above list:hi
Hello
Enter one question from above list:how are
you? I am fine
Enter one question from above list:are you working?
yes. I'am working in MRCET
Enter one question from above list:what is your name?
My name is Emilia
Enter your name?sachin
Nice name and Nice meeting you sachin
Enter one question from above list:quit
Program:
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
my_str = "Hello!!!, he said ---and went."
# To take input from the user
# my_str = input("Enter a string: ")
# remove punctuation from the string
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
# display the unpunctuated string
print(no_punct)
Output:
Hello he said and went
PROGRAM-4(B)
Program:
words = my_str.split()
#print(words)
# sort the list
words.sort()
# display the sorted words
print("The sorted words are:")
for word in words:
print(word)
Output:
The sorted words are:
Example
Hello
Is
With
an
cased
letter
s this
Program:
# Recursive Python function to solve tower of hanoi
def TowerOfHanoi(n , from_rod, to_rod, aux_rod):
if n == 0:
return
TowerOfHanoi(n-1, from_rod, aux_rod, to_rod)
Aim:
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
# Driver Code
print("Following is the Breadth-First
Search") bfs(visited, graph, '5') #
function calling
Output:
Following is the Breadth-First Search
537248
Aim:
Program:
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
# Driver Code
Output:
Following is the Depth-First Search
5
3
2
4
Aim:
Program:
import random
def randomSolution(tsp):
cities = list(range(len(tsp)))
solution = []
for i in range(len(tsp)):
solution.append(randomCity)
cities.remove(randomCity)
return solution
routeLength = 0
for i in range(len(solution)):
return routeLength
def getNeighbours(solution):
neighbours = []
for i in range(len(solution)):
neighbour[i] = solution[j]
neighbour[j] = solution[i]
neighbours.append(neighbour)
return neighbours
bestNeighbour = neighbours[0]
bestRouteLength =
currentRouteLength bestNeighbour =
neighbour
def hillClimbing(tsp):
currentSolution = randomSolution(tsp)
neighbours = getNeighbours(currentSolution)
currentSolution = bestNeighbour
currentRouteLength = bestNeighbourRouteLength
Artificial Intelligence Manual
neighbours = getNeighbours(currentSolution)
def main():
tsp = [
print(hillClimbing(tsp))
main()
Output:
Aim:
Program:
from collections import deque
class Graph:
def init (self, adjac_lis):
self.adjac_lis = adjac_lis
# This is heuristic function which is having equal values for all nodes
def h(self, n):
H={
'A': 1,
'B': 1,
'C': 1,
'D': 1
}
return H[n]
if n == None:
print('Path does not exist!')
return None
while par[n] != n:
reconst_path.append(n) n =
par[n]
reconst_path.append(start)
reconst_path.reverse()
if m in closed_lst:
closed_lst.remove(m)
open_lst.add(m)
Output:
Aim:
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
player = 1
########win Flags##########
Win = 1
Draw = -1
Running = 0
Stop = 1
###########################
Game = Running
Mark = 'X'
os.system('cls')
DrawBoard()
if(Game==Draw):
print("Game Draw")
elif(Game==Win):
player-=1
if(player%2!=0):
print("Player 1 Won")
else:
print("Player 2 Won")
Output:
[[0 0 0]
[0 0 0]
[0 0 0]]
Board after 1
move [[0 0 0]
[0 1 0]
[0 0 0]]
Board after 2
move [[0 0 0]
[0 1 0]
[0 2 0]]
Board after 3
move [[0 0 0]
[0 1 0]
[1 2 0]]
Board after 4
move [[0 0 2]
[0 1 0]
[1 2 0]]
Board after 5
move [[0 0 2]
Artificial Intelligence Manual
[0 1 0]
[1 2 1]]
Board after 6
move [[0 0 2]
[0 1 2]
[1 2 1]]
Board after 7
move [[0 0 2]
[1 1 2]
[1 2 1]]
Board after 8
move [[2 0 2]
[1 1 2]
[1 2 1]]
Board after 9
move [[2 1 2]
[1 1 2]
[1 2 1]]
Winner is: -1
print("Steps: ")
Output:
Steps:
0 0
4 0
4 3
0 3
3 0
3 3
4 2
0 2
True