Skip to content
This repository was archived by the owner on Dec 19, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions Python/8_puzzle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import copy
start_state = [[1, 2, 3], [8, 0, 4], [7, 6, 5]]
goal_state = [[2, 8, 1], [0, 4, 3], [7, 6, 5]]
q = []
cnt = 1


def find_pos(start):
for i in range(len(start)):
for j in range(len(start[i])):
if start[i][j] == 0:
return (i, j)


def compare(curr, goal):
if curr == goal:
return 1
else:
return 0


def up(state, u, v):
state[u][v] = state[u-1][v] # up
state[u-1][v] = 0
return state


def right(state, l, m):
state[l][m] = state[l][m+1] # right
state[l][m+1] = 0
return state


def left(state, n, o):
state[n][o] = state[n][o-1] # left
state[n][o-1] = 0
return state


def down(state, r, s):
state[r][s] = state[r+1][s] # down
state[r+1][s] = 0
return state


def states(start, goal):
pos = find_pos(start)
i = pos[0]
j = pos[1]
global cnt
if i-1 >= 0:
state_1 = copy.deepcopy(start)
state1 = up(state_1, i, j)
if compare(state1, goal):
print(cnt)
print(state1)
return 1
else:
if state1 not in q:
q.append(state1)
cnt += 1

if j+1 <= 2:
state_2 = copy.deepcopy(start)
state2 = right(state_2, i, j)
if compare(state2, goal):
print(cnt)
print(state2)
return 1
else:
if state2 not in q:
q.append(state2)
cnt += 1

if j-1 >= 0:
state_3 = copy.deepcopy(start)
state3 = left(state_3, i, j)
if compare(state3, goal):
print(cnt)
print(state3)
return 1
else:
if state3 not in q:
q.append(state3)
cnt += 1

if i+1 <= 2:
state_4 = copy.deepcopy(start)
state4 = down(state_4, i, j)
if compare(state4, goal):
print(cnt)
print(state4)
return 1

else:
if state4 not in q:
q.append(state4)
cnt += 1


def func():
if not compare(start_state, goal_state):
q.append(start_state)
i = 0
while(q):
spop = q[i]
i += 1
if(states(spop, goal_state)):
break


func()
42 changes: 42 additions & 0 deletions Python/Water jug 1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
x = 0
y = 0

state = [[0, 0]]

while(x != 2):
if [4, y] not in state and x < 4:
x = 4
state.append([x, y])
print(x, y)
if [x, 3] not in state and y < 3:
y = 3
state.append([x, y])
print(x, y)
if [0, y] not in state and x > 0:
x = 0
state.append([x, y])
print(x, y)
if [x, 0] not in state and y > 0:
y = 0
state.append([x, y])
print(x, y)
if [4, y-(4-x)] not in state and x+y >= 4 and y > 0:
y = y-(4-x)
x = 4
state.append([x, y])
print(x, y)
if [x-(3-y), 3] not in state and x+y >= 3 and x > 0:
x = x-(3-y)
y = 3
state.append([x, y])
print(x, y)
if [x+y, 0] not in state and (x+y) <= 4 and y >= 0:
x = x+y
y = 0
state.append([x, y])
print(x, y)
if [0, y+x] not in state and (x+y) <= 3 and x >= 0:
y = x+y
x = 0
state.append([x, y])
print(x, y)
38 changes: 38 additions & 0 deletions Python/travelling_salesman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from sys import maxsize
from itertools import permutations

V = 4


def travellingSalesmanProblem(graph, s):
vertex = []
for i in range(V):
if i != s:
vertex.append(i)
min_path = maxsize

all_perm = list(permutations(vertex))
all_perm.append(vertex)

for j in range(len(all_perm)):
current_pathweight = 0
k = s
vertex = all_perm[j]
for i in range(len(vertex)):
current_pathweight += graph[k][vertex[i]]
k = vertex[i]

current_pathweight += graph[k][s]

min_path = min(min_path, current_pathweight)

return min_path


graph = [[0, 10, 15, 20], [10, 0, 35, 25],
[15, 35, 0, 30], [20, 25, 30, 0]]
# s = int(input())
s = 1
print(travellingSalesmanProblem(graph, s))


25 changes: 25 additions & 0 deletions Python/water_jug2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
x = 12
y = 0
z = 0
state = [[12,0,0]]

while(x != 6):
if [x-(8-y), 8, z] not in state and x+y >= 8 and x > 0:
x = x-(8-y)
y = 8
state.append([x, y, z])
print(x, y, z)
if [x, y-(5-z), 5] not in state and y+z >= 5 and y > 0:
y = y-(5-z)
z = 5
state.append([x, y, z])
print(x, y, z)
if [x+z, y, 0] not in state and (x+z) <= 12 and z >= 0:
x = x+z
z = 0
state.append([x, y, z])
print(x, y, z)
if [x, 0, z] not in state and y > 0:
y = 0
state.append([x, y, z])
print(x, y, z)