Update 2015 solutions
This commit is contained in:
33
lib.py
33
lib.py
@@ -226,7 +226,7 @@ def count_trailing_repeats(lst):
|
||||
class A_Star(object):
|
||||
def __init__(self, starts, is_goal, h, d, neighbors):
|
||||
"""
|
||||
:param h: heuristic function
|
||||
:param h: heuristic function (never overestimate)
|
||||
:param d: cost from node to node function
|
||||
:param neighbors: neighbors function
|
||||
"""
|
||||
@@ -297,6 +297,35 @@ def mod_inverse(a, m):
|
||||
|
||||
g, x, _ = egcd(a, m)
|
||||
if g != 1:
|
||||
raise Exception('Modular inverse does not exist')
|
||||
raise Exception("Modular inverse does not exist")
|
||||
else:
|
||||
return x % m
|
||||
|
||||
|
||||
class V:
|
||||
def __init__(self, *args):
|
||||
self.xs: tuple[int] = tuple(args)
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, V):
|
||||
return all(v1 == v2 for v1, v2 in zip(self.xs, other.xs))
|
||||
elif hasattr(other, "__len__") and len(self.xs) == len(other):
|
||||
return all(v1 == v2 for v1, v2 in zip(self.xs, other))
|
||||
return False
|
||||
|
||||
def __getitem__(self, i: int):
|
||||
return self.xs[i]
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.xs)
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, V):
|
||||
return V(*[v1 + v2 for v1, v2 in zip(self.xs, other.xs)])
|
||||
assert hasattr(other, "__len__"), f"V.__add__({self}, {other}) missing `len`"
|
||||
assert len(self.xs) == len(other), f"V.__add__({self}, {other}) `len` mismatch"
|
||||
return V(*[v1 + v2 for v1, v2 in zip(self.xs, other)])
|
||||
|
||||
def __repr__(self):
|
||||
s = ", ".join(map(str, self.xs))
|
||||
return f"V({s})"
|
||||
|
||||
Reference in New Issue
Block a user