pracai 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pracai-0.0.1/LICENSE +21 -0
- pracai-0.0.1/PKG-INFO +18 -0
- pracai-0.0.1/README.md +8 -0
- pracai-0.0.1/mypackage/A 8 puzzle.py +73 -0
- pracai-0.0.1/mypackage/A CitiesDistance.py +58 -0
- pracai-0.0.1/mypackage/A RobotNavigation.py +62 -0
- pracai-0.0.1/mypackage/BFS 8puzzle.py +69 -0
- pracai-0.0.1/mypackage/CitiesDistance.py +49 -0
- pracai-0.0.1/mypackage/CrossWord.py +92 -0
- pracai-0.0.1/mypackage/Cryptarithmetic.py +34 -0
- pracai-0.0.1/mypackage/HillClimbing8puzzle.py +73 -0
- pracai-0.0.1/mypackage/MapColouring.py +46 -0
- pracai-0.0.1/mypackage/MinimaxTicTacToe.py +95 -0
- pracai-0.0.1/mypackage/NQueens.py +58 -0
- pracai-0.0.1/mypackage/POS Tagging.py +24 -0
- pracai-0.0.1/mypackage/RobotNavigation.py +53 -0
- pracai-0.0.1/mypackage/SimilarityScore.py +33 -0
- pracai-0.0.1/mypackage/SpellChecker.py +34 -0
- pracai-0.0.1/mypackage/TicTacToe.py +59 -0
- pracai-0.0.1/mypackage/WaterJugBFS.py +45 -0
- pracai-0.0.1/mypackage/WaterJugDFS.py +61 -0
- pracai-0.0.1/mypackage/__init__.py +1 -0
- pracai-0.0.1/mypackage/cli.py +19 -0
- pracai-0.0.1/mypackage/magicSquare.py +39 -0
- pracai-0.0.1/pracai.egg-info/PKG-INFO +18 -0
- pracai-0.0.1/pracai.egg-info/SOURCES.txt +30 -0
- pracai-0.0.1/pracai.egg-info/dependency_links.txt +1 -0
- pracai-0.0.1/pracai.egg-info/entry_points.txt +2 -0
- pracai-0.0.1/pracai.egg-info/requires.txt +1 -0
- pracai-0.0.1/pracai.egg-info/top_level.txt +1 -0
- pracai-0.0.1/pyproject.toml +18 -0
- pracai-0.0.1/setup.cfg +4 -0
pracai-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
---
|
|
3
|
+
|
|
4
|
+
### `LICENSE`
|
|
5
|
+
|
|
6
|
+
```text id="vs1qjv"
|
|
7
|
+
MIT License
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2026 Arya
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software without restriction, including without limitation the rights
|
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
|
pracai-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pracai
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python package containing multiple utility scripts
|
|
5
|
+
Requires-Python: >=3.8
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: nltk
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# MyPackage
|
|
12
|
+
|
|
13
|
+
A Python package containing multiple utility scripts.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install mypackage
|
pracai-0.0.1/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import heapq
|
|
2
|
+
import copy
|
|
3
|
+
|
|
4
|
+
goal = [[1,2,3],
|
|
5
|
+
[4,5,6],
|
|
6
|
+
[7,8,0]]
|
|
7
|
+
|
|
8
|
+
# Heuristic: Manhattan Distance
|
|
9
|
+
def heuristic(state):
|
|
10
|
+
h = 0
|
|
11
|
+
for i in range(3):
|
|
12
|
+
for j in range(3):
|
|
13
|
+
val = state[i][j]
|
|
14
|
+
if val != 0:
|
|
15
|
+
goal_x = (val - 1) // 3
|
|
16
|
+
goal_y = (val - 1) % 3
|
|
17
|
+
h += abs(i - goal_x) + abs(j - goal_y)
|
|
18
|
+
return h
|
|
19
|
+
|
|
20
|
+
# Get neighbors
|
|
21
|
+
def get_neighbors(state):
|
|
22
|
+
neighbors = []
|
|
23
|
+
|
|
24
|
+
for i in range(3):
|
|
25
|
+
for j in range(3):
|
|
26
|
+
if state[i][j] == 0:
|
|
27
|
+
x, y = i, j
|
|
28
|
+
|
|
29
|
+
moves = [(0,1), (1,0), (0,-1), (-1,0)]
|
|
30
|
+
|
|
31
|
+
for dx, dy in moves:
|
|
32
|
+
nx, ny = x + dx, y + dy
|
|
33
|
+
if 0 <= nx < 3 and 0 <= ny < 3:
|
|
34
|
+
new_state = copy.deepcopy(state)
|
|
35
|
+
new_state[x][y], new_state[nx][ny] = new_state[nx][ny], new_state[x][y]
|
|
36
|
+
neighbors.append(new_state)
|
|
37
|
+
|
|
38
|
+
return neighbors
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def a_star(initial):
|
|
42
|
+
pq = []
|
|
43
|
+
heapq.heappush(pq, (heuristic(initial), 0, initial)) # (f, g, state)
|
|
44
|
+
|
|
45
|
+
visited = set()
|
|
46
|
+
|
|
47
|
+
while pq:
|
|
48
|
+
f, g, current = heapq.heappop(pq)
|
|
49
|
+
|
|
50
|
+
print("\nCurrent State:")
|
|
51
|
+
for row in current:
|
|
52
|
+
print(row)
|
|
53
|
+
|
|
54
|
+
if current == goal:
|
|
55
|
+
print("\nGoal Reached!")
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
visited.add(str(current))
|
|
59
|
+
|
|
60
|
+
for neighbor in get_neighbors(current):
|
|
61
|
+
if str(neighbor) not in visited:
|
|
62
|
+
new_g = g + 1
|
|
63
|
+
new_f = new_g + heuristic(neighbor)
|
|
64
|
+
heapq.heappush(pq, (new_f, new_g, neighbor))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# Initial state
|
|
68
|
+
initial = [[1,2,3],
|
|
69
|
+
[4,5,6],
|
|
70
|
+
[0,7,8]]
|
|
71
|
+
|
|
72
|
+
print("A* Algorithm for 8 Puzzle:")
|
|
73
|
+
a_star(initial)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import heapq
|
|
2
|
+
|
|
3
|
+
# Graph with distances
|
|
4
|
+
graph = {
|
|
5
|
+
'A': [('B', 1), ('C', 4)],
|
|
6
|
+
'B': [('A', 1), ('D', 2), ('E', 5)],
|
|
7
|
+
'C': [('A', 4), ('F', 3)],
|
|
8
|
+
'D': [('B', 2), ('G', 1)],
|
|
9
|
+
'E': [('B', 5), ('G', 2)],
|
|
10
|
+
'F': [('C', 3), ('G', 6)],
|
|
11
|
+
'G': []
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
# Heuristic values (estimated distance to goal G)
|
|
15
|
+
heuristic = {
|
|
16
|
+
'A': 6, 'B': 5, 'C': 4,
|
|
17
|
+
'D': 3, 'E': 2, 'F': 5, 'G': 0
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
def a_star(start, goal):
|
|
21
|
+
pq = []
|
|
22
|
+
heapq.heappush(pq, (heuristic[start], 0, start)) # (f, g, node)
|
|
23
|
+
|
|
24
|
+
visited = set()
|
|
25
|
+
parent = {start: None}
|
|
26
|
+
|
|
27
|
+
while pq:
|
|
28
|
+
f, g, current = heapq.heappop(pq)
|
|
29
|
+
|
|
30
|
+
if current in visited:
|
|
31
|
+
continue
|
|
32
|
+
visited.add(current)
|
|
33
|
+
|
|
34
|
+
# Goal check
|
|
35
|
+
if current == goal:
|
|
36
|
+
path = []
|
|
37
|
+
while current:
|
|
38
|
+
path.append(current)
|
|
39
|
+
current = parent[current]
|
|
40
|
+
|
|
41
|
+
print("Shortest Path:", " -> ".join(path[::-1]))
|
|
42
|
+
print("Total Cost:", g)
|
|
43
|
+
return
|
|
44
|
+
|
|
45
|
+
# Explore neighbors
|
|
46
|
+
for neighbor, cost in graph[current]:
|
|
47
|
+
if neighbor not in visited:
|
|
48
|
+
parent[neighbor] = current
|
|
49
|
+
new_g = g + cost
|
|
50
|
+
new_f = new_g + heuristic[neighbor]
|
|
51
|
+
heapq.heappush(pq, (new_f, new_g, neighbor))
|
|
52
|
+
|
|
53
|
+
print("No path found")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
# Run
|
|
57
|
+
print("A* for Cities Distance:")
|
|
58
|
+
a_star('A', 'G')
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import heapq
|
|
2
|
+
|
|
3
|
+
def a_star(grid, start, goal):
|
|
4
|
+
|
|
5
|
+
# Heuristic (Manhattan distance)
|
|
6
|
+
def h(pos):
|
|
7
|
+
return abs(pos[0] - goal[0]) + abs(pos[1] - goal[1])
|
|
8
|
+
|
|
9
|
+
pq = []
|
|
10
|
+
heapq.heappush(pq, (h(start), 0, start)) # (f, g, position)
|
|
11
|
+
|
|
12
|
+
visited = set()
|
|
13
|
+
parent = {start: None}
|
|
14
|
+
|
|
15
|
+
while pq:
|
|
16
|
+
f, g, current = heapq.heappop(pq)
|
|
17
|
+
|
|
18
|
+
# Goal check
|
|
19
|
+
if current == goal:
|
|
20
|
+
path = []
|
|
21
|
+
while current:
|
|
22
|
+
path.append(current)
|
|
23
|
+
current = parent[current]
|
|
24
|
+
print("Path:", path[::-1])
|
|
25
|
+
print("Cost:", g)
|
|
26
|
+
return
|
|
27
|
+
|
|
28
|
+
if current in visited:
|
|
29
|
+
continue
|
|
30
|
+
visited.add(current)
|
|
31
|
+
|
|
32
|
+
x, y = current
|
|
33
|
+
|
|
34
|
+
# Possible moves
|
|
35
|
+
for dx, dy in [(0,1), (1,0), (0,-1), (-1,0)]:
|
|
36
|
+
nx, ny = x + dx, y + dy
|
|
37
|
+
|
|
38
|
+
# Check valid move
|
|
39
|
+
if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and grid[nx][ny] == 0:
|
|
40
|
+
if (nx, ny) not in visited:
|
|
41
|
+
new_g = g + 1
|
|
42
|
+
new_f = new_g + h((nx, ny))
|
|
43
|
+
|
|
44
|
+
parent[(nx, ny)] = current
|
|
45
|
+
heapq.heappush(pq, (new_f, new_g, (nx, ny)))
|
|
46
|
+
|
|
47
|
+
print("No path found")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Example grid
|
|
51
|
+
grid = [
|
|
52
|
+
[0, 0, 1, 0],
|
|
53
|
+
[0, 0, 1, 0],
|
|
54
|
+
[0, 0, 0, 0],
|
|
55
|
+
[0, 1, 1, 0]
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
start = (0, 0)
|
|
59
|
+
goal = (3, 3)
|
|
60
|
+
|
|
61
|
+
print("A* Robot Navigation:")
|
|
62
|
+
a_star(grid, start, goal)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import heapq
|
|
2
|
+
import copy
|
|
3
|
+
|
|
4
|
+
goal = [[1,2,3],
|
|
5
|
+
[4,5,6],
|
|
6
|
+
[7,8,0]]
|
|
7
|
+
|
|
8
|
+
# Heuristic function (misplaced tiles)
|
|
9
|
+
def heuristic(state):
|
|
10
|
+
h = 0
|
|
11
|
+
for i in range(3):
|
|
12
|
+
for j in range(3):
|
|
13
|
+
if state[i][j] != 0 and state[i][j] != goal[i][j]:
|
|
14
|
+
h += 1
|
|
15
|
+
return h
|
|
16
|
+
|
|
17
|
+
# Get neighbors
|
|
18
|
+
def get_neighbors(state):
|
|
19
|
+
neighbors = []
|
|
20
|
+
|
|
21
|
+
for i in range(3):
|
|
22
|
+
for j in range(3):
|
|
23
|
+
if state[i][j] == 0:
|
|
24
|
+
x, y = i, j
|
|
25
|
+
|
|
26
|
+
moves = [(0,1), (1,0), (0,-1), (-1,0)]
|
|
27
|
+
|
|
28
|
+
for dx, dy in moves:
|
|
29
|
+
nx, ny = x + dx, y + dy
|
|
30
|
+
if 0 <= nx < 3 and 0 <= ny < 3:
|
|
31
|
+
new_state = copy.deepcopy(state)
|
|
32
|
+
new_state[x][y], new_state[nx][ny] = new_state[nx][ny], new_state[x][y]
|
|
33
|
+
neighbors.append(new_state)
|
|
34
|
+
|
|
35
|
+
return neighbors
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def best_first_search(initial):
|
|
39
|
+
visited = set()
|
|
40
|
+
pq = []
|
|
41
|
+
|
|
42
|
+
# (heuristic, state)
|
|
43
|
+
heapq.heappush(pq, (heuristic(initial), initial))
|
|
44
|
+
|
|
45
|
+
while pq:
|
|
46
|
+
h, current = heapq.heappop(pq)
|
|
47
|
+
|
|
48
|
+
print("\nCurrent State:")
|
|
49
|
+
for row in current:
|
|
50
|
+
print(row)
|
|
51
|
+
|
|
52
|
+
if current == goal:
|
|
53
|
+
print("\nGoal Reached!")
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
visited.add(str(current))
|
|
57
|
+
|
|
58
|
+
for neighbor in get_neighbors(current):
|
|
59
|
+
if str(neighbor) not in visited:
|
|
60
|
+
heapq.heappush(pq, (heuristic(neighbor), neighbor))
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# Initial state
|
|
64
|
+
initial = [[1,2,3],
|
|
65
|
+
[4,5,6],
|
|
66
|
+
[0,7,8]]
|
|
67
|
+
|
|
68
|
+
print("Best First Search for 8 Puzzle:")
|
|
69
|
+
best_first_search(initial)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import heapq
|
|
2
|
+
|
|
3
|
+
# Graph (city connections with distances)
|
|
4
|
+
graph = {
|
|
5
|
+
'A': [('B', 1), ('C', 4)],
|
|
6
|
+
'B': [('A', 1), ('D', 2), ('E', 5)],
|
|
7
|
+
'C': [('A', 4), ('F', 3)],
|
|
8
|
+
'D': [('B', 2), ('G', 1)],
|
|
9
|
+
'E': [('B', 5), ('G', 2)],
|
|
10
|
+
'F': [('C', 3), ('G', 6)],
|
|
11
|
+
'G': []
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
# Heuristic values (estimated distance to goal G)
|
|
15
|
+
heuristic = {
|
|
16
|
+
'A': 6, 'B': 5, 'C': 4,
|
|
17
|
+
'D': 3, 'E': 2, 'F': 5, 'G': 0
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
def best_first_search(start, goal):
|
|
21
|
+
pq = [(heuristic[start], start)] # (h, city)
|
|
22
|
+
visited = set()
|
|
23
|
+
parent = {start: None}
|
|
24
|
+
|
|
25
|
+
while pq:
|
|
26
|
+
_, current = heapq.heappop(pq)
|
|
27
|
+
|
|
28
|
+
# Goal check
|
|
29
|
+
if current == goal:
|
|
30
|
+
path = []
|
|
31
|
+
while current:
|
|
32
|
+
path.append(current)
|
|
33
|
+
current = parent[current]
|
|
34
|
+
print("Path:", " -> ".join(path[::-1]))
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
visited.add(current)
|
|
38
|
+
|
|
39
|
+
for neighbor, _ in graph[current]:
|
|
40
|
+
if neighbor not in visited:
|
|
41
|
+
parent[neighbor] = current
|
|
42
|
+
heapq.heappush(pq, (heuristic[neighbor], neighbor))
|
|
43
|
+
|
|
44
|
+
print("No path found")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# Run
|
|
48
|
+
print("Best First Search (Cities Distance):")
|
|
49
|
+
best_first_search('A', 'G')
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
def print_grid(grid):
|
|
2
|
+
for row in grid:
|
|
3
|
+
print("".join(row))
|
|
4
|
+
print()
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def can_place_h(grid, word, row, col):
|
|
8
|
+
if col + len(word) > len(grid[0]):
|
|
9
|
+
return False
|
|
10
|
+
for i in range(len(word)):
|
|
11
|
+
if grid[row][col + i] != '-' and grid[row][col + i] != word[i]:
|
|
12
|
+
return False
|
|
13
|
+
return True
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def can_place_v(grid, word, row, col):
|
|
17
|
+
if row + len(word) > len(grid):
|
|
18
|
+
return False
|
|
19
|
+
for i in range(len(word)):
|
|
20
|
+
if grid[row + i][col] != '-' and grid[row + i][col] != word[i]:
|
|
21
|
+
return False
|
|
22
|
+
return True
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def place_h(grid, word, row, col):
|
|
26
|
+
temp = []
|
|
27
|
+
for i in range(len(word)):
|
|
28
|
+
temp.append(grid[row][col + i])
|
|
29
|
+
grid[row][col + i] = word[i]
|
|
30
|
+
return temp
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def place_v(grid, word, row, col):
|
|
34
|
+
temp = []
|
|
35
|
+
for i in range(len(word)):
|
|
36
|
+
temp.append(grid[row + i][col])
|
|
37
|
+
grid[row + i][col] = word[i]
|
|
38
|
+
return temp
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def unplace_h(grid, temp, row, col):
|
|
42
|
+
for i in range(len(temp)):
|
|
43
|
+
grid[row][col + i] = temp[i]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def unplace_v(grid, temp, row, col):
|
|
47
|
+
for i in range(len(temp)):
|
|
48
|
+
grid[row + i][col] = temp[i]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def solve_crossword(grid, words):
|
|
52
|
+
if not words:
|
|
53
|
+
return True
|
|
54
|
+
|
|
55
|
+
word = words[0]
|
|
56
|
+
|
|
57
|
+
for i in range(len(grid)):
|
|
58
|
+
for j in range(len(grid[0])):
|
|
59
|
+
|
|
60
|
+
# Horizontal
|
|
61
|
+
if can_place_h(grid, word, i, j):
|
|
62
|
+
temp = place_h(grid, word, i, j)
|
|
63
|
+
if solve_crossword(grid, words[1:]):
|
|
64
|
+
return True
|
|
65
|
+
unplace_h(grid, temp, i, j)
|
|
66
|
+
|
|
67
|
+
# Vertical
|
|
68
|
+
if can_place_v(grid, word, i, j):
|
|
69
|
+
temp = place_v(grid, word, i, j)
|
|
70
|
+
if solve_crossword(grid, words[1:]):
|
|
71
|
+
return True
|
|
72
|
+
unplace_v(grid, temp, i, j)
|
|
73
|
+
|
|
74
|
+
return False
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# Grid
|
|
78
|
+
grid = [
|
|
79
|
+
['+', '-', '+', '+', '+'],
|
|
80
|
+
['+', '-', '-', '-', '+'],
|
|
81
|
+
['+', '-', '+', '+', '+'],
|
|
82
|
+
['+', '+', '+', '+', '+']
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
words = ["HI", "IT"]
|
|
86
|
+
|
|
87
|
+
print("Solving Crossword...\n")
|
|
88
|
+
|
|
89
|
+
if solve_crossword(grid, words):
|
|
90
|
+
print_grid(grid)
|
|
91
|
+
else:
|
|
92
|
+
print("No solution found")
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
|
|
3
|
+
def solve_cryptarithmetic():
|
|
4
|
+
letters = "SENDMORY"
|
|
5
|
+
|
|
6
|
+
print("Solving SEND + MORE = MONEY...\n")
|
|
7
|
+
|
|
8
|
+
# Try all permutations of digits
|
|
9
|
+
for perm in itertools.permutations(range(10), len(letters)):
|
|
10
|
+
s, e, n, d, m, o, r, y = perm
|
|
11
|
+
|
|
12
|
+
# Leading digit cannot be 0
|
|
13
|
+
if s == 0 or m == 0:
|
|
14
|
+
continue
|
|
15
|
+
|
|
16
|
+
# Form numbers
|
|
17
|
+
send = s*1000 + e*100 + n*10 + d
|
|
18
|
+
more = m*1000 + o*100 + r*10 + e
|
|
19
|
+
money = m*10000 + o*1000 + n*100 + e*10 + y
|
|
20
|
+
|
|
21
|
+
# Check condition
|
|
22
|
+
if send + more == money:
|
|
23
|
+
print("Solution Found!\n")
|
|
24
|
+
print(" SEND =", send)
|
|
25
|
+
print(" MORE =", more)
|
|
26
|
+
print("MONEY =", money)
|
|
27
|
+
print("\nMapping:")
|
|
28
|
+
print(dict(zip(letters, perm)))
|
|
29
|
+
return
|
|
30
|
+
|
|
31
|
+
print("No solution found")
|
|
32
|
+
|
|
33
|
+
# Run
|
|
34
|
+
solve_cryptarithmetic()
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
|
|
3
|
+
# Heuristic: count misplaced tiles
|
|
4
|
+
def heuristic(state, goal):
|
|
5
|
+
h = 0
|
|
6
|
+
for i in range(3):
|
|
7
|
+
for j in range(3):
|
|
8
|
+
if state[i][j] != 0 and state[i][j] != goal[i][j]:
|
|
9
|
+
h += 1
|
|
10
|
+
return h
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Get neighbors by moving blank (0)
|
|
14
|
+
def get_neighbors(state):
|
|
15
|
+
neighbors = []
|
|
16
|
+
|
|
17
|
+
# Find position of 0
|
|
18
|
+
for i in range(3):
|
|
19
|
+
for j in range(3):
|
|
20
|
+
if state[i][j] == 0:
|
|
21
|
+
x, y = i, j
|
|
22
|
+
|
|
23
|
+
# Possible moves
|
|
24
|
+
moves = [(0,1), (1,0), (0,-1), (-1,0)]
|
|
25
|
+
|
|
26
|
+
for dx, dy in moves:
|
|
27
|
+
nx, ny = x + dx, y + dy
|
|
28
|
+
|
|
29
|
+
if 0 <= nx < 3 and 0 <= ny < 3:
|
|
30
|
+
new_state = copy.deepcopy(state)
|
|
31
|
+
new_state[x][y], new_state[nx][ny] = new_state[nx][ny], new_state[x][y]
|
|
32
|
+
neighbors.append(new_state)
|
|
33
|
+
|
|
34
|
+
return neighbors
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def hill_climbing(initial, goal):
|
|
38
|
+
current = initial
|
|
39
|
+
|
|
40
|
+
while True:
|
|
41
|
+
print("\nCurrent State:")
|
|
42
|
+
for row in current:
|
|
43
|
+
print(row)
|
|
44
|
+
|
|
45
|
+
# Check goal
|
|
46
|
+
if current == goal:
|
|
47
|
+
print("\nGoal Reached!")
|
|
48
|
+
return
|
|
49
|
+
|
|
50
|
+
neighbors = get_neighbors(current)
|
|
51
|
+
|
|
52
|
+
# Choose best neighbor
|
|
53
|
+
next_state = min(neighbors, key=lambda x: heuristic(x, goal))
|
|
54
|
+
|
|
55
|
+
# If no improvement → stop
|
|
56
|
+
if heuristic(next_state, goal) >= heuristic(current, goal):
|
|
57
|
+
print("\nReached Local Optimum (stuck)")
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
current = next_state
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# Initial and Goal
|
|
64
|
+
initial = [[1,2,3],
|
|
65
|
+
[4,5,6],
|
|
66
|
+
[0,7,8]]
|
|
67
|
+
|
|
68
|
+
goal = [[1,2,3],
|
|
69
|
+
[4,5,6],
|
|
70
|
+
[7,8,0]]
|
|
71
|
+
|
|
72
|
+
print("Hill Climbing for 8 Puzzle:")
|
|
73
|
+
hill_climbing(initial, goal)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
def is_safe(graph, color, node, c):
|
|
2
|
+
for i in range(len(graph)):
|
|
3
|
+
if graph[node][i] == 1 and color[i] == c:
|
|
4
|
+
return False
|
|
5
|
+
return True
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def solve(graph, m, color, node):
|
|
9
|
+
# If all nodes colored
|
|
10
|
+
if node == len(graph):
|
|
11
|
+
return True
|
|
12
|
+
|
|
13
|
+
for c in range(1, m + 1):
|
|
14
|
+
if is_safe(graph, color, node, c):
|
|
15
|
+
color[node] = c
|
|
16
|
+
|
|
17
|
+
if solve(graph, m, color, node + 1):
|
|
18
|
+
return True
|
|
19
|
+
|
|
20
|
+
color[node] = 0 # backtrack
|
|
21
|
+
|
|
22
|
+
return False
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def map_coloring():
|
|
26
|
+
# Adjacency matrix
|
|
27
|
+
graph = [
|
|
28
|
+
[0, 1, 1, 1],
|
|
29
|
+
[1, 0, 1, 0],
|
|
30
|
+
[1, 1, 0, 1],
|
|
31
|
+
[1, 0, 1, 0]
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
m = 3 # number of colors
|
|
35
|
+
color = [0] * len(graph)
|
|
36
|
+
|
|
37
|
+
if solve(graph, m, color, 0):
|
|
38
|
+
print("Color assignment:")
|
|
39
|
+
for i in range(len(color)):
|
|
40
|
+
print(f"Region {i} -> Color {color[i]}")
|
|
41
|
+
else:
|
|
42
|
+
print("No solution exists")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# Run
|
|
46
|
+
map_coloring()
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
def print_board(board):
|
|
4
|
+
for row in board:
|
|
5
|
+
print(" | ".join(row))
|
|
6
|
+
print("-" * 9)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def check_winner(board):
|
|
10
|
+
# Rows
|
|
11
|
+
for row in board:
|
|
12
|
+
if row[0] == row[1] == row[2] and row[0] != " ":
|
|
13
|
+
return row[0]
|
|
14
|
+
|
|
15
|
+
# Columns
|
|
16
|
+
for col in range(3):
|
|
17
|
+
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != " ":
|
|
18
|
+
return board[0][col]
|
|
19
|
+
|
|
20
|
+
# Diagonals
|
|
21
|
+
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != " ":
|
|
22
|
+
return board[0][0]
|
|
23
|
+
|
|
24
|
+
if board[0][2] == board[1][1] == board[2][0] and board[0][2] != " ":
|
|
25
|
+
return board[0][2]
|
|
26
|
+
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def is_full(board):
|
|
31
|
+
return all(cell != " " for row in board for cell in row)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def minimax(board, is_maximizing):
|
|
35
|
+
winner = check_winner(board)
|
|
36
|
+
|
|
37
|
+
if winner == 'X':
|
|
38
|
+
return 1
|
|
39
|
+
if winner == 'O':
|
|
40
|
+
return -1
|
|
41
|
+
if is_full(board):
|
|
42
|
+
return 0
|
|
43
|
+
|
|
44
|
+
if is_maximizing:
|
|
45
|
+
best = -math.inf
|
|
46
|
+
for i in range(3):
|
|
47
|
+
for j in range(3):
|
|
48
|
+
if board[i][j] == " ":
|
|
49
|
+
board[i][j] = 'X'
|
|
50
|
+
score = minimax(board, False)
|
|
51
|
+
board[i][j] = " "
|
|
52
|
+
best = max(best, score)
|
|
53
|
+
return best
|
|
54
|
+
else:
|
|
55
|
+
best = math.inf
|
|
56
|
+
for i in range(3):
|
|
57
|
+
for j in range(3):
|
|
58
|
+
if board[i][j] == " ":
|
|
59
|
+
board[i][j] = 'O'
|
|
60
|
+
score = minimax(board, True)
|
|
61
|
+
board[i][j] = " "
|
|
62
|
+
best = min(best, score)
|
|
63
|
+
return best
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def best_move(board):
|
|
67
|
+
best_score = -math.inf
|
|
68
|
+
move = None
|
|
69
|
+
|
|
70
|
+
for i in range(3):
|
|
71
|
+
for j in range(3):
|
|
72
|
+
if board[i][j] == " ":
|
|
73
|
+
board[i][j] = 'X'
|
|
74
|
+
score = minimax(board, False)
|
|
75
|
+
board[i][j] = " "
|
|
76
|
+
|
|
77
|
+
if score > best_score:
|
|
78
|
+
best_score = score
|
|
79
|
+
move = (i, j)
|
|
80
|
+
|
|
81
|
+
return move
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# Example board
|
|
85
|
+
board = [
|
|
86
|
+
['O', 'X', ' '],
|
|
87
|
+
[' ', 'X', ' '],
|
|
88
|
+
[' ', ' ', 'O']
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
print("Current Board:")
|
|
92
|
+
print_board(board)
|
|
93
|
+
|
|
94
|
+
move = best_move(board)
|
|
95
|
+
print("Best move for X:", move)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# N-Queens - Non AI (Backtracking)
|
|
2
|
+
|
|
3
|
+
def print_board(board, n):
|
|
4
|
+
for i in range(n):
|
|
5
|
+
for j in range(n):
|
|
6
|
+
print(board[i][j], end=" ")
|
|
7
|
+
print()
|
|
8
|
+
print()
|
|
9
|
+
|
|
10
|
+
def is_safe(board, row, col, n):
|
|
11
|
+
# Check column
|
|
12
|
+
for i in range(row):
|
|
13
|
+
if board[i][col] == 'Q':
|
|
14
|
+
return False
|
|
15
|
+
|
|
16
|
+
# Check left diagonal
|
|
17
|
+
i, j = row-1, col-1
|
|
18
|
+
while i >= 0 and j >= 0:
|
|
19
|
+
if board[i][j] == 'Q':
|
|
20
|
+
return False
|
|
21
|
+
i -= 1
|
|
22
|
+
j -= 1
|
|
23
|
+
|
|
24
|
+
# Check right diagonal
|
|
25
|
+
i, j = row-1, col+1
|
|
26
|
+
while i >= 0 and j < n:
|
|
27
|
+
if board[i][j] == 'Q':
|
|
28
|
+
return False
|
|
29
|
+
i -= 1
|
|
30
|
+
j += 1
|
|
31
|
+
|
|
32
|
+
return True
|
|
33
|
+
|
|
34
|
+
def solve_n_queens(board, row, n):
|
|
35
|
+
if row == n:
|
|
36
|
+
print("Solution:")
|
|
37
|
+
print_board(board, n)
|
|
38
|
+
return True
|
|
39
|
+
|
|
40
|
+
for col in range(n):
|
|
41
|
+
if is_safe(board, row, col, n):
|
|
42
|
+
board[row][col] = 'Q'
|
|
43
|
+
|
|
44
|
+
if solve_n_queens(board, row + 1, n):
|
|
45
|
+
return True
|
|
46
|
+
|
|
47
|
+
board[row][col] = '.' # backtrack
|
|
48
|
+
|
|
49
|
+
return False
|
|
50
|
+
|
|
51
|
+
def main():
|
|
52
|
+
n = int(input("Enter value of N: "))
|
|
53
|
+
board = [['.' for _ in range(n)] for _ in range(n)]
|
|
54
|
+
|
|
55
|
+
if not solve_n_queens(board, 0, n):
|
|
56
|
+
print("No solution exists")
|
|
57
|
+
|
|
58
|
+
main()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import nltk
|
|
2
|
+
|
|
3
|
+
def pos_tagging():
|
|
4
|
+
text = input("Enter a sentence: ")
|
|
5
|
+
|
|
6
|
+
# Tokenize words
|
|
7
|
+
words = nltk.word_tokenize(text)
|
|
8
|
+
|
|
9
|
+
# POS tagging
|
|
10
|
+
tags = nltk.pos_tag(words)
|
|
11
|
+
|
|
12
|
+
print("\nPOS Tags:")
|
|
13
|
+
for word, tag in tags:
|
|
14
|
+
print(word, "->", tag)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# Download required data (only first time)
|
|
18
|
+
nltk.download('punkt')
|
|
19
|
+
nltk.download('punkt_tab')
|
|
20
|
+
nltk.download('averaged_perceptron_tagger')
|
|
21
|
+
nltk.download('averaged_perceptron_tagger_eng')
|
|
22
|
+
|
|
23
|
+
# Run
|
|
24
|
+
pos_tagging()
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import heapq
|
|
2
|
+
|
|
3
|
+
def best_first_search(grid, start, goal):
|
|
4
|
+
|
|
5
|
+
# Heuristic function (Manhattan Distance)
|
|
6
|
+
def h(pos):
|
|
7
|
+
return abs(pos[0] - goal[0]) + abs(pos[1] - goal[1])
|
|
8
|
+
|
|
9
|
+
pq = [(h(start), start)]
|
|
10
|
+
visited = set([start])
|
|
11
|
+
parent = {start: None}
|
|
12
|
+
|
|
13
|
+
while pq:
|
|
14
|
+
_, current = heapq.heappop(pq)
|
|
15
|
+
|
|
16
|
+
# Goal check
|
|
17
|
+
if current == goal:
|
|
18
|
+
path = []
|
|
19
|
+
while current:
|
|
20
|
+
path.append(current)
|
|
21
|
+
current = parent[current]
|
|
22
|
+
print("Path:", path[::-1])
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
x, y = current
|
|
26
|
+
|
|
27
|
+
# Possible moves: right, down, left, up
|
|
28
|
+
for dx, dy in [(0,1), (1,0), (0,-1), (-1,0)]:
|
|
29
|
+
nx, ny = x + dx, y + dy
|
|
30
|
+
|
|
31
|
+
# Check valid move
|
|
32
|
+
if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and grid[nx][ny] == 0:
|
|
33
|
+
if (nx, ny) not in visited:
|
|
34
|
+
visited.add((nx, ny))
|
|
35
|
+
parent[(nx, ny)] = current
|
|
36
|
+
heapq.heappush(pq, (h((nx, ny)), (nx, ny)))
|
|
37
|
+
|
|
38
|
+
print("No path found")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# Example grid
|
|
42
|
+
grid = [
|
|
43
|
+
[0, 0, 1, 0],
|
|
44
|
+
[0, 0, 1, 0],
|
|
45
|
+
[0, 0, 0, 0],
|
|
46
|
+
[0, 1, 1, 0]
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
start = (0, 0)
|
|
50
|
+
goal = (3, 3)
|
|
51
|
+
|
|
52
|
+
print("Robot Navigation using Best First Search:")
|
|
53
|
+
best_first_search(grid, start, goal)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from collections import Counter
|
|
2
|
+
import math
|
|
3
|
+
|
|
4
|
+
def cosine_similarity(text1, text2):
|
|
5
|
+
# Convert to word frequency
|
|
6
|
+
vec1 = Counter(text1.lower().split())
|
|
7
|
+
vec2 = Counter(text2.lower().split())
|
|
8
|
+
|
|
9
|
+
# Common words
|
|
10
|
+
intersection = set(vec1.keys()) & set(vec2.keys())
|
|
11
|
+
|
|
12
|
+
# Numerator
|
|
13
|
+
numerator = sum(vec1[x] * vec2[x] for x in intersection)
|
|
14
|
+
|
|
15
|
+
# Denominator
|
|
16
|
+
sum1 = sum(v**2 for v in vec1.values())
|
|
17
|
+
sum2 = sum(v**2 for v in vec2.values())
|
|
18
|
+
|
|
19
|
+
denominator = math.sqrt(sum1) * math.sqrt(sum2)
|
|
20
|
+
|
|
21
|
+
if denominator == 0:
|
|
22
|
+
return 0.0
|
|
23
|
+
|
|
24
|
+
return numerator / denominator
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Input
|
|
28
|
+
t1 = input("Enter first sentence: ")
|
|
29
|
+
t2 = input("Enter second sentence: ")
|
|
30
|
+
|
|
31
|
+
score = cosine_similarity(t1, t2)
|
|
32
|
+
|
|
33
|
+
print("\nSimilarity Score:", round(score, 4))
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
def levenshtein_distance(s1, s2):
|
|
2
|
+
if len(s1) < len(s2):
|
|
3
|
+
return levenshtein_distance(s2, s1)
|
|
4
|
+
|
|
5
|
+
if len(s2) == 0:
|
|
6
|
+
return len(s1)
|
|
7
|
+
|
|
8
|
+
previous_row = range(len(s2) + 1)
|
|
9
|
+
|
|
10
|
+
for i, c1 in enumerate(s1):
|
|
11
|
+
current_row = [i + 1]
|
|
12
|
+
|
|
13
|
+
for j, c2 in enumerate(s2):
|
|
14
|
+
insertions = previous_row[j + 1] + 1
|
|
15
|
+
deletions = current_row[j] + 1
|
|
16
|
+
substitutions = previous_row[j] + (c1 != c2)
|
|
17
|
+
|
|
18
|
+
current_row.append(min(insertions, deletions, substitutions))
|
|
19
|
+
|
|
20
|
+
previous_row = current_row
|
|
21
|
+
|
|
22
|
+
return previous_row[-1]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# Dictionary
|
|
26
|
+
dictionary = ["apple", "banana", "orange", "grape", "intelligence", "artificial"]
|
|
27
|
+
|
|
28
|
+
# Input
|
|
29
|
+
word = input("Enter word: ")
|
|
30
|
+
|
|
31
|
+
# Find closest word
|
|
32
|
+
closest = min(dictionary, key=lambda w: levenshtein_distance(word, w))
|
|
33
|
+
|
|
34
|
+
print("\nDid you mean:", closest)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Tic Tac Toe - Non AI (Simple Version)
|
|
2
|
+
|
|
3
|
+
board = [" " for _ in range(9)]
|
|
4
|
+
|
|
5
|
+
def print_board():
|
|
6
|
+
print()
|
|
7
|
+
print(board[0], "|", board[1], "|", board[2])
|
|
8
|
+
print("--+---+--")
|
|
9
|
+
print(board[3], "|", board[4], "|", board[5])
|
|
10
|
+
print("--+---+--")
|
|
11
|
+
print(board[6], "|", board[7], "|", board[8])
|
|
12
|
+
print()
|
|
13
|
+
|
|
14
|
+
def check_winner(player):
|
|
15
|
+
win_positions = [
|
|
16
|
+
[0,1,2], [3,4,5], [6,7,8], # rows
|
|
17
|
+
[0,3,6], [1,4,7], [2,5,8], # columns
|
|
18
|
+
[0,4,8], [2,4,6] # diagonals
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
for pos in win_positions:
|
|
22
|
+
if board[pos[0]] == player and \
|
|
23
|
+
board[pos[1]] == player and \
|
|
24
|
+
board[pos[2]] == player:
|
|
25
|
+
return True
|
|
26
|
+
return False
|
|
27
|
+
|
|
28
|
+
def play_game():
|
|
29
|
+
player = "X"
|
|
30
|
+
|
|
31
|
+
for turn in range(9):
|
|
32
|
+
print_board()
|
|
33
|
+
move = int(input(f"Player {player}, enter position (0-8): "))
|
|
34
|
+
|
|
35
|
+
if move < 0 or move > 8:
|
|
36
|
+
print("Invalid input! Try again.")
|
|
37
|
+
continue
|
|
38
|
+
|
|
39
|
+
if board[move] != " ":
|
|
40
|
+
print("Position already taken!")
|
|
41
|
+
continue
|
|
42
|
+
|
|
43
|
+
board[move] = player
|
|
44
|
+
|
|
45
|
+
if check_winner(player):
|
|
46
|
+
print_board()
|
|
47
|
+
print(f"Player {player} wins!")
|
|
48
|
+
return
|
|
49
|
+
|
|
50
|
+
# switch player
|
|
51
|
+
if player == "X":
|
|
52
|
+
player = "O"
|
|
53
|
+
else:
|
|
54
|
+
player = "X"
|
|
55
|
+
|
|
56
|
+
print_board()
|
|
57
|
+
print("Match Draw!")
|
|
58
|
+
|
|
59
|
+
play_game()
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from collections import deque
|
|
2
|
+
|
|
3
|
+
def water_jug_bfs(cap1, cap2, target):
|
|
4
|
+
visited = set()
|
|
5
|
+
queue = deque([((0, 0), [])]) # (state, path)
|
|
6
|
+
|
|
7
|
+
while queue:
|
|
8
|
+
(jug1, jug2), path = queue.popleft()
|
|
9
|
+
|
|
10
|
+
# If already visited, skip
|
|
11
|
+
if (jug1, jug2) in visited:
|
|
12
|
+
continue
|
|
13
|
+
|
|
14
|
+
visited.add((jug1, jug2))
|
|
15
|
+
path = path + [(jug1, jug2)]
|
|
16
|
+
|
|
17
|
+
# Goal condition
|
|
18
|
+
if jug1 == target or jug2 == target:
|
|
19
|
+
print("Solution Path:")
|
|
20
|
+
for state in path:
|
|
21
|
+
print(state)
|
|
22
|
+
return
|
|
23
|
+
|
|
24
|
+
# All possible operations
|
|
25
|
+
next_states = [
|
|
26
|
+
(cap1, jug2), # Fill jug1
|
|
27
|
+
(jug1, cap2), # Fill jug2
|
|
28
|
+
(0, jug2), # Empty jug1
|
|
29
|
+
(jug1, 0), # Empty jug2
|
|
30
|
+
# Pour jug1 -> jug2
|
|
31
|
+
(jug1 - min(jug1, cap2 - jug2), jug2 + min(jug1, cap2 - jug2)),
|
|
32
|
+
# Pour jug2 -> jug1
|
|
33
|
+
(jug1 + min(jug2, cap1 - jug1), jug2 - min(jug2, cap1 - jug1))
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
# Add new states to queue
|
|
37
|
+
for state in next_states:
|
|
38
|
+
if state not in visited:
|
|
39
|
+
queue.append((state, path))
|
|
40
|
+
|
|
41
|
+
print("No solution found")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Example
|
|
45
|
+
water_jug_bfs(3, 5, 4)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Water Jug Problem using DFS
|
|
2
|
+
|
|
3
|
+
def dfs(jug1, jug2, visited, path, cap1, cap2, target):
|
|
4
|
+
|
|
5
|
+
# If already visited, stop
|
|
6
|
+
if (jug1, jug2) in visited:
|
|
7
|
+
return False
|
|
8
|
+
|
|
9
|
+
visited.add((jug1, jug2))
|
|
10
|
+
path.append((jug1, jug2))
|
|
11
|
+
|
|
12
|
+
# Goal condition
|
|
13
|
+
if jug1 == target or jug2 == target:
|
|
14
|
+
return True
|
|
15
|
+
|
|
16
|
+
# All possible operations
|
|
17
|
+
|
|
18
|
+
# Fill jug1
|
|
19
|
+
if dfs(cap1, jug2, visited, path, cap1, cap2, target):
|
|
20
|
+
return True
|
|
21
|
+
|
|
22
|
+
# Fill jug2
|
|
23
|
+
if dfs(jug1, cap2, visited, path, cap1, cap2, target):
|
|
24
|
+
return True
|
|
25
|
+
|
|
26
|
+
# Empty jug1
|
|
27
|
+
if dfs(0, jug2, visited, path, cap1, cap2, target):
|
|
28
|
+
return True
|
|
29
|
+
|
|
30
|
+
# Empty jug2
|
|
31
|
+
if dfs(jug1, 0, visited, path, cap1, cap2, target):
|
|
32
|
+
return True
|
|
33
|
+
|
|
34
|
+
# Pour jug1 → jug2
|
|
35
|
+
pour = min(jug1, cap2 - jug2)
|
|
36
|
+
if dfs(jug1 - pour, jug2 + pour, visited, path, cap1, cap2, target):
|
|
37
|
+
return True
|
|
38
|
+
|
|
39
|
+
# Pour jug2 → jug1
|
|
40
|
+
pour = min(jug2, cap1 - jug1)
|
|
41
|
+
if dfs(jug1 + pour, jug2 - pour, visited, path, cap1, cap2, target):
|
|
42
|
+
return True
|
|
43
|
+
|
|
44
|
+
path.pop() # backtrack
|
|
45
|
+
return False
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def water_jug_dfs(cap1, cap2, target):
|
|
49
|
+
visited = set()
|
|
50
|
+
path = []
|
|
51
|
+
|
|
52
|
+
if dfs(0, 0, visited, path, cap1, cap2, target):
|
|
53
|
+
print("Solution Path:")
|
|
54
|
+
for state in path:
|
|
55
|
+
print(state)
|
|
56
|
+
else:
|
|
57
|
+
print("No solution")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# Example
|
|
61
|
+
water_jug_dfs(3, 5, 4)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# empty
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
def main():
|
|
4
|
+
path = os.path.dirname(__file__)
|
|
5
|
+
|
|
6
|
+
files = []
|
|
7
|
+
|
|
8
|
+
for f in sorted(os.listdir(path)):
|
|
9
|
+
if f.endswith(".py") and f not in ["__init__.py", "cli.py"]:
|
|
10
|
+
files.append(f[:-3]) # .py remove karega
|
|
11
|
+
|
|
12
|
+
print("\nAvailable modules:\n")
|
|
13
|
+
|
|
14
|
+
for i, file in enumerate(files, 1):
|
|
15
|
+
print(f"{i}. {file}")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
if __name__ == "__main__":
|
|
19
|
+
main()
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Magic Square (Odd Order) - Non AI
|
|
2
|
+
|
|
3
|
+
def generate_magic_square(n):
|
|
4
|
+
magic = [[0]*n for _ in range(n)]
|
|
5
|
+
|
|
6
|
+
i = 0
|
|
7
|
+
j = n // 2 # middle column
|
|
8
|
+
|
|
9
|
+
for num in range(1, n*n + 1):
|
|
10
|
+
magic[i][j] = num
|
|
11
|
+
|
|
12
|
+
new_i = (i - 1) % n
|
|
13
|
+
new_j = (j + 1) % n
|
|
14
|
+
|
|
15
|
+
if magic[new_i][new_j]:
|
|
16
|
+
i = (i + 1) % n
|
|
17
|
+
else:
|
|
18
|
+
i = new_i
|
|
19
|
+
j = new_j
|
|
20
|
+
|
|
21
|
+
return magic
|
|
22
|
+
|
|
23
|
+
def print_square(square):
|
|
24
|
+
for row in square:
|
|
25
|
+
print(" ".join(str(x) for x in row))
|
|
26
|
+
|
|
27
|
+
def main():
|
|
28
|
+
n = int(input("Enter odd number (n): "))
|
|
29
|
+
|
|
30
|
+
if n % 2 == 0:
|
|
31
|
+
print("Magic square only works for odd n")
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
square = generate_magic_square(n)
|
|
35
|
+
|
|
36
|
+
print("\nMagic Square:")
|
|
37
|
+
print_square(square)
|
|
38
|
+
|
|
39
|
+
main()
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pracai
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python package containing multiple utility scripts
|
|
5
|
+
Requires-Python: >=3.8
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: nltk
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# MyPackage
|
|
12
|
+
|
|
13
|
+
A Python package containing multiple utility scripts.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install mypackage
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
mypackage/A 8 puzzle.py
|
|
5
|
+
mypackage/A CitiesDistance.py
|
|
6
|
+
mypackage/A RobotNavigation.py
|
|
7
|
+
mypackage/BFS 8puzzle.py
|
|
8
|
+
mypackage/CitiesDistance.py
|
|
9
|
+
mypackage/CrossWord.py
|
|
10
|
+
mypackage/Cryptarithmetic.py
|
|
11
|
+
mypackage/HillClimbing8puzzle.py
|
|
12
|
+
mypackage/MapColouring.py
|
|
13
|
+
mypackage/MinimaxTicTacToe.py
|
|
14
|
+
mypackage/NQueens.py
|
|
15
|
+
mypackage/POS Tagging.py
|
|
16
|
+
mypackage/RobotNavigation.py
|
|
17
|
+
mypackage/SimilarityScore.py
|
|
18
|
+
mypackage/SpellChecker.py
|
|
19
|
+
mypackage/TicTacToe.py
|
|
20
|
+
mypackage/WaterJugBFS.py
|
|
21
|
+
mypackage/WaterJugDFS.py
|
|
22
|
+
mypackage/__init__.py
|
|
23
|
+
mypackage/cli.py
|
|
24
|
+
mypackage/magicSquare.py
|
|
25
|
+
pracai.egg-info/PKG-INFO
|
|
26
|
+
pracai.egg-info/SOURCES.txt
|
|
27
|
+
pracai.egg-info/dependency_links.txt
|
|
28
|
+
pracai.egg-info/entry_points.txt
|
|
29
|
+
pracai.egg-info/requires.txt
|
|
30
|
+
pracai.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nltk
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mypackage
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pracai"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "Python package containing multiple utility scripts"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
|
|
12
|
+
dependencies = ["nltk"]
|
|
13
|
+
|
|
14
|
+
[project.scripts]
|
|
15
|
+
pracai = "mypackage.cli:main"
|
|
16
|
+
|
|
17
|
+
[tool.setuptools]
|
|
18
|
+
packages = ["mypackage"]
|
pracai-0.0.1/setup.cfg
ADDED