Practical No : 4
sing
Problem Statement : Implement a solution for a Constraint Satisfaction Problem using
Branch and Bound and Backtracking for n-queens problem or a graph colouring problem.
Code :
# Python program to solve N Queen
# Problem using backtracking
global N
N = int(input("enter the N :"))
def printSolution(board): for i in
range(N): for j in range(N):
print (board[i][j],end=' ')
print()
# A utility function to check if a queen can
# be placed on board[row][col]. Note that this
# function is called when "col" queens are
# already placed in columns from 0 to col -1.
# So we need to check only left side for
# attacking queens def isSafe(board, row,
col):
# Check this row on left side for i in
range(col):
if board[row][i] == 1:
return False
# Check upper diagonal on left side for i, j in zip(range(row, -1, -1),
range(col, -1, -1)): if board[i][j] == 1:
return False
# Check lower diagonal on left side for i, j in zip(range(row, N, 1),
range(col, -1, -1)): if board[i][j] == 1:
return False
return True
def solveNQUtil(board, col):
# base case: If all queens are placed
# then return true if col
>= N:
return True
# Consider this column and try placing # this queen in
all rows one by one for i in range(N):
if isSafe(board, i, col): # Place this queen in
board[i][col] board[i][col] = 1
# recur to place rest of the queens if
solveNQUtil(board, col + 1) == True:
return True
# If placing queen in board[i][col
# doesn't lead to a solution, then #
queen from board[i][col] board[i][col] = 0
# if the queen can not be placed in any row in
# this column col then return false return False
# This function solves the N Queen problem using
# Backtracking. It mainly uses solveNQUtil() to
# solve the problem. It returns false if queens
# cannot be placed, otherwise return true and # placement of queens
in the form of 1s.
# note that there may be more than one
# solutions, this function prints one of the # feasible solutions.
def solveNQ():
board = [ [0, 0, 0, 0,0,0,0,0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
] if solveNQUtil(board, 0) == False: print ("Solution does not exist")
return False
printSolution(board) return True
solveNQ()
Output :
enter the N :6 0 0 0 1 0
0
100000
000010
010000
000001
001000
True
Practical No : 5
Problem Statement Develop an elementary chatbot for any suitable customermer
interaction application.
Code :
def greet(bot_name, birth_year):
print("Hello! My name is {0}.".format(bot_name)) print("I was created in
{0}.".format(birth_year))
def remind_name(): print('Please, remind me your name.') name = input()
print("What a great name you have, {0}!".format(name))
def guess_age():
print('Let me guess your age.') print('Enter remainders of dividing your age by 3, 5 and 7.')
rem3 = int(input()) rem5 =
int(input()) rem7 = int(input())
age = (rem3 * 70 + rem5 * 21 + rem7 * 15) % 105
print("Your age is {0}; that's a good time to start programming!".format(age))
def count(): print('Now I will prove to you that I can count to any number you want.')
num = int(input())
counter = 0 while counter <=
num:
print("{0} !".format(counter)) counter += 1
def test():
print("Let's test
your
programming
knowledge.")
print("Why do
we use
methods?")
print("1. To repeat a statement multiple times.") print("2. To decompose a program
into several small subroutines.") print("3. To determine the execution time of a
program.") print("4. To interrupt the execution of a program.")
answer = 2 guess = int(input()) while guess
!= answer: print("Please, try again.")
guess = int(input())
print('Completed, have a nice day!')
print('.................................') print(' ................................ ')
print(' ................................ ')
def end():
print('Congratulations, have a nice day!')
print('.................................') print(' ................................ ')
print('.................................') input()
greet('Sbot', '2021') # change it as you need remind_name()
guess_age() count() test() end()
Output :
Hello! My name is Sbot.
I was created in 2021. Please, remind me
your name. omkar
What a great name you have, omkar!
Let me guess your age.
Enter remainders of dividing your age by 3, 5 and 7.
0
1
0
Your age is 21; that's a good time to start programming!
Now I will prove to you that I can count to any number you want.
5
0 !
1 !
2 !
3 !
4 !
5 !
Let's test your programming knowledge.
Why do we use methods?
1. To repeat a statement multiple times.
2. To decompose a program into several small subroutines.
3. To determine the execution time of a program.
4. To interrupt the execution of a program.
2
Completed, have a nice day!
.................................
.................................
.................................
Congratulations, have a nice day!
.................................
................................. .................................
bye