Source Code Ai
Source Code Ai
source code x=['M', 'L', 'G' , 'C'] y=[] print("Before Process") print("Element in the Left Side Bank ", x) print("Element in the Right Side Bank ", y ) while True:
print(x[1]," ", x[2]," ", x[3], " Select any one from the list") i=input("Enter the item :") i=i.upper() if x[1]==i and x[2]=='G' and x[3]=='C': print("Goat will eat cabbage :")
break elif x[2]==i and x[3]!='C': y.append(x[2]) if len(y)==2 and y[0]=='G': x[2]=y[0] y[0]=y[1] y.pop() elif x[1]==i and x[2]=='G': y.append(x[1]) x[1]=x[2] x[2]='' elif
x[1]==i and x[2]=='C': y.append(x[1]) x[1]=x[2] x[2]='' if len(y)==2 and y[0]=='G': x[2]=y[0] y[0]=y[1] y.pop() elif x[1]==i and x[2]!='C' and x[2]!='G': y.append(x[1])
y.append('M') x[1]='' x=[] print("Goal is reached ") break if x[2]==i and x[3]=='C': y.append(x[2]) x[2]=x[3] x[3]='' if x[3]==i: print("Lion will eat Goat ") break
print("After Process") print("Element in the Left Side Bank ", x) print("Element in the Right Side Bank ", y)
ource code- BY MINIMUM STEPS class Waterjug: def __init__(self,am,bm,a,b,g): self.a_max = am; self.b_max = bm; self.a = a; self.b = b; self.goal = g; def fillA(self): self.a = self.a_max; print ('(',
self.a, ',',self.b, ')') def fillB(self): self.b = self.b_max; print ('(', self.a, ',', self.b, ')') def emptyA(self): self.a = 0; print ('(', self.a, ',', self.b, ')') def emptyB(self): self.b = 0; print ('(', self.a, ',', self.b,
')') def transferAtoB(self): while (True): self.a = self.a - 1 self.b = self.b + 1 if (self.a == 0 or self.b == self.b_max): break print ('(', self.a, ',', self.b, ')') def main(self): while (True): if (self.a ==
self.goal or self.b == self.goal): break if (self.a == 0): self.fillA() elif (self.a > 0 and self.b != self.b_max): self.transferAtoB() elif (self.a > 0 and self.b == self.b_max): self.emptyB()
waterjug=Waterjug(5,3,0,0,4); waterjug.main();
BY MAXIMUM STEPS def pour(jugM, jugN): A, B, fill = 3, 5, 4 print("%d\t%d" % (jugM,jugN)) if jugN is fill: return elif jugN is B: pour(0, jugM) elif jugM != 0 and jugN
is 0: pour(0, jugM) elif jugM is fill: pour(jugM, 0) elif jugM < A: pour(A, jugN) elif jugM < (B-jugN): pour(0, (jugM+jugN)) else: pour(jugM-(B-jugN), (B-jugN)+jugN)
print("JUGM\tJUGN") pour(0, 0)
Source code BFS- from queue import Queue graph = { 0 : [1,2,3], 1 : [0,4], 2 : [0,4], 3 : [0,4], 4 : [1,2,3] } print("The adjacency List representing the graph is:")
print(graph) def bfs(graph, source): Q = Queue() visited_vertices = set() Q.put(source) visited_vertices.update({1}) while not Q.empty(): vertex = Q.get() print(vertex,
end=" ") for u in graph[vertex]: if u not in visited_vertices: Q.put(u) visited_vertices.update({u}) print("BFS traversal of graph with source 1 is:") bfs(graph, 1)
dFS- class Stack: def __init__(self): self.list = [] def push(self, item): self.list.append(item) def pop(self): return self.list.pop() def top(self): return self.list[-1] def
is_empty(self): return len(self.list) == 0 def depth_first_search(graph, start): stack = Stack() stack.push(start) path = [] while not stack.is_empty(): vertex =
stack.pop() if vertex in path: continue path.append(vertex) for neighbor in graph[vertex]: stack.push(neighbor) return path def main(): adjacency_matrix = { 'S' :
['A','B','C'], 'A' : ['S','D'], 'B' : ['D','S'], 'C' : ['D','S'], 'D' : ['A','B','C'] } dfs_path = depth_first_search(adjacency_matrix, 'B') print("Depth First Traversal is : ")
print(dfs_path) if __name__ == '__main__': main()
Source code – def TowerOfHanoi(n , source, destination, auxiliary): if n==1: print ("Move disk 1 from source",source,"to destination",destination) return
TowerOfHanoi(n-1, source, auxiliary, destination) print ("Move disk",n,"from source",source,"to destination",destination) TowerOfHanoi(n-1, auxiliary, destination,
source) n = 4 TowerOfHanoi(n,'A','B','C')