RAHUL GUPTA, 2000520140040
ARTIFICIAL INTELLIGENCE LAB FILE
UNDER GUIDANCE BY: SUBMITTED BY:
DR. SHASHANK YADAV RAHUL GUPTA
2000520140040
MCA 3 rd SEMESTER
RAHUL GUPTA, 2000520140040
INDEX
SR ASSIGNMENT REMARK
1. i) Program for checking that given number is prime
number or not.
ii) Program for calculating GCD(Greatest Common
Divisor) of two integer numbers.
iii) Program for Sorting an array of numbers.
iv) Program for Linear Search.
v) Implement List, Tuples, and Dictionary in python.
2. Python program for DFS (Depth First Search) in a
given graph.
3. Python program for BFS (Breadth First Search) in a
given graph.
4. Python program for implementing Water-Jug
Problem.
5. Python program for implementing 4-Queens Problem.
6. Implement 8-Puzzle problem using Python.
7. Recursive Python function to solve tower of hanoi
problem.
8. Implementation of pattern recognition problem
(Handwritten digit recognition).
RAHUL GUPTA, 2000520140040
Assignment : 1
Objectives :
i) Program for checking that given number is prime number or not.
ii) Program for calculating GCD(Greatest Common Divisor) of two
integer numbers.
iii) Program for Sorting an array of numbers.
iv) Program for Linear Search.
v) Implement List, Tuples, and Dictionary in python.
i) n = int(input(“Enter a number : ”))
for i in range(2, n//2):
if n%i == 0;
print(“%d is not prime number” %n)
break
else:
print (“%d is prime number” %n)
RAHUL GUPTA, 2000520140040
ii) x = int(input(“Enter two numbers : ”))
y = int(input())
def gcd(x,y):
if y == 0:
return x
return gcd(y, x % y)
print(“GCD is : %d ” %gcd(a, b))
RAHUL GUPTA, 2000520140040
iii) print(“how many no. do you want to enter” )
n = int(input())
arr = []
for_in range(n):
arr.append(int(input()))
print(“Before sorting array\n”)
print(arr)
print(“\n After sorting array \n”)
arr.sort()
print(arr)
RAHUL GUPTA, 2000520140040
iv) print(“ How many no. do you want to enter ”)
n = int(input())
arr = []
for_in range(n) :
arr.append(int(input()))
print(“which no. do you want to search :”)
k = int(input())
# performing linear search
for i in range(n) :
if arr[i] == k:
print(“number found at index %d” %i)
break
else:
print(“ No. not found”)
RAHUL GUPTA, 2000520140040
v) # create a list
a = [23,14,5,78,36,45]
# print the list
print(a)
# Access using index of list items.
print(a[3])
# change the item of the list
a[0] = 25
print(a)
a.pop(3) # drop the element at position
print(a)
a.remove(14) # remove the element
print(a)
a.append(14) # add the element in the list
a.append(36)
a.sort()
print(a)
T = (23,14,5,45,36) # create the tuple
print(T)
print(T.count(5)) # count the number of item
print(T.index(45)) # get the position of item
# Create the dictionary
D={
"Fan": "पंखा",
"Box": "डब्बा",
"Class": "कक्षा",
"Secularism": "धर्मनिरपेक्ष "
}
print("Options are ", D.keys()) #keys() get the key of items in the
dictionary.
k = input("Enter the Word\n")
#print("The meaning of your word is: ", D[k])
print("The meaning of your word is: ", D.get(k)) # get() will not throw an
error, key is not present.
RAHUL GUPTA, 2000520140040
RAHUL GUPTA, 2000520140040
Assignment : 2
Objectives: Python program for DFS (Depth First Search) in
a given graph.
graph = {
0 : [1,2],
1 : [0,3,4],
2 : [0],
3 : [1],
4 : [2,3],
}
Stack = []
visited = []
node = 0
stack.append(node)
visited.append(node)
while stack :
node = stack.pop()
print(node , end = ‘ ’)
for n in graph[node] :
if n not in visited :
visited.append(n)
stack.append(n)
RAHUL GUPTA, 2000520140040
Assignment : 3
Objectives : Python program for BFS (Breadth First Search)
in a given graph.
graph = {
0 : [1],
1 : [3, 4],
2 : [0],
3 : [1,2],
4 : [2],
}
queue = []
visited = []
node = 0 #assuming a starting node
visited.append(node)
queue.append(node)
#if __name __ == “__main__”:
While queue :
node = queue.pop(0)
print(node, end == “ ” )
for n in graph[node] :
if n not in visited :
queue.append(n)
visited.append(n)
RAHUL GUPTA, 2000520140040
Assignment : 4
Objectives : Python program for implementing Water-Jug
Problem.
global cap1, cap2, fill
cap1 = int(input("size of jug1 :"))
cap2 = int(input("size of jug2 :"))
fill = int(input("target :"))
def pour(jug1, jug2):
#cap1, cap2, fill = 3, 4, 2 #Change maximum capacity and final
capacity
print("%d\t%d" % (jug1, jug2))
if jug2 is fill:
return
elif jug2 is cap2:
pour(0, jug1)
elif jug1 != 0 and jug2 == 0:
pour(0, jug1)
elif jug1 is fill:
pour(jug1, 0)
elif jug1 < cap1:
pour(cap1, jug2)
RAHUL GUPTA, 2000520140040
elif jug1 < (cap2-jug2):
pour(0, (jug1+jug2))
else:
pour(jug1-(cap2-jug2), (cap2-jug2)+jug2)
print("JUG1\tJUG2")
pour(0, 0)
RAHUL GUPTA, 2000520140040
Assignment : 5
Objectives : Python program for implementing 4-Queens
Problem.
#___________N QUEEN PROBLEM_________
# board = [
# [0,0,0,0],
# [0,0,0,0],
# [0,0,0,0],
# [0,0,0,0]
#]
N = int(input("enter value of n "))
board = []
# board creation
for i in range(N):
board.append([])
for j in range(N):
board[i].append(0)
def is_attack(i, j):
#cheking in row and column
for k in range(N):
if(board[i][k] == 1 or board[k][j] == 1):
return True
RAHUL GUPTA, 2000520140040
# checking for diagonals
for k in range(N):
for l in range(N):
if (i + j) == (k + l) or (i - j) == (k - l):
if board[k][l] == 1:
return True
return False
def queen_solve(n):
if n == 0:
return True
for i in range(N):
for j in range(N):
if not(is_attack(i, j)) and board[i][j] != 1:
board[i][j] = 1
if queen_solve(n - 1) == True:
return True
board[i][j] = 0
return False
#print(board)
if queen_solve(N):
RAHUL GUPTA, 2000520140040
for i in board:
print(i)
else:
print("not posible to place all queen")
Assignment : 6
RAHUL GUPTA, 2000520140040
Objectives : Implement 8-Puzzle problem using Python.
Assignment : 7
RAHUL GUPTA, 2000520140040
Objectives : Recursive Python function to solve tower of
hanoi problem.
# Creating a recursive function
def tower_of_hanoi(disks, source, auxiliary, target):
if(disks == 1):
print('Move disk 1 from rod {} to rod {}.'.format(source, target))
return
# function call itself
tower_of_hanoi(disks - 1, source, target, auxiliary)
print('Move disk {} from rod {} to rod {}.'.format(disks, source, tar
get))
tower_of_hanoi(disks - 1, auxiliary, source, target)
disks = int(input('Enter the number of disks: '))
# We are referring source as A, auxiliary as B, and target as C
tower_of_hanoi(disks, 'A', 'B', 'C') # Calling the function
Assignment : 8
RAHUL GUPTA, 2000520140040
Objectives : Implementation of pattern recognition problem
(Handwritten digit recognition).
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
model = load_model('mnist.h5')
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28))
#convert rgb to grayscale
img = img.convert('L')
img = np.array(img)
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1)
img = img/255.0
#predicting the class
res = model.predict([img])[0]
return np.argmax(res), max(res)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg =
"white", cursor="cross")
self.label = tk.Label(self, text="Draw..", font=("Helvetica",
48))
RAHUL GUPTA, 2000520140040
self.classify_btn = tk.Button(self, text = "Recognise",
command = self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command
= self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the
canvas
rect = win32gui.GetWindowRect(HWND) # get the
coordinate of the canvas
a,b,c,d = rect
rect=(a+4,b+4,c-4,d-4)
im = ImageGrab.grab(rect)
digit, acc = predict_digit(im)
self.label.configure(text= str(digit)+', '+ str(int(acc*100))
+'%')
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y +
r, fill='black')
app = App()
mainloop()
RAHUL GUPTA, 2000520140040