mathref 0.1.0__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.
- mathref-0.1.0/PKG-INFO +17 -0
- mathref-0.1.0/README.md +10 -0
- mathref-0.1.0/mathref/__init__.py +0 -0
- mathref-0.1.0/mathref/__main__.py +48 -0
- mathref-0.1.0/mathref/mathref/01_maatraco.py +258 -0
- mathref-0.1.0/mathref/mathref/02_mathref.py +21 -0
- mathref-0.1.0/mathref.egg-info/PKG-INFO +17 -0
- mathref-0.1.0/mathref.egg-info/SOURCES.txt +10 -0
- mathref-0.1.0/mathref.egg-info/dependency_links.txt +1 -0
- mathref-0.1.0/mathref.egg-info/top_level.txt +1 -0
- mathref-0.1.0/pyproject.toml +16 -0
- mathref-0.1.0/setup.cfg +4 -0
mathref-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mathref
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A collection of numbered Python math references
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
|
|
8
|
+
A Python package containing numbered practical notes and code.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
python -m pip install maatraco-kit
|
|
12
|
+
|
|
13
|
+
## Run in Python IDLE
|
|
14
|
+
import runpy
|
|
15
|
+
runpy.run_module("maatracos", run_name="__main__")
|
|
16
|
+
|
|
17
|
+
Enter a maatraco number to display its contents. Enter 0 to exit.
|
mathref-0.1.0/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
A Python package containing numbered practical notes and code.
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
python -m pip install maatraco-kit
|
|
5
|
+
|
|
6
|
+
## Run in Python IDLE
|
|
7
|
+
import runpy
|
|
8
|
+
runpy.run_module("maatracos", run_name="__main__")
|
|
9
|
+
|
|
10
|
+
Enter a maatraco number to display its contents. Enter 0 to exit.
|
|
File without changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from importlib.resources import files
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def main():
|
|
5
|
+
folder = files("mathref").joinpath("maatracos")
|
|
6
|
+
|
|
7
|
+
mathrefs = sorted(
|
|
8
|
+
(
|
|
9
|
+
p for p in folder.iterdir()
|
|
10
|
+
if p.name.lower().endswith((".txt", ".py"))
|
|
11
|
+
),
|
|
12
|
+
key=lambda p: p.name
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
if not mathrefs:
|
|
16
|
+
print("No MathRef files found.")
|
|
17
|
+
return
|
|
18
|
+
|
|
19
|
+
print("\n===== MATHREF =====")
|
|
20
|
+
print("Collection!\n")
|
|
21
|
+
|
|
22
|
+
for i, mathref in enumerate(mathrefs, start=1):
|
|
23
|
+
print(f"{i}. {mathref.name}")
|
|
24
|
+
|
|
25
|
+
while True:
|
|
26
|
+
choice = input("\nEnter number (0 to exit): ").strip()
|
|
27
|
+
|
|
28
|
+
if choice == "0":
|
|
29
|
+
print("Goodbye!")
|
|
30
|
+
break
|
|
31
|
+
|
|
32
|
+
if not choice.isdigit():
|
|
33
|
+
print("Please enter a valid number.")
|
|
34
|
+
continue
|
|
35
|
+
|
|
36
|
+
number = int(choice)
|
|
37
|
+
|
|
38
|
+
if not 1 <= number <= len(mathrefs):
|
|
39
|
+
print("MathRef not found.")
|
|
40
|
+
continue
|
|
41
|
+
|
|
42
|
+
print(f"\n===== {mathrefs[number - 1].name} =====\n")
|
|
43
|
+
print(mathrefs[number - 1].read_text(encoding="utf-8"))
|
|
44
|
+
print()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
if __name__ == "__main__":
|
|
48
|
+
main()
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import heapq
|
|
5
|
+
from collections import deque
|
|
6
|
+
|
|
7
|
+
def read_nums():
|
|
8
|
+
return list(map(int, input("Enter numbers separated by spaces: ").split()))
|
|
9
|
+
|
|
10
|
+
# 1. Sorting: remember "sort()" fallback; these are manual algorithms.
|
|
11
|
+
def sorting():
|
|
12
|
+
a = read_nums()
|
|
13
|
+
print("1 Bubble 2 Selection 3 Insertion 4 Merge 5 Quick")
|
|
14
|
+
ch = input("Choice: ")
|
|
15
|
+
if ch == "1":
|
|
16
|
+
for i in range(len(a)):
|
|
17
|
+
for j in range(len(a)-i-1):
|
|
18
|
+
if a[j] > a[j+1]:
|
|
19
|
+
a[j], a[j+1] = a[j+1], a[j]
|
|
20
|
+
elif ch == "2":
|
|
21
|
+
for i in range(len(a)):
|
|
22
|
+
m = i
|
|
23
|
+
for j in range(i+1, len(a)):
|
|
24
|
+
if a[j] < a[m]: m = j
|
|
25
|
+
a[i], a[m] = a[m], a[i]
|
|
26
|
+
elif ch == "3":
|
|
27
|
+
for i in range(1, len(a)):
|
|
28
|
+
key, j = a[i], i-1
|
|
29
|
+
while j >= 0 and a[j] > key:
|
|
30
|
+
a[j+1] = a[j]; j -= 1
|
|
31
|
+
a[j+1] = key
|
|
32
|
+
elif ch == "4":
|
|
33
|
+
if len(a) > 1:
|
|
34
|
+
m = len(a)//2
|
|
35
|
+
left, right = a[:m], a[m:]
|
|
36
|
+
# recursive merge sort
|
|
37
|
+
def ms(x):
|
|
38
|
+
if len(x) <= 1: return x
|
|
39
|
+
m = len(x)//2
|
|
40
|
+
L, R = ms(x[:m]), ms(x[m:])
|
|
41
|
+
out = []
|
|
42
|
+
while L and R: out.append(L.pop(0) if L[0] <= R[0] else R.pop(0))
|
|
43
|
+
return out + L + R
|
|
44
|
+
a = ms(a)
|
|
45
|
+
elif ch == "5":
|
|
46
|
+
def qs(x):
|
|
47
|
+
if len(x) <= 1: return x
|
|
48
|
+
p = x[-1]
|
|
49
|
+
return qs([v for v in x[:-1] if v <= p]) + [p] + qs([v for v in x[:-1] if v > p])
|
|
50
|
+
a = qs(a)
|
|
51
|
+
print("Sorted:", a)
|
|
52
|
+
|
|
53
|
+
# 2. Matrix multiplication (standard O(n^3))
|
|
54
|
+
def matrix():
|
|
55
|
+
n = int(input("Square matrix size n: "))
|
|
56
|
+
print("Enter matrix A row by row:")
|
|
57
|
+
A = [list(map(int, input().split())) for _ in range(n)]
|
|
58
|
+
print("Enter matrix B row by row:")
|
|
59
|
+
B = [list(map(int, input().split())) for _ in range(n)]
|
|
60
|
+
C = [[sum(A[i][k]*B[k][j] for k in range(n)) for j in range(n)] for i in range(n)]
|
|
61
|
+
print("Result:")
|
|
62
|
+
for row in C: print(row)
|
|
63
|
+
|
|
64
|
+
# 3. Hiring problem / randomized-style candidate selection
|
|
65
|
+
def hiring():
|
|
66
|
+
scores = read_nums()
|
|
67
|
+
best, hired = -1, []
|
|
68
|
+
for i, score in enumerate(scores):
|
|
69
|
+
if score > best:
|
|
70
|
+
hired.append((i+1, score)); best = score
|
|
71
|
+
print("Candidates (position, score):", scores)
|
|
72
|
+
print("Hired record-breakers:", hired)
|
|
73
|
+
print("Total hires:", len(hired))
|
|
74
|
+
|
|
75
|
+
# 4. Order statistics and counting sort
|
|
76
|
+
def order_stats():
|
|
77
|
+
a = read_nums()
|
|
78
|
+
print("1 k-th smallest/largest 2 Counting sort (non-negative only)")
|
|
79
|
+
ch = input("Choice: ")
|
|
80
|
+
if ch == "1":
|
|
81
|
+
k = int(input("Enter k (1 = smallest/largest): "))
|
|
82
|
+
if not 1 <= k <= len(a): print("Invalid k"); return
|
|
83
|
+
s = sorted(a)
|
|
84
|
+
print("k-th smallest:", s[k-1], "| k-th largest:", s[-k])
|
|
85
|
+
else:
|
|
86
|
+
if min(a) < 0: print("Counting sort needs non-negative integers"); return
|
|
87
|
+
out = []
|
|
88
|
+
for v in range(max(a)+1): out += [v] * a.count(v)
|
|
89
|
+
print("Counting sort:", out)
|
|
90
|
+
|
|
91
|
+
# 5. Binary tree traversal and node counts
|
|
92
|
+
class Node:
|
|
93
|
+
def __init__(self, x):
|
|
94
|
+
self.x, self.left, self.right = x, None, None
|
|
95
|
+
|
|
96
|
+
def trees():
|
|
97
|
+
# Simple fixed tree; change node values/links for a question.
|
|
98
|
+
r = Node(10); r.left = Node(5); r.right = Node(15)
|
|
99
|
+
r.left.left = Node(2); r.left.right = Node(7); r.right.right = Node(20)
|
|
100
|
+
def ino(n): return ino(n.left)+[n.x]+ino(n.right) if n else []
|
|
101
|
+
def pre(n): return [n.x]+pre(n.left)+pre(n.right) if n else []
|
|
102
|
+
def post(n): return post(n.left)+post(n.right)+[n.x] if n else []
|
|
103
|
+
def count(n): return 0 if not n else 1+count(n.left)+count(n.right)
|
|
104
|
+
def leaves(n): return 0 if not n else (1 if not n.left and not n.right else leaves(n.left)+leaves(n.right))
|
|
105
|
+
print("Inorder LNR:", ino(r)); print("Preorder NLR:", pre(r)); print("Postorder LRN:", post(r))
|
|
106
|
+
print("Total nodes:", count(r), "| Leaf nodes:", leaves(r))
|
|
107
|
+
|
|
108
|
+
# Common sample weighted graph for BFS/DFS/Dijkstra/Prim/Kruskal.
|
|
109
|
+
def get_graph():
|
|
110
|
+
return {
|
|
111
|
+
'A': {'B': 4, 'C': 2}, 'B': {'A': 4, 'C': 1, 'D': 5},
|
|
112
|
+
'C': {'A': 2, 'B': 1, 'D': 8, 'E': 10}, 'D': {'B': 5, 'C': 8, 'E': 2},
|
|
113
|
+
'E': {'C': 10, 'D': 2}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
# 6. Graph algorithms
|
|
117
|
+
def graphs():
|
|
118
|
+
g = get_graph()
|
|
119
|
+
s = input("Start vertex (A/B/C/D/E): ").upper()
|
|
120
|
+
print("1 BFS 2 DFS 3 Dijkstra shortest distances 4 Prim MST 5 Kruskal MST")
|
|
121
|
+
ch = input("Choice: ")
|
|
122
|
+
if ch == "1":
|
|
123
|
+
q, seen, order = deque([s]), {s}, []
|
|
124
|
+
while q:
|
|
125
|
+
v = q.popleft(); order.append(v)
|
|
126
|
+
for u in g[v]:
|
|
127
|
+
if u not in seen: seen.add(u); q.append(u)
|
|
128
|
+
print("BFS:", order)
|
|
129
|
+
elif ch == "2":
|
|
130
|
+
seen, order = set(), []
|
|
131
|
+
def dfs(v):
|
|
132
|
+
seen.add(v); order.append(v)
|
|
133
|
+
for u in g[v]:
|
|
134
|
+
if u not in seen: dfs(u)
|
|
135
|
+
dfs(s); print("DFS:", order)
|
|
136
|
+
elif ch == "3":
|
|
137
|
+
d = {v: float("inf") for v in g}; d[s] = 0; pq = [(0, s)]
|
|
138
|
+
while pq:
|
|
139
|
+
cost, v = heapq.heappop(pq)
|
|
140
|
+
if cost != d[v]: continue
|
|
141
|
+
for u, w in g[v].items():
|
|
142
|
+
if cost+w < d[u]: d[u] = cost+w; heapq.heappush(pq, (d[u], u))
|
|
143
|
+
print("Shortest distances:", d)
|
|
144
|
+
else:
|
|
145
|
+
edges = sorted((w, v, u) for v in g for u, w in g[v].items() if v < u)
|
|
146
|
+
if ch == "4": # Prim
|
|
147
|
+
seen, chosen = {s}, []
|
|
148
|
+
while len(seen) < len(g):
|
|
149
|
+
options = [(w, v, u) for v in seen for u, w in g[v].items() if u not in seen]
|
|
150
|
+
if not options: break
|
|
151
|
+
w, v, u = min(options); chosen.append((v, u, w)); seen.add(u)
|
|
152
|
+
else: # Kruskal
|
|
153
|
+
parent = {v: v for v in g}
|
|
154
|
+
def find(x):
|
|
155
|
+
while parent[x] != x: x = parent[x]
|
|
156
|
+
return x
|
|
157
|
+
chosen = []
|
|
158
|
+
for w, v, u in edges:
|
|
159
|
+
a, b = find(v), find(u)
|
|
160
|
+
if a != b: parent[a] = b; chosen.append((v, u, w))
|
|
161
|
+
print("MST edges:", chosen, "| Total weight:", sum(e[2] for e in chosen))
|
|
162
|
+
|
|
163
|
+
# 7. String matching: works as a safe fallback for Naive, KMP/Rabin-Karp/Boyer-Moore questions
|
|
164
|
+
def strings():
|
|
165
|
+
text = input("Enter text: ")
|
|
166
|
+
pat = input("Enter pattern: ")
|
|
167
|
+
found = [i for i in range(len(text)-len(pat)+1) if text[i:i+len(pat)] == pat]
|
|
168
|
+
print("Pattern positions (0-based):", found if pat else [])
|
|
169
|
+
|
|
170
|
+
# 8. Dynamic programming: LCS, rod cutting, matrix-chain cost
|
|
171
|
+
def dynamic():
|
|
172
|
+
print("1 LCS 2 Rod cutting 3 Matrix-chain multiplication")
|
|
173
|
+
ch = input("Choice: ")
|
|
174
|
+
if ch == "1":
|
|
175
|
+
a, b = input("String 1: "), input("String 2: ")
|
|
176
|
+
dp = [[0]*(len(b)+1) for _ in range(len(a)+1)]
|
|
177
|
+
for i in range(1, len(a)+1):
|
|
178
|
+
for j in range(1, len(b)+1):
|
|
179
|
+
dp[i][j] = dp[i-1][j-1]+1 if a[i-1] == b[j-1] else max(dp[i-1][j], dp[i][j-1])
|
|
180
|
+
print("LCS length:", dp[-1][-1])
|
|
181
|
+
elif ch == "2":
|
|
182
|
+
p = read_nums(); n = int(input("Rod length (<= number of prices): "))
|
|
183
|
+
dp = [0]*(n+1)
|
|
184
|
+
for i in range(1, n+1):
|
|
185
|
+
dp[i] = max(p[j]+dp[i-j-1] for j in range(i))
|
|
186
|
+
print("Maximum revenue:", dp[n])
|
|
187
|
+
else:
|
|
188
|
+
dims = read_nums() # dimensions, e.g. 10 20 30 40
|
|
189
|
+
n = len(dims)-1; dp = [[0]*n for _ in range(n)]
|
|
190
|
+
for length in range(2, n+1):
|
|
191
|
+
for i in range(n-length+1):
|
|
192
|
+
j = i+length-1
|
|
193
|
+
dp[i][j] = min(dp[i][k]+dp[k+1][j]+dims[i]*dims[k+1]*dims[j+1] for k in range(i,j))
|
|
194
|
+
print("Minimum multiplications:", dp[0][n-1])
|
|
195
|
+
|
|
196
|
+
# 9. Greedy: activity selection (Huffman is more involved; this is a reusable greedy pattern)
|
|
197
|
+
def greedy():
|
|
198
|
+
n = int(input("Number of activities: "))
|
|
199
|
+
acts = []
|
|
200
|
+
for i in range(n):
|
|
201
|
+
start, finish = map(int, input(f"Activity {i+1} start finish: ").split())
|
|
202
|
+
acts.append((finish, start, i+1))
|
|
203
|
+
acts.sort()
|
|
204
|
+
chosen, end = [], -1
|
|
205
|
+
for finish, start, name in acts:
|
|
206
|
+
if start >= end: chosen.append(name); end = finish
|
|
207
|
+
print("Selected activity numbers:", chosen)
|
|
208
|
+
|
|
209
|
+
# 10. Approximation: set cover / vertex cover approximation
|
|
210
|
+
def approximation():
|
|
211
|
+
print("1 Set cover 2 Vertex cover")
|
|
212
|
+
ch = input("Choice: ")
|
|
213
|
+
if ch == "1":
|
|
214
|
+
universe = set(read_nums())
|
|
215
|
+
n = int(input("Number of sets: "))
|
|
216
|
+
sets = {}
|
|
217
|
+
for i in range(n): sets[f"S{i+1}"] = set(read_nums())
|
|
218
|
+
chosen = []
|
|
219
|
+
while universe:
|
|
220
|
+
best = max(sets, key=lambda k: len(sets[k] & universe))
|
|
221
|
+
covered = sets[best] & universe
|
|
222
|
+
if not covered: break
|
|
223
|
+
chosen.append(best); universe -= covered
|
|
224
|
+
print("Chosen sets:", chosen, "| Uncovered:", universe)
|
|
225
|
+
else:
|
|
226
|
+
n = int(input("Number of edges: "))
|
|
227
|
+
edges = []
|
|
228
|
+
for _ in range(n): edges.append(input("Edge u v: ").split())
|
|
229
|
+
cover = set()
|
|
230
|
+
while edges:
|
|
231
|
+
degree = {}
|
|
232
|
+
for u, v in edges:
|
|
233
|
+
degree[u] = degree.get(u, 0)+1; degree[v] = degree.get(v, 0)+1
|
|
234
|
+
v = max(degree, key=degree.get); cover.add(v)
|
|
235
|
+
edges = [(u, w) for u, w in edges if u != v and w != v]
|
|
236
|
+
print("Approximate vertex cover:", cover)
|
|
237
|
+
|
|
238
|
+
def main():
|
|
239
|
+
while True:
|
|
240
|
+
print("\\n=== AOA MASTER PRACTICAL ===")
|
|
241
|
+
print("1 Sorting 2 Matrix multiplication 3 Hiring problem")
|
|
242
|
+
print("4 Order statistics/counting sort 5 Binary tree")
|
|
243
|
+
print("6 Graphs 7 String matching 8 Dynamic programming")
|
|
244
|
+
print("9 Greedy activity selection 10 Approximation")
|
|
245
|
+
print("0 Exit")
|
|
246
|
+
ch = input("Select practical: ")
|
|
247
|
+
if ch == "0": break
|
|
248
|
+
try:
|
|
249
|
+
{"1": sorting, "2": matrix, "3": hiring, "4": order_stats,
|
|
250
|
+
"5": trees, "6": graphs, "7": strings, "8": dynamic,
|
|
251
|
+
"9": greedy, "10": approximation}[ch]()
|
|
252
|
+
except KeyError:
|
|
253
|
+
print("Choose a valid menu number.")
|
|
254
|
+
except (ValueError, IndexError, KeyError) as e:
|
|
255
|
+
print("Input issue - check the values and try again.", e)
|
|
256
|
+
|
|
257
|
+
if __name__ == "__main__":
|
|
258
|
+
main()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
from google import genai
|
|
4
|
+
from google.genai import types
|
|
5
|
+
|
|
6
|
+
os.environ["GEMINI_API_KEY"] = ""
|
|
7
|
+
|
|
8
|
+
user_prompt = "write a simple minimal without comments only code"
|
|
9
|
+
|
|
10
|
+
client = genai.Client()
|
|
11
|
+
response = client.models.generate_content(
|
|
12
|
+
model="gemini-3.6-flash",
|
|
13
|
+
contents=user_prompt,
|
|
14
|
+
config=types.GenerateContentConfig(
|
|
15
|
+
automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=True)
|
|
16
|
+
)
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
print(response.text)
|
|
20
|
+
|
|
21
|
+
pip install google-genai
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mathref
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A collection of numbered Python math references
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
|
|
8
|
+
A Python package containing numbered practical notes and code.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
python -m pip install maatraco-kit
|
|
12
|
+
|
|
13
|
+
## Run in Python IDLE
|
|
14
|
+
import runpy
|
|
15
|
+
runpy.run_module("maatracos", run_name="__main__")
|
|
16
|
+
|
|
17
|
+
Enter a maatraco number to display its contents. Enter 0 to exit.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
mathref/__init__.py
|
|
4
|
+
mathref/__main__.py
|
|
5
|
+
mathref.egg-info/PKG-INFO
|
|
6
|
+
mathref.egg-info/SOURCES.txt
|
|
7
|
+
mathref.egg-info/dependency_links.txt
|
|
8
|
+
mathref.egg-info/top_level.txt
|
|
9
|
+
mathref/mathref/01_maatraco.py
|
|
10
|
+
mathref/mathref/02_mathref.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mathref
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mathref"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A collection of numbered Python math references"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
|
|
12
|
+
[tool.setuptools.packages.find]
|
|
13
|
+
include = ["mathref*"]
|
|
14
|
+
|
|
15
|
+
[tool.setuptools.package-data]
|
|
16
|
+
mathref = ["maatracos/*.txt", "maatracos/*.docx", "maatracos/*.pdf"]
|
mathref-0.1.0/setup.cfg
ADDED