dkl9 commited on 2025-187 00:48:17
Showing 1 changed files, with 7 additions and 15 deletions.
| ... | ... |
@@ -1,10 +1,12 @@ |
| 1 | 1 |
import itertools |
| 2 | 2 |
import math |
| 3 |
-import random |
|
| 3 |
+import re |
|
| 4 |
+import sys |
|
| 4 | 5 |
import time |
| 5 | 6 |
import turtle |
| 6 | 7 |
import typing |
| 7 | 8 |
import collections.abc |
| 9 |
+import Levenshtein |
|
| 8 | 10 |
|
| 9 | 11 |
Func: typing.TypeAlias = collections.abc.Callable |
| 10 | 12 |
DistTable: typing.TypeAlias = list[list[float]] |
| ... | ... |
@@ -149,13 +151,8 @@ def scattered_hierarchy(hierarchy: BinaryTree) -> IndSeq: |
| 149 | 151 |
def total_len(distances: DistTable, seq: IndSeq) -> float: |
| 150 | 152 |
return sum(distances[i][j] for (i, j) in itertools.pairwise(seq)) |
| 151 | 153 |
|
| 152 |
-N = 100 |
|
| 153 |
-points: list[tuple[float, float]] = [(random.randint(0, N // 2), random.randint(0, N // 3)) for _ in range(N)] |
|
| 154 |
-print(points) |
|
| 155 |
-t: turtle.Turtle = turtle.Turtle() |
|
| 156 |
-t.hideturtle() |
|
| 157 |
-t.pen(speed=10) |
|
| 158 |
-dt: DistTable = distance_table(points, math.dist) |
|
| 154 |
+points: list[str] = [l.strip() for l in sys.stdin] |
|
| 155 |
+dt: DistTable = distance_table(points, lambda a, b: Levenshtein.distance(a, b, weights=(1, 1, 3))) |
|
| 159 | 156 |
print(show_mat(dt)) |
| 160 | 157 |
to = time.time() |
| 161 | 158 |
h: BinaryTree = distance_hierarchy(dt) |
| ... | ... |
@@ -167,16 +164,11 @@ METHODS = [ |
| 167 | 164 |
("nearest-nb sum", lambda: greedy_min_seq(dt, score_nearest)),
|
| 168 | 165 |
("maximin", lambda: greedy_maximin(dt)),
|
| 169 | 166 |
] |
| 170 |
-input("ready?")
|
|
| 171 | 167 |
for (name, method) in METHODS: |
| 172 | 168 |
to = time.time() |
| 173 | 169 |
seq = method() |
| 174 | 170 |
tf = time.time() |
| 175 | 171 |
l = total_len(dt, seq) |
| 176 | 172 |
print(f"{name} method took {tf - to} s to find {seq}, length {l}")
|
| 177 |
- t.clear() |
|
| 178 |
- for (i, (x, y)) in enumerate(points[k] for k in seq): |
|
| 179 |
- t.teleport(2000 / N * (x - 0.25 * N), 2000 / N * (y - 0.17 * N)) |
|
| 180 |
- t.dot(20 / math.sqrt(i + 1)) |
|
| 181 |
- t.write(i) |
|
| 182 |
- input("next?")
|
|
| 173 |
+ for k in seq[:int(math.sqrt(len(points)))]: |
|
| 174 |
+ print("\t" + points[k])
|
|
| 183 | 175 |