Ai Programs
Ai Programs
class Solution:
dict = {}
flatten = []
for i in range(len(board)):
flatten += board[i]
flatten = tuple(flatten)
dict[flatten] = 0
return 0
return self.get_paths(dict)
cnt = 0
while True:
if len(current_nodes) == 0:
return -1
for node in current_nodes:
next_moves = self.find_next(node)
dict[move] = cnt + 1
return cnt + 1
cnt += 1
moves = {
0: [1, 3],
1: [0, 2, 4],
2: [1, 5],
3: [0, 4, 6],
4: [1, 3, 5, 7],
5: [2, 4, 8],
6: [3, 7],
7: [4, 6, 8],
8: [5, 7],
results = []
pos_0 = node.index(0)
results.append(tuple(new_node))
return results
ob = Solution()
matrix = [
[3, 1, 2],
[4, 7, 5],
[6, 8, 0]
print(ob.solve(matrix))
8queen
N = int(input())
for k in range(0,N):
if board[i][k]==1 or board[k][j]==1:
return True
#checking diagonally
for k in range(0,N):
for l in range(0,N):
if (k+l==i+j) or (k-l==i-j):
if board[k][l]==1:
return True
return False
def N_queens(n):
if n==0:
return True
for i in range(0,N):
for j in range(0,N):
if N_queens(n-1)==True:
return True
board[i][j] = 0
return False
N_queens(N)
for i in board:
print (i)
Crypt 1
import itertools
s=0
factor = 1
s += factor * substitution[letter]
factor *= 10
return s
def solve2(equation):
left = left.split('+')
letters = set(right)
letters.add(letter)
letters = list(letters)
digits = range(1 0)
if __name__ == '__main__':
open_set = set(start_node)
closed_set = set()
g[start_node] = 0
parents[start_node] = start_node
n = None
for v in open_set:
n=v
if n == stop_node or Graph_nodes[n] == None:
pass
else:
#nodes 'm' not in first and last set are added to first
open_set.add(m)
parents[m] = n
#for each node m,compare its distance from start i.e g(m) to the
else:
#update g(m)
#change parent of m to n
parents[m] = n
if m in closed_set:
closed_set.remove(m)
open_set.add(m)
if n == None:
return None
if n == stop_node:
path = []
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
return path
open_set.remove(n)
closed_set.add(n)
return None
def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None
def heuristic(n):
H_dist = {
'A': 11,
'B': 6,
'C': 99,
'D': 1,
'E': 7,
'G': 0,
}
return H_dist[n]
Graph_nodes = {
'C': None,
aStarAlgo('A', 'G')
SMASTAR
class Node:
self.state = state
self.parent = parent
self.action = action
def heuristic(state):
distance = 0
for i in range(9):
if state[i] != 0:
return distance
open_list[:] = open_list[:len(open_list) // 2] # Keep only the top half of the open list
# Define the function to select the best node
def select_best_node(open_list):
def is_goal(node):
def generate_successors(node):
successors = []
zero_index = node.state.index(0)
new_state = list(node.state)
return successors
if node.state == successor.state:
return True
return False
# Define the memory-bounded search function
node = Node(initial_state)
open_list = [node]
closed_list = []
while open_list:
prune_memory(open_list, closed_list)
if not open_list:
return None
current_node = select_best_node(open_list)
if is_goal(current_node):
return current_node
open_list.remove(current_node)
closed_list.append(current_node)
open_list.append(successor)
return None
goal_state = (1, 2, 3, 4, 5, 6, 7, 8, 0)
moves = {
0: [1, 3],
1: [0, 2, 4],
2: [1, 5],
3: [0, 4, 6],
4: [1, 3, 5, 7],
5: [2, 4, 8],
6: [3, 7],
7: [4, 6, 8],
8: [5, 7]
# Example usage
if goal_node:
print("Solution found!")
while goal_node.parent:
print("Action:", goal_node.action)
print("State:")
print(goal_node.state[:3])
print(goal_node.state[3:6])
print(goal_node.state[6:])
print()
goal_node = goal_node.parent
else:
print("Memory limit exceeded. No solution found within the given memory limit.")
if goal_node:
print("Solution found!")
print("Action:", goal_node.action)
print("State:")
print(goal_node.state[:3])
print(goal_node.state[3:6])
print(goal_node.state[6:])
print()
goal_node = goal_node.parent
else:
print("Memory limit exceeded. No solution found within the given memory limit.")
MINMAX
if depth == 3:
return values[nodeIndex]
if maximizingPlayer:
best = MIN
break
return best
else:
best = MAX
break
return best
if __name__ == "__main__":
CONSTRAINTS = [
("csc", "maths"),
("csc", "phy"),
("mat", "phy"),
("mat", "che"),
("mat", "tam"),
("phy", "tam"),
("phy", "eng"),
("che", "eng"),
("tam", "eng"),
("tam", "bio"),
("eng", "bio")
def backtrack(assignment):
if len(assignment) == len(VARIABLES):
return assignment
var = select_unassigned_variable(assignment)
assignment[var] = value
result = backtrack(assignment)
return result
return None
def select_unassigned_variable(assignment):
return var
return False
return True
solution = backtrack(dict())
print(solution)