You are on page 1of 23

AI SKILLING PROBLEMS

1. In terms of intelligent agent implement vacuum-cleaner world


problem in python for just two locations
Code:-
def vac(location,status,sr) :
if (location=='A' and status=='Dirty'):
print('Vacuum randomly placed at A')
print('A is Dirty')
print('Suck the dirt at',location)
if (sr != 'clean'):
print ('Move Right')
vacu('B',sr)
return 'Both rooms are clean'
elif(location=='A' and status=='clean'):
print('Vacuum randomly placed at A')
print('A is Clean')
print('Move Right')
if (sr != 'clean'):
vacu('B',sr)
return 'Both rooms are clean'
elif(location=='B' and status=='Dirty'):
print('Vacuum randomly placed at B')
print('B is Dirty')
print('Suck the dirt at',location)
if (sr != 'clean'):
print ('Move Left')
vacu('A',status)
return 'Both rooms are clean'
elif(location=='B' and status=='clean'):
print('Vacuum randomly placed at B')
print('B is Dirty')
print('Move Left')
if (sr != 'clean'):
vacu('A',status)
return 'Both rooms are clean'
def vacu(l,s2):
print('Vacuum cleaner arrived',l)
print(l,' is Dirty')
print('Suck the dirt at',l)

l,s,s1=input().split()
result=vac(l,s,s1)
print(result)
Output:-

2. In terms of intelligent agent design an interactive English tutor.


Code:-
f1=open('questions.txt',"r")
f2=open('answers.txt',"r")
que=[l.strip("\n") for l in f1.readlines()]
ans=[l.strip("\n") for l in f2.readlines()]
t='y'
while(t=='y'):
q=input("Ask something:")
j=1
for i in que:
if(i.lower()==q.lower()):
j=0
break
if(j==0):
print(ans[que.index(i)])
elif(j==1):
print("I will tell u later")
t=input("Do u have any question (y/n):")
Output:-
3. A medical diagnosis system can be considered as a type of agent.
Write a python program for performance measure of healthy
patient and reduced costs.
Code:-
print("*******MEDICAL DIAGNOSIS************ ")

print("Enter Doctors details")

doname=input("NAME: ")

doid=int(input("id: "))

print("Enter patient details")

paname=input("NAME: ")

paage=int(input("AGE: "))

pawgh=float(input("WEIGHT: "))

pahgh=float(input("HEIGHT: "))

print("Enter the problem")

print("Face ,Back and rashes")

print("Confsuion , severely slurred speech and loss of coordination")

print("Depression , Obesity and High cholestrall")


print("Spotty and blistering red rash")

print("Gums bleeding when touched")

while True:

ques=input("Enter one question from above list:")

if ques in ["Face ,Back and rashes"]:

print("ACNE ... Refer to dermotologist ")

print("its better yo get treated from sudarsan in appollo madhapur")

elif ques in ["Confsuion , severely slurred speech and loss of coordination"]:

print("Heavy Alcohol Consumption")

print("consult neurologist in andhra hospital vijayawada")

elif ques in ["Depression , Obesity and High cholestrall"]:

print("Binge Eating")

print("take cholestrol controlling juices")

elif ques in ["Spotty and blistering red rash"]:

print("Chicken Pox")

print("get affected area treated with neem")

elif ques in ["Gums bleeding when touched"]:

print("Gum Disease")

print("consult a dentist who did specialisation")

elif ques in ["quit"]:

break

else:

print("I dont understand what you said")


Output:-
4. Your goal is to navigate a robot out of maze. The robot starts in the
center of the maze facing north. You can turn the robot to face
north, east, south or west. You can direct the robot to move
forward a certain distance, although it will stop before hitting a
wall. (Make use of intelligent agent instead of robot).
Code:-
grid = [[0, 0, 0, 0, 0, 1],

[1, 1, 0, 0, 0, 1],

[0, 0, 0, 1, 0, 0],

[0, 1, 1, 0, 0, 1],

[0, 1, 0, 0, 1, 0],

[0, 1, 0, 0, 0, 2]]

def search(x, y):

if grid[x][y] == 2:

print('found at %d,%d ' %(x, y))

return True
elif grid[x][y] == 1:

print('wall at %d,%d' % (x, y))

return False

elif grid[x][y] == 3:

print('visited at %d,%d' % (x, y))

return False

print('visiting %d,%d' % (x, y))

grid[x][y] = 3

if ((x < len(grid)-1 and search(x+1, y))

or (y > 0 and search(x, y-1))

or (x > 0 and search(x-1, y))

or (y < len(grid)-1 and search(x, y+1))):

return True

return False

search(0, 0)

Output:-
5. A 3-foot –tall monkey in a room where some bananas are
suspended from the 8-foot ceiling. He would like to get the
bananas. The room contains two stackable, moveable, climbable 3-
foot-high-crates. Write a python program to solve monkey-banana
problem.
Code:-
print("enter the height of banana from ground\n")

a=int(input())

print("enter the height of monkey in the room\n")

b=int(input())

print("enter height of the chair in cms\n")

c=int(input())

print("enter height of the stick\n")

d=int(input())

print("capacity of the monkey to jump\n")

e=int(input())

if a==b:

print("monkey grabbed bananas\n")

exit(0)

elif b+e>=a:

print("monkey cannot grab bananas by hand\n")

print("monkey grabbed the bananas by jumping\n")

exit(0)

else:

print("monkey cannot grab bananas without any help\n")

print("search for any other ways or things in the room\n")

print("found chair & stick at corner of the room\n")

if c+b>=a:

print("monkey grabbed the bananas with the help of chair\n")

exit(0)

elif c+b+e>=a:

print("monkey grabbed the bananas by jumping from the chair\n")

exit(0)
elif b+d>=a:

print("monkey grabbed the bananas with the help of stick\n")

exit(0)

elif b+c+d>=a:

print("monkey grabbed the bananas with the help of stick & chair\n")

exit(0)

else:

print("monkey cannot grab banana\n")


Output:-

6. Consider the problem of finding the shortest path between two


points on a plane that has convex polygonal obstacles as shown in
given figure. This is an idealization of the problem that a robot has
to solve to navigate in a crowded environment.
Code:-
my_dict= { 'pentagon' : [35,60,51,45,40],

'Rectangle': [20,29,71, 91],

'tri_angle': [17,9,19],

'Trapizeum': [25,60,50,44],

'triangle':[19,20,17],

'Square' :[23,26,27,22],

'Hexagon': [24,16,22,14,20,18],

'Rhombus' : [26,36,34,28]

cost = 0

for i in (my_dict):

print(my_dict[i])

print('Initial Position of s')

print('s moved in pentagon',min(my_dict['pentagon']))

cost+=min(my_dict['pentagon'])

print(cost)

if min(my_dict['Rectangle'] )==my_dict['Rectangle'][0] or min(my_dict['Rectangle'] )


==my_dict['Rectangle'][1] :

print('s moved in Rectangle',min(my_dict['Rectangle']))

cost+=min(my_dict['Rectangle'])

else:

print('s moved in Rectangle',my_dict['Rectangle'][0],min(my_dict['Rectangle']))

cost+=min(my_dict['Rectangle'])+my_dict['Rectangle'][0]

print(cost)

if min(my_dict['tri_angle'] )==my_dict['tri_angle'][0]:

print('s moved in tri_angle',min(my_dict['tri_angle']))


cost+=min(my_dict['tri_angle'])

else:

print('s moved in tri_angle',my_dict['tri_angle'][0],min(my_dict['tri_angle']))

cost+=min(my_dict['tri_angle'])+my_dict['tri_angle'][0]

print(cost)

if min(my_dict['Trapizeum'] )==my_dict['Trapizeum'][0] or min(my_dict['Trapizeum'] )


==my_dict['Trapizeum'][1] :

print('s moved in Trapizeum',min(my_dict['Trapizeum']))

cost+=min(my_dict['Trapizeum'])

else:

print('s moved in Trapizeum',my_dict['Trapizeum'][0],min(my_dict['Trapizeum']))

cost+=min(my_dict['Trapizeum'])+my_dict['Trapizeum'][0]

print(cost)

if min(my_dict['triangle'] )==my_dict['triangle'][0] :

print('s moved in triangle',min(my_dict['triangle']))

cost+=min(my_dict['triangle'])

else:

print('s moved in triangle',my_dict['triangle'][0],min(my_dict['triangle']))

cost+=min(my_dict['triangle'])+my_dict['triangle'][0]

print(cost)

if min(my_dict['Square'] )==my_dict['Square'][0] or min(my_dict['Square'] )


==my_dict['Square'][1] :

print('s moved in Square',min(my_dict['Square']))

cost+=min(my_dict['Square'])

else:

print('s moved in Square',my_dict['Square'][0],min(my_dict['Square']))

cost+=min(my_dict['Square'])+my_dict['Square'][0]

print(cost)
if min(my_dict['Hexagon'] )==my_dict['Hexagon'][0] or min(my_dict['Hexagon'] )
==my_dict['Hexagon'][1] :

print('s moved in Hexagon',min(my_dict['Hexagon']))

cost+=min(my_dict['Hexagon'])

else:

print('s moved in Hexagon',my_dict['Hexagon'][0],min(my_dict['Hexagon']))

cost+=min(my_dict['Hexagon'])+my_dict['Hexagon'][0]

print(cost)

if min(my_dict['Rhombus'] )==my_dict['Rhombus'][0] or min(my_dict['Rhombus'] )


==my_dict['Rhombus'][1] :

print('s moved in Rhombus',min(my_dict['Rhombus']))

cost+=min(my_dict['Rhombus'])

else:

print('s moved in Rhombus',my_dict['Rhombus'][0],min(my_dict['Rhombus']))

cost+=min(my_dict['Rhombus'])+my_dict['Rhombus'][0]

print('Total cost to reach destination',cost)


Output:-
7. Three missionaries and three cannibals are on one side of a river,
along with a boat that can hold one or two people. Find a way to
get everyone to the other side without ever leaving a group of
missionaries in one place outnumbered by the cannibals in that
place. Implement and solve the problem optimally using an
appropriate search algorithm. Write Python code.
Code:-
print("Boat M C")

i=j=3

x=y=0

b=[0,0]

boat=sum(b)
print(boat,i,j)

if x==0 and y==0:

b[0]=b[1]=1

i-=b[0]

j-=b[1]

boat=sum(b)

print(boat,i,j)

if i==j:

y=1

b[1]-=y

boat=sum(b)

print(boat,i,j)

if x<y and i==j:

b[0]-=1

b[1]=j

j-=2

i+=1

boat=sum(b)

print(boat,i,j)

if x<y and i>j:

b[1]=1

y+=1

boat=sum(b)

print(boat,i,j)

if x<y and i>j:

b[0]=i-1

b[1]-=1
i-=2

j+=1

boat=sum(b)

print(boat,i,j)

if x<y and i==j:

x=y=i=j=b[0]=b[1]=1

boat=sum(b)

print(boat,i,j)

if i==j and x==y and b[0]==b[1]:

b[0]+=1

b[1]-=1

i-=1

j+=1

boat=sum(b)

print(boat,i,j)

if x==y and i<j:

y-=1

x+=b[0]

b[0]=0

b[1]=1

boat=sum(b)

print(boat,i,j)

if x>y and i<j:

b[1]+=1

j-=1

boat=sum(b)

print(boat,i,j)
if x>y and i<j:

y+=1

b[1]-=1

boat=sum(b)

print(boat,i,j)

if x>y and i<j:

j-=1

b[1]+=1

boat=sum(b)

print(boat,i,j)

if i==j==0:

y+=b[1]

b[1]-=b[1]

boat=sum(b)

print(boat,i,j)

Output:-
8. (i)Implement SEND + MORE = MONEY Problem using CSP
algorithm.
Code:-
import itertools

def get_value(word, substitution):


s=1
factor = 1
for letter in reversed(word):
s += factor * substitution[letter]
factor *= 10
return s

def solve2(equation):
# split equation in left and right
left, right = equation.lower().replace(' ', '').split('=')
# split words in left part
left = left.split('+')
# create list of used letters
letters = set(right)
for word in left:
for letter in word:
letters.add(letter)
letters = list(letters)

digits = range(10)
for perm in itertools.permutations(digits, len(letters)):
sol = dict(zip(letters, perm))

if sum(get_value(word, sol) for word in left) == get_value(right, sol):


print(' + '.join(str(get_value(word, sol)) for word in left) + " = {} (mapping:
{})".format(get_value(right, sol), sol))
break;

if __name__ == '__main__':
print("SEND + MORE = MONEY ")
solve2('SEND + MORE = MONEY ')
(ii) Implement CROSS + ROAD = DANGER Problem using CSP
algorithm.
Code:-
import itertools

def get_value(word, substitution):


s=1
factor = 1
for letter in reversed(word):
s += factor * substitution[letter]
factor *= 10
return s

def solve2(equation):
# split equation in left and right
left, right = equation.lower().replace(' ', '').split('=')
# split words in left part
left = left.split('+')
# create list of used letters
letters = set(right)
for word in left:
for letter in word:
letters.add(letter)
letters = list(letters)

digits = range(10)
for perm in itertools.permutations(digits, len(letters)):
sol = dict(zip(letters, perm))

if sum(get_value(word, sol) for word in left) == get_value(right, sol):


print(' + '.join(str(get_value(word, sol)) for word in left) + " = {} (mapping:
{})".format(get_value(right, sol), sol))
break;

if __name__ == '__main__':
print("CROSS + ROAD = DANGER ")
solve2('CROSS + ROAD = DANGER ')
Output:-
9. Use precise formulations for the following as constraint
satisfaction problems and implement it in python: Class
scheduling: There is a fixed number of professors and classrooms,
a list of classes to be offered, and a list of possible time slots for
classes. Each professor has a set of classes that he or she can teach.
Code:-
from collections import defaultdict

from itertools import permutations

pro=['A','B','C','D','E','F'];

classroom=['s1','s2','s3','s4','s5'];

time=['9am-10am','10am-11am','11am-12pm','1.30pm-2.30pm','2.30pm-3.30pm'];

ans=defaultdict(lambda:false)

perm=list(permutations(pro))

days=['day1','day2','day3','day4','day5']

for i in range(0,1):

ans[days[i]]=[perm[i][0],classroom[0],time[0],perm[i][1],classroom[1],time[i],perm[i]
[2],classroom[2],time[i]];
print(ans)

Output:-

10. Represent the following sentences in first-order logic, using a


consistent vocabulary (which you must define) and implement it
in python.
a. Some students took French in spring 2001.
b. Every student who takes French passes it.
c. Only one student took Greek in spring 2001.
d. The best score in Greek is always higher than the best
score in French.
e. Every person who buys a policy is smart.
f. No person buys an expensive policy.
g. There is an agent who sells policies only to people who
are not insured.
h. There is a barber who shaves all men in town who do
not shave themselves.
i. A person born in the UK, each of whose parents is a UK
citizen or a UK resident, is a UK citizen by birth.
j. A person born outside the UK, one of whose parents is a
UK citizen by birth, is a UK citizen by descent.
k. Politicians can fool some of the people all of the
time, and they can fool all of the people some of the time, but
they can't fool all of the people all of the time.

You might also like