Artificial Intelligence Lab File
Artificial Intelligence Lab File
Artificial Intelligence Lab File
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.
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.
def gcd(x,y):
if y == 0:
return x
return gcd(y, x % y)
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
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
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)
Assignment : 4
else:
pour(jug1-(cap2-jug2), (cap2-jug2)+jug2)
print("JUG1\tJUG2")
pour(0, 0)
RAHUL GUPTA, 2000520140040
Assignment : 5
# 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 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
Assignment : 7
RAHUL GUPTA, 2000520140040
# 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
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
# 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)
app = App()
mainloop()
RAHUL GUPTA, 2000520140040