aoalabcode 1.0.0__py3-none-any.whl
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.
- aoalabcode/__init__.py +1 -0
- aoalabcode/cli.py +103 -0
- aoalabcode/templates/Bellman_Ford.txt +39 -0
- aoalabcode/templates/BinarySearch.txt +26 -0
- aoalabcode/templates/Dijikstra.txt +38 -0
- aoalabcode/templates/Floyd_Warshall.txt +26 -0
- aoalabcode/templates/GraphColouring.txt +33 -0
- aoalabcode/templates/GreedyKnapsack.txt +23 -0
- aoalabcode/templates/Hamiltonian.txt +46 -0
- aoalabcode/templates/InsertionSort.txt +19 -0
- aoalabcode/templates/KMP.txt +41 -0
- aoalabcode/templates/Kruskal.txt +32 -0
- aoalabcode/templates/LCS.txt +44 -0
- aoalabcode/templates/MaxMin.txt +31 -0
- aoalabcode/templates/MergeSort.txt +39 -0
- aoalabcode/templates/NQueen.txt +23 -0
- aoalabcode/templates/Prims.txt +30 -0
- aoalabcode/templates/QuickSort.txt +28 -0
- aoalabcode/templates/Rabin_Karp_Matcher.txt +31 -0
- aoalabcode/templates/SelectionSort.txt +20 -0
- aoalabcode/templates/SumOfSubsets.txt +27 -0
- aoalabcode/templates/naive_string_matches.txt +14 -0
- aoalabcode-1.0.0.dist-info/METADATA +5 -0
- aoalabcode-1.0.0.dist-info/RECORD +27 -0
- aoalabcode-1.0.0.dist-info/WHEEL +5 -0
- aoalabcode-1.0.0.dist-info/entry_points.txt +2 -0
- aoalabcode-1.0.0.dist-info/top_level.txt +1 -0
aoalabcode/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.0.0"
|
aoalabcode/cli.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from importlib.resources import files
|
|
3
|
+
import shutil
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
CODES = {
|
|
7
|
+
"floyd": "Floyd_Warshall.txt",
|
|
8
|
+
"binarysearch": "BinarySearch.txt",
|
|
9
|
+
"dijkstra": "Dijkstra.txt",
|
|
10
|
+
"bellmanford": "Bellman_Ford.txt",
|
|
11
|
+
"graphcolouring": "GraphColouring.txt",
|
|
12
|
+
"greedyknapsack": "GreedyKnapsack.txt",
|
|
13
|
+
"insertionsort": "InsertionSort.txt",
|
|
14
|
+
"kmp": "KMP.txt",
|
|
15
|
+
"kruskal": "Kruskal.txt",
|
|
16
|
+
"hamiltonian": "Hamiltonian.txt",
|
|
17
|
+
"lcs": "LCS.txt",
|
|
18
|
+
"maxmin": "MaxMin.txt",
|
|
19
|
+
"mergesort": "MergeSort.txt",
|
|
20
|
+
"nqueen": "NQueen.txt",
|
|
21
|
+
"naive": "naive_string_matches.txt",
|
|
22
|
+
"prims": "Prims.txt",
|
|
23
|
+
"quicksort": "QuickSort.txt",
|
|
24
|
+
"selectionsort": "SelectionSort.txt",
|
|
25
|
+
"rabinkarp": "Rabin_Karp_Matcher.txt",
|
|
26
|
+
"sumofsubsets": "SumOfSubsets.txt",
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def copy_template(code):
|
|
31
|
+
try:
|
|
32
|
+
src = files("aoalabcode.templates").joinpath(CODES[code])
|
|
33
|
+
dst = Path.cwd() / CODES[code]
|
|
34
|
+
|
|
35
|
+
shutil.copy(str(src), dst)
|
|
36
|
+
|
|
37
|
+
print(f"\nDownloaded {CODES[code]}")
|
|
38
|
+
print(f"Saved to {dst}")
|
|
39
|
+
|
|
40
|
+
except Exception as e:
|
|
41
|
+
print(f"\nError: {e}")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def list_codes():
|
|
45
|
+
print("\nAvailable Codes:\n")
|
|
46
|
+
|
|
47
|
+
for code in CODES:
|
|
48
|
+
print(code)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def copy_all():
|
|
52
|
+
for code in CODES:
|
|
53
|
+
copy_template(code)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def interactive_menu():
|
|
57
|
+
codes = list(CODES.keys())
|
|
58
|
+
|
|
59
|
+
print("\nAvailable Codes:\n")
|
|
60
|
+
|
|
61
|
+
for i, code in enumerate(codes, start=1):
|
|
62
|
+
print(f"{i}. {code}")
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
choice = int(input("\nChoose option: "))
|
|
66
|
+
|
|
67
|
+
if 1 <= choice <= len(codes):
|
|
68
|
+
copy_template(codes[choice - 1])
|
|
69
|
+
else:
|
|
70
|
+
print("Invalid choice")
|
|
71
|
+
|
|
72
|
+
except ValueError:
|
|
73
|
+
print("Please enter a valid number")
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def main():
|
|
77
|
+
if len(sys.argv) == 1:
|
|
78
|
+
interactive_menu()
|
|
79
|
+
return
|
|
80
|
+
|
|
81
|
+
command = sys.argv[1].lower()
|
|
82
|
+
|
|
83
|
+
if command == "list":
|
|
84
|
+
list_codes()
|
|
85
|
+
|
|
86
|
+
elif command == "all":
|
|
87
|
+
copy_all()
|
|
88
|
+
|
|
89
|
+
elif command in CODES:
|
|
90
|
+
copy_template(command)
|
|
91
|
+
|
|
92
|
+
else:
|
|
93
|
+
print("\nCode not found.\n")
|
|
94
|
+
|
|
95
|
+
print("Available commands:")
|
|
96
|
+
print(" aoalabcode")
|
|
97
|
+
print(" aoalabcode list")
|
|
98
|
+
print(" aoalabcode all")
|
|
99
|
+
print(" aoalabcode <code>")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
if __name__ == "__main__":
|
|
103
|
+
main()
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
def Bellman_Ford(G, s):
|
|
2
|
+
dist = {}
|
|
3
|
+
|
|
4
|
+
for v in G:
|
|
5
|
+
dist[v] = float('inf')
|
|
6
|
+
|
|
7
|
+
dist[s] = 0
|
|
8
|
+
|
|
9
|
+
for i in range(len(G) - 1):
|
|
10
|
+
for u, v, w in G["edges"]:
|
|
11
|
+
if dist[u] != float('inf') and dist[v] > dist[u] + w:
|
|
12
|
+
dist[v] = dist[u] + w
|
|
13
|
+
|
|
14
|
+
for u, v, w in G["edges"]:
|
|
15
|
+
if dist[u] != float('inf') and dist[v] > dist[u] + w:
|
|
16
|
+
return False
|
|
17
|
+
|
|
18
|
+
print("Shortest Distances:")
|
|
19
|
+
for v in dist:
|
|
20
|
+
print(v, ":", dist[v])
|
|
21
|
+
|
|
22
|
+
return True
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# Example
|
|
26
|
+
G = {
|
|
27
|
+
"A": [],
|
|
28
|
+
"B": [],
|
|
29
|
+
"C": [],
|
|
30
|
+
"D": [],
|
|
31
|
+
"edges": [
|
|
32
|
+
("A", "B", 4),
|
|
33
|
+
("A", "C", 5),
|
|
34
|
+
("B", "C", -2),
|
|
35
|
+
("C", "D", 3)
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
print("Negative Cycle Present:", not Bellman_Ford(G, "A"))
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
def binary_search(a, x):
|
|
2
|
+
low = 0
|
|
3
|
+
high = len(a) - 1
|
|
4
|
+
|
|
5
|
+
while low <= high:
|
|
6
|
+
mid = (low + high) // 2
|
|
7
|
+
|
|
8
|
+
if a[mid] == x:
|
|
9
|
+
return mid
|
|
10
|
+
elif a[mid] < x:
|
|
11
|
+
low = mid + 1
|
|
12
|
+
else:
|
|
13
|
+
high = mid - 1
|
|
14
|
+
|
|
15
|
+
return -1
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
a = [10, 20, 30, 40, 50]
|
|
19
|
+
x = 30
|
|
20
|
+
|
|
21
|
+
result = binary_search(a, x)
|
|
22
|
+
|
|
23
|
+
if result == -1:
|
|
24
|
+
print("Element not found")
|
|
25
|
+
else:
|
|
26
|
+
print("Element found at position", result + 1)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
def dijkstra(graph, s):
|
|
2
|
+
dist = {}
|
|
3
|
+
|
|
4
|
+
for v in graph:
|
|
5
|
+
dist[v] = float('inf')
|
|
6
|
+
|
|
7
|
+
dist[s] = 0
|
|
8
|
+
|
|
9
|
+
S = set()
|
|
10
|
+
Q = list(graph.keys())
|
|
11
|
+
|
|
12
|
+
while Q:
|
|
13
|
+
u = min(Q, key=lambda x: dist[x])
|
|
14
|
+
|
|
15
|
+
Q.remove(u)
|
|
16
|
+
S.add(u)
|
|
17
|
+
|
|
18
|
+
for v, w in graph[u]:
|
|
19
|
+
if dist[v] > dist[u] + w:
|
|
20
|
+
dist[v] = dist[u] + w
|
|
21
|
+
|
|
22
|
+
return dist
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# Example Graph
|
|
26
|
+
graph = {
|
|
27
|
+
'A': [('B', 4), ('C', 1)],
|
|
28
|
+
'B': [('D', 1)],
|
|
29
|
+
'C': [('B', 2), ('D', 5)],
|
|
30
|
+
'D': []
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
s = 'A'
|
|
34
|
+
|
|
35
|
+
dist = dijkstra(graph, s)
|
|
36
|
+
|
|
37
|
+
for v in dist:
|
|
38
|
+
print(v, ":", dist[v])
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
def FLOYD_WARSHALL(W):
|
|
2
|
+
n = len(W)
|
|
3
|
+
D = [row[:] for row in W]
|
|
4
|
+
|
|
5
|
+
for k in range(n):
|
|
6
|
+
for i in range(n):
|
|
7
|
+
for j in range(n):
|
|
8
|
+
D[i][j] = min(D[i][j], D[i][k] + D[k][j])
|
|
9
|
+
|
|
10
|
+
return D
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Example
|
|
14
|
+
INF = float('inf')
|
|
15
|
+
|
|
16
|
+
W = [
|
|
17
|
+
[0, 3, INF, 7],
|
|
18
|
+
[8, 0, 2, INF],
|
|
19
|
+
[5, INF, 0, 1],
|
|
20
|
+
[2, INF, INF, 0]
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
D = FLOYD_WARSHALL(W)
|
|
24
|
+
|
|
25
|
+
for row in D:
|
|
26
|
+
print(row)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
def isSafe(k, color):
|
|
2
|
+
for j in range(1, n + 1):
|
|
3
|
+
if G[k][j] == 1 and x[j] == color:
|
|
4
|
+
return False
|
|
5
|
+
return True
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def GraphColoring(k):
|
|
9
|
+
for color in range(1, m + 1):
|
|
10
|
+
if isSafe(k, color):
|
|
11
|
+
x[k] = color
|
|
12
|
+
|
|
13
|
+
if k == n:
|
|
14
|
+
print(x[1:n + 1])
|
|
15
|
+
else:
|
|
16
|
+
GraphColoring(k + 1)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# Example
|
|
20
|
+
n = 4
|
|
21
|
+
m = 3
|
|
22
|
+
|
|
23
|
+
G = [
|
|
24
|
+
[0, 0, 0, 0, 0],
|
|
25
|
+
[0, 0, 1, 1, 1],
|
|
26
|
+
[0, 1, 0, 1, 0],
|
|
27
|
+
[0, 1, 1, 0, 1],
|
|
28
|
+
[0, 1, 0, 1, 0]
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
x = [0] * (n + 1)
|
|
32
|
+
|
|
33
|
+
GraphColoring(1)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
def fractionalKnapsack(items, capacity):
|
|
2
|
+
weightTaken = 0
|
|
3
|
+
profit = 0
|
|
4
|
+
|
|
5
|
+
items.sort(key=lambda x: x[0] / x[1], reverse=True)
|
|
6
|
+
|
|
7
|
+
for vi, wi in items:
|
|
8
|
+
if weightTaken + wi <= capacity:
|
|
9
|
+
weightTaken += wi
|
|
10
|
+
profit += vi
|
|
11
|
+
else:
|
|
12
|
+
remaining = capacity - weightTaken
|
|
13
|
+
profit += remaining * (vi / wi)
|
|
14
|
+
break
|
|
15
|
+
|
|
16
|
+
return profit
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# Example
|
|
20
|
+
items = [(60, 10), (100, 20), (120, 30)] # (value, weight)
|
|
21
|
+
capacity = 50
|
|
22
|
+
|
|
23
|
+
print("Maximum Profit =", fractionalKnapsack(items, capacity))
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
def nextValue(k):
|
|
2
|
+
while True:
|
|
3
|
+
x[k] = (x[k] + 1) % (n + 1)
|
|
4
|
+
|
|
5
|
+
if x[k] == 0:
|
|
6
|
+
return
|
|
7
|
+
|
|
8
|
+
if G[x[k - 1]][x[k]] == 1:
|
|
9
|
+
for j in range(1, k):
|
|
10
|
+
if x[j] == x[k]:
|
|
11
|
+
break
|
|
12
|
+
|
|
13
|
+
if j == k - 1:
|
|
14
|
+
if k < n or (k == n and G[x[n]][x[1]] == 1):
|
|
15
|
+
return
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def Hamiltonian(k):
|
|
19
|
+
while True:
|
|
20
|
+
nextValue(k)
|
|
21
|
+
|
|
22
|
+
if x[k] == 0:
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
if k == n:
|
|
26
|
+
print(x[1:n + 1])
|
|
27
|
+
else:
|
|
28
|
+
Hamiltonian(k + 1)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# Example
|
|
32
|
+
n = 5
|
|
33
|
+
|
|
34
|
+
G = [
|
|
35
|
+
[0, 0, 0, 0, 0, 0],
|
|
36
|
+
[0, 0, 1, 1, 0, 1],
|
|
37
|
+
[0, 1, 0, 1, 1, 0],
|
|
38
|
+
[0, 1, 1, 0, 1, 1],
|
|
39
|
+
[0, 0, 1, 1, 0, 1],
|
|
40
|
+
[0, 1, 0, 1, 1, 0]
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
x = [0] * (n + 1)
|
|
44
|
+
x[1] = 1
|
|
45
|
+
|
|
46
|
+
Hamiltonian(2)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
def insertion_sort(a):
|
|
2
|
+
for i in range(1, len(a)):
|
|
3
|
+
key = a[i]
|
|
4
|
+
j = i - 1
|
|
5
|
+
|
|
6
|
+
while j >= 0 and a[j] > key:
|
|
7
|
+
a[j + 1] = a[j]
|
|
8
|
+
j -= 1
|
|
9
|
+
|
|
10
|
+
a[j + 1] = key
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
a = [64, 25, 12, 22, 11]
|
|
14
|
+
|
|
15
|
+
insertion_sort(a)
|
|
16
|
+
|
|
17
|
+
print("Sorted Array:")
|
|
18
|
+
for i in a:
|
|
19
|
+
print(i, end=" ")
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
def Compute_Prefix_Function(P):
|
|
2
|
+
m = len(P)
|
|
3
|
+
pi = [0] * m
|
|
4
|
+
k = 0
|
|
5
|
+
|
|
6
|
+
for q in range(1, m):
|
|
7
|
+
while k > 0 and P[k] != P[q]:
|
|
8
|
+
k = pi[k - 1]
|
|
9
|
+
|
|
10
|
+
if P[k] == P[q]:
|
|
11
|
+
k += 1
|
|
12
|
+
|
|
13
|
+
pi[q] = k
|
|
14
|
+
|
|
15
|
+
return pi
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def KMP_Matcher(S, P):
|
|
19
|
+
n = len(S)
|
|
20
|
+
m = len(P)
|
|
21
|
+
|
|
22
|
+
pi = Compute_Prefix_Function(P)
|
|
23
|
+
q = 0
|
|
24
|
+
|
|
25
|
+
for i in range(n):
|
|
26
|
+
while q > 0 and P[q] != S[i]:
|
|
27
|
+
q = pi[q - 1]
|
|
28
|
+
|
|
29
|
+
if P[q] == S[i]:
|
|
30
|
+
q += 1
|
|
31
|
+
|
|
32
|
+
if q == m:
|
|
33
|
+
print("Pattern occurs with shift", i - m + 1)
|
|
34
|
+
q = pi[q - 1]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# Example
|
|
38
|
+
S = input("Enter text: ")
|
|
39
|
+
P = input("Enter pattern: ")
|
|
40
|
+
|
|
41
|
+
KMP_Matcher(S, P)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
def find(parent, i):
|
|
2
|
+
if parent[i] == i:
|
|
3
|
+
return i
|
|
4
|
+
return find(parent, parent[i])
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def union(parent, x, y):
|
|
8
|
+
parent[find(parent, x)] = find(parent, y)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def MST_Kruskal(V, E):
|
|
12
|
+
A = []
|
|
13
|
+
parent = {}
|
|
14
|
+
|
|
15
|
+
for v in V:
|
|
16
|
+
parent[v] = v
|
|
17
|
+
|
|
18
|
+
E.sort(key=lambda x: x[2])
|
|
19
|
+
|
|
20
|
+
for u, v, w in E:
|
|
21
|
+
if find(parent, u) != find(parent, v):
|
|
22
|
+
A.append((u, v, w))
|
|
23
|
+
union(parent, u, v)
|
|
24
|
+
|
|
25
|
+
return A
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# Example
|
|
29
|
+
V = [0, 1, 2, 3]
|
|
30
|
+
E = [(0, 1, 10), (0, 2, 6), (0, 3, 5), (1, 3, 15), (2, 3, 4)]
|
|
31
|
+
|
|
32
|
+
print(MST_Kruskal(V, E))
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
def LCS_LENGTH(X, Y):
|
|
2
|
+
m = len(X)
|
|
3
|
+
n = len(Y)
|
|
4
|
+
|
|
5
|
+
c = [[0] * (n + 1) for _ in range(m + 1)]
|
|
6
|
+
b = [[""] * (n + 1) for _ in range(m + 1)]
|
|
7
|
+
|
|
8
|
+
for i in range(1, m + 1):
|
|
9
|
+
for j in range(1, n + 1):
|
|
10
|
+
if X[i - 1] == Y[j - 1]:
|
|
11
|
+
c[i][j] = c[i - 1][j - 1] + 1
|
|
12
|
+
b[i][j] = "↖"
|
|
13
|
+
elif c[i - 1][j] >= c[i][j - 1]:
|
|
14
|
+
c[i][j] = c[i - 1][j]
|
|
15
|
+
b[i][j] = "↑"
|
|
16
|
+
else:
|
|
17
|
+
c[i][j] = c[i][j - 1]
|
|
18
|
+
b[i][j] = "←"
|
|
19
|
+
|
|
20
|
+
return c, b
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def PRINT_LCS(b, X, i, j):
|
|
24
|
+
if i == 0 or j == 0:
|
|
25
|
+
return
|
|
26
|
+
|
|
27
|
+
if b[i][j] == "↖":
|
|
28
|
+
PRINT_LCS(b, X, i - 1, j - 1)
|
|
29
|
+
print(X[i - 1], end="")
|
|
30
|
+
elif b[i][j] == "↑":
|
|
31
|
+
PRINT_LCS(b, X, i - 1, j)
|
|
32
|
+
else:
|
|
33
|
+
PRINT_LCS(b, X, i, j - 1)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
X = input("Enter X: ")
|
|
37
|
+
Y = input("Enter Y: ")
|
|
38
|
+
|
|
39
|
+
c, b = LCS_LENGTH(X, Y)
|
|
40
|
+
|
|
41
|
+
print("Length of LCS =", c[len(X)][len(Y)])
|
|
42
|
+
print("LCS =", end=" ")
|
|
43
|
+
PRINT_LCS(b, X, len(X), len(Y))
|
|
44
|
+
print()
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
def maxmin(i, j, a):
|
|
2
|
+
if i == j:
|
|
3
|
+
return a[i], a[i]
|
|
4
|
+
|
|
5
|
+
if i == j - 1:
|
|
6
|
+
if a[i] > a[j]:
|
|
7
|
+
return a[i], a[j]
|
|
8
|
+
else:
|
|
9
|
+
return a[j], a[i]
|
|
10
|
+
|
|
11
|
+
mid = (i + j) // 2
|
|
12
|
+
|
|
13
|
+
max, min = maxmin(i, mid, a)
|
|
14
|
+
max1, min1 = maxmin(mid + 1, j, a)
|
|
15
|
+
|
|
16
|
+
if max < max1:
|
|
17
|
+
max = max1
|
|
18
|
+
|
|
19
|
+
if min > min1:
|
|
20
|
+
min = min1
|
|
21
|
+
|
|
22
|
+
return max, min
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# Example
|
|
26
|
+
a = list(map(int, input("Enter elements: ").split()))
|
|
27
|
+
|
|
28
|
+
max, min = maxmin(0, len(a) - 1, a)
|
|
29
|
+
|
|
30
|
+
print("Maximum =", max)
|
|
31
|
+
print("Minimum =", min)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
def merge_sort(a):
|
|
2
|
+
if len(a) > 1:
|
|
3
|
+
mid = len(a) // 2
|
|
4
|
+
|
|
5
|
+
L = a[:mid]
|
|
6
|
+
R = a[mid:]
|
|
7
|
+
|
|
8
|
+
merge_sort(L)
|
|
9
|
+
merge_sort(R)
|
|
10
|
+
|
|
11
|
+
i = j = k = 0
|
|
12
|
+
|
|
13
|
+
while i < len(L) and j < len(R):
|
|
14
|
+
if L[i] < R[j]:
|
|
15
|
+
a[k] = L[i]
|
|
16
|
+
i += 1
|
|
17
|
+
else:
|
|
18
|
+
a[k] = R[j]
|
|
19
|
+
j += 1
|
|
20
|
+
k += 1
|
|
21
|
+
|
|
22
|
+
while i < len(L):
|
|
23
|
+
a[k] = L[i]
|
|
24
|
+
i += 1
|
|
25
|
+
k += 1
|
|
26
|
+
|
|
27
|
+
while j < len(R):
|
|
28
|
+
a[k] = R[j]
|
|
29
|
+
j += 1
|
|
30
|
+
k += 1
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
a = [64, 25, 12, 22, 11]
|
|
34
|
+
|
|
35
|
+
merge_sort(a)
|
|
36
|
+
|
|
37
|
+
print("Sorted Array:")
|
|
38
|
+
for i in a:
|
|
39
|
+
print(i, end=" ")
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
def isSafe(k, i):
|
|
2
|
+
for j in range(1, k):
|
|
3
|
+
if x[j] == i or abs(x[j] - i) == abs(j - k):
|
|
4
|
+
return False
|
|
5
|
+
return True
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def NQueen(k, n):
|
|
9
|
+
for i in range(1, n + 1):
|
|
10
|
+
if isSafe(k, i):
|
|
11
|
+
x[k] = i
|
|
12
|
+
|
|
13
|
+
if k == n:
|
|
14
|
+
print(x[1:n + 1])
|
|
15
|
+
else:
|
|
16
|
+
NQueen(k + 1, n)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# Example
|
|
20
|
+
n = int(input("Enter n: "))
|
|
21
|
+
x = [0] * (n + 1)
|
|
22
|
+
|
|
23
|
+
NQueen(1, n)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
def MST_Prim(G, r):
|
|
2
|
+
Q = list(G.keys())
|
|
3
|
+
|
|
4
|
+
key = {u: float('inf') for u in G}
|
|
5
|
+
pi = {u: None for u in G}
|
|
6
|
+
|
|
7
|
+
key[r] = 0
|
|
8
|
+
|
|
9
|
+
while Q:
|
|
10
|
+
u = min(Q, key=lambda x: key[x])
|
|
11
|
+
Q.remove(u)
|
|
12
|
+
|
|
13
|
+
for v, w in G[u]:
|
|
14
|
+
if v in Q and w < key[v]:
|
|
15
|
+
pi[v] = u
|
|
16
|
+
key[v] = w
|
|
17
|
+
|
|
18
|
+
return pi
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# Example
|
|
22
|
+
G = {
|
|
23
|
+
0: [(1, 2), (3, 6)],
|
|
24
|
+
1: [(0, 2), (2, 3), (3, 8), (4, 5)],
|
|
25
|
+
2: [(1, 3), (4, 7)],
|
|
26
|
+
3: [(0, 6), (1, 8), (4, 9)],
|
|
27
|
+
4: [(1, 5), (2, 7), (3, 9)]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
print(MST_Prim(G, 0))
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
def partition(a, low, high):
|
|
2
|
+
pivot = a[high]
|
|
3
|
+
i = low - 1
|
|
4
|
+
|
|
5
|
+
for j in range(low, high):
|
|
6
|
+
if a[j] <= pivot:
|
|
7
|
+
i += 1
|
|
8
|
+
a[i], a[j] = a[j], a[i]
|
|
9
|
+
|
|
10
|
+
a[i + 1], a[high] = a[high], a[i + 1]
|
|
11
|
+
return i + 1
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def quick_sort(a, low, high):
|
|
15
|
+
if low < high:
|
|
16
|
+
pi = partition(a, low, high)
|
|
17
|
+
|
|
18
|
+
quick_sort(a, low, pi - 1)
|
|
19
|
+
quick_sort(a, pi + 1, high)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
a = [64, 25, 12, 22, 11]
|
|
23
|
+
|
|
24
|
+
quick_sort(a, 0, len(a) - 1)
|
|
25
|
+
|
|
26
|
+
print("Sorted Array:")
|
|
27
|
+
for i in a:
|
|
28
|
+
print(i, end=" ")
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
def Rabin_Karp_Matcher(T, P, d, q):
|
|
2
|
+
n = len(T)
|
|
3
|
+
m = len(P)
|
|
4
|
+
|
|
5
|
+
h = pow(d, m - 1, q)
|
|
6
|
+
p = 0
|
|
7
|
+
t0 = 0
|
|
8
|
+
|
|
9
|
+
for i in range(m):
|
|
10
|
+
p = (d * p + ord(P[i])) % q
|
|
11
|
+
t0 = (d * t0 + ord(T[i])) % q
|
|
12
|
+
|
|
13
|
+
for s in range(n - m + 1):
|
|
14
|
+
if p == t0:
|
|
15
|
+
if P == T[s:s + m]:
|
|
16
|
+
print("Pattern occurs with shift", s)
|
|
17
|
+
|
|
18
|
+
if s < n - m:
|
|
19
|
+
t0 = (d * (t0 - ord(T[s]) * h) + ord(T[s + m])) % q
|
|
20
|
+
if t0 < 0:
|
|
21
|
+
t0 += q
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# Example
|
|
25
|
+
T = input("Enter text: ")
|
|
26
|
+
P = input("Enter pattern: ")
|
|
27
|
+
|
|
28
|
+
d = 256 # number of characters
|
|
29
|
+
q = 101 # prime number
|
|
30
|
+
|
|
31
|
+
Rabin_Karp_Matcher(T, P, d, q)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
def selection_sort(a):
|
|
2
|
+
n = len(a)
|
|
3
|
+
|
|
4
|
+
for i in range(n):
|
|
5
|
+
m = i
|
|
6
|
+
|
|
7
|
+
for j in range(i + 1, n):
|
|
8
|
+
if a[j] < a[m]:
|
|
9
|
+
m = j
|
|
10
|
+
|
|
11
|
+
a[i], a[m] = a[m], a[i]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
a = [64, 25, 12, 22, 11]
|
|
15
|
+
|
|
16
|
+
selection_sort(a)
|
|
17
|
+
|
|
18
|
+
print("Sorted Array:")
|
|
19
|
+
for i in a:
|
|
20
|
+
print(i, end=" ")
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
def promising(k, s, r):
|
|
2
|
+
return (s + r >= target) and (s == target or s + w[k + 1] <= target)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def SumOfSubsets(k, s, r):
|
|
6
|
+
if promising(k, s, r):
|
|
7
|
+
if s == target:
|
|
8
|
+
subset = []
|
|
9
|
+
for i in range(1, len(w)):
|
|
10
|
+
if x[i] == 1:
|
|
11
|
+
subset.append(w[i])
|
|
12
|
+
print(subset)
|
|
13
|
+
else:
|
|
14
|
+
x[k + 1] = 1
|
|
15
|
+
SumOfSubsets(k + 1, s + w[k + 1], r - w[k + 1])
|
|
16
|
+
|
|
17
|
+
x[k + 1] = 0
|
|
18
|
+
SumOfSubsets(k + 1, s, r - w[k + 1])
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# Example
|
|
22
|
+
w = [0, 5, 10, 12, 13, 15, 18] # 1-based indexing
|
|
23
|
+
target = 30
|
|
24
|
+
|
|
25
|
+
x = [0] * len(w)
|
|
26
|
+
|
|
27
|
+
SumOfSubsets(0, 0, sum(w))
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
aoalabcode/__init__.py,sha256=Aj77VL1d5Mdku7sgCgKQmPuYavPpAHuZuJcy6bygQZE,21
|
|
2
|
+
aoalabcode/cli.py,sha256=hsFes4O7-tCNbVcc5OG8WOxbzjIMeJqVU3EaA-01kWU,2353
|
|
3
|
+
aoalabcode/templates/Bellman_Ford.txt,sha256=YJfkBURS9BdKzb4IlH9zzuXsrQ6bCllbL9tCTOnMH5s,777
|
|
4
|
+
aoalabcode/templates/BinarySearch.txt,sha256=pylYjlttCCTSIFmiYnKXZXotbZ0WB9ks9qf1qGA1aCk,464
|
|
5
|
+
aoalabcode/templates/Dijikstra.txt,sha256=0X3qX_FsXmY3SxM4ygbyPmAoA6rb3xYY1mPGHBtKqBo,607
|
|
6
|
+
aoalabcode/templates/Floyd_Warshall.txt,sha256=G8HFIZnpoV4cfB8xvVl4yVB77gwJsxGIFnhb8OftBzo,420
|
|
7
|
+
aoalabcode/templates/GraphColouring.txt,sha256=TVj27ftdieR5NbWe8otQw8cWNsC90NGHqKlijXmoY7s,566
|
|
8
|
+
aoalabcode/templates/GreedyKnapsack.txt,sha256=QoPWwLbM9FGdzkl3_uclkF3L6UtAt2a8mdPagiPZRlA,568
|
|
9
|
+
aoalabcode/templates/Hamiltonian.txt,sha256=aQ5tum9JlvK0uwmVu_eAgSuaAnipimbCr7nXtFin4QE,811
|
|
10
|
+
aoalabcode/templates/InsertionSort.txt,sha256=T-8FHFNqr3FCQ2NUESb6J1pkxfNs66bU8tZ476etdRQ,321
|
|
11
|
+
aoalabcode/templates/KMP.txt,sha256=PGYIfcYAgJJpB8Ax5h3xZPXARtPs80UWGEngZ4jPLSc,708
|
|
12
|
+
aoalabcode/templates/Kruskal.txt,sha256=n2T0zyduz1e66fbXT5Ey2Mgdb3OAOAd4kvk6YcHf2o4,587
|
|
13
|
+
aoalabcode/templates/LCS.txt,sha256=3F4Td4ix90QDy6Og87mIj9SRE8tnRIzJd6Vk7LgBEuQ,1040
|
|
14
|
+
aoalabcode/templates/MaxMin.txt,sha256=21SzcLY5QxPpOEj8HOdeFeMyGQGxNalbHZKIorkQaKU,557
|
|
15
|
+
aoalabcode/templates/MergeSort.txt,sha256=-KAeyZmH0FNpBs8rr6rA9zNidLZmesZibOfnyoXYk-E,697
|
|
16
|
+
aoalabcode/templates/NQueen.txt,sha256=qhm-jvvbmMRnjYFbZiJuL6kCqjJhDyk3PxY1P_EX8M0,436
|
|
17
|
+
aoalabcode/templates/Prims.txt,sha256=oiWlwnYLA5hP8fij09SVI64-zVRBHpG5zfEa4ZoYfpE,564
|
|
18
|
+
aoalabcode/templates/QuickSort.txt,sha256=ORroAla6mfnig3W5NsDECcv5EXVIErPjhGWnus4lRok,539
|
|
19
|
+
aoalabcode/templates/Rabin_Karp_Matcher.txt,sha256=daG8MC005EHwRhRfU2IOF5IQZhTHXIdVEeu9to_szUc,676
|
|
20
|
+
aoalabcode/templates/SelectionSort.txt,sha256=AuLz9v0PAei-BwrkVwQbPyBeT-WeRH7thSnU8pSIPA4,316
|
|
21
|
+
aoalabcode/templates/SumOfSubsets.txt,sha256=nQMT4nOpr1h2H5xV0RB92Zzz7Yc6r4Vm7RNvJS5p7HU,658
|
|
22
|
+
aoalabcode/templates/naive_string_matches.txt,sha256=0n4UKr_E65omemL9wSO8vpSUqoFlYvROxkU6Ufl7lUI,267
|
|
23
|
+
aoalabcode-1.0.0.dist-info/METADATA,sha256=nMNaSAP-ngA3I-ygfVLLw7vJEOsQWxQSniox0Z8Gr0g,114
|
|
24
|
+
aoalabcode-1.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
25
|
+
aoalabcode-1.0.0.dist-info/entry_points.txt,sha256=Bk2hgwGFoITfZsUITGZknQDi9xWHGFW3Yj2e51nabC4,51
|
|
26
|
+
aoalabcode-1.0.0.dist-info/top_level.txt,sha256=J_QFY2Y9cx6Ok9myhHn6D2Cr0_GPVCRbmgFrgptRvEw,11
|
|
27
|
+
aoalabcode-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
aoalabcode
|