Compare commits

...

2 Commits

5 changed files with 43 additions and 78 deletions

View File

@ -1,5 +1,4 @@
bs4~=0.0.1
beautifulsoup4~=4.12.2
fishhook~=0.2.9
requests~=2.31.0
beautifulsoup4~=4.12
requests~=2.32
tqdm~=4.66

View File

@ -6,6 +6,21 @@ from typing import Union, List, Iterable
from .tools import minmax
NEIGHBOURS = [(0, -1), (1, 0), (0, 1), (-1, 0)]
NEIGHBOURS_3D = [(0, -1, 0), (0, 0, -1), (1, 0, 0), (0, 0, -1), (0, 1, 0), (-1, 0, 0)]
DIAGONAL_NEIGHBOURS = [(-1, -1), (1, -1), (-1, 1), (1, 1)]
DIAGONAL_NEIGHBOURS_3D = [
(-1, -1, -1),
(1, -1, -1),
(-1, -1, 1),
(1, -1, 1),
(-1, 1, -1),
(-1, 1, 1),
(1, 1, -1),
(1, 1, 1),
]
class DistanceAlgorithm(Enum):
MANHATTAN = 0
EUCLIDEAN = 1
@ -157,36 +172,17 @@ class Coordinate(tuple):
:return: list of Coordinate
"""
if self[2] is None:
nb_list = [x * dist for x in NEIGHBOURS]
if includeDiagonal:
nb_list = [
(-dist, -dist),
(-dist, 0),
(-dist, dist),
(0, -dist),
(0, dist),
(dist, -dist),
(dist, 0),
(dist, dist),
]
else:
nb_list = [(-dist, 0), (dist, 0), (0, -dist), (0, dist)]
nb_list += [x * dist for x in DIAGONAL_NEIGHBOURS]
for dx, dy in nb_list:
if minX <= self[0] + dx <= maxX and minY <= self[1] + dy <= maxY:
yield self.__class__(self[0] + dx, self[1] + dy)
else:
nb_list = [x * dist for x in NEIGHBOURS_3D]
if includeDiagonal:
nb_list = [(x, y, z) for x in [-dist, 0, dist] for y in [-dist, 0, dist] for z in [-dist, 0, dist]]
nb_list.remove((0, 0, 0))
else:
nb_list = [
(-dist, 0, 0),
(0, -dist, 0),
(dist, 0, 0),
(0, dist, 0),
(0, 0, dist),
(0, 0, -dist),
]
nb_list += [x * dist for x in DIAGONAL_NEIGHBOURS_3D]
for dx, dy, dz in nb_list:
if minX <= self[0] + dx <= maxX and minY <= self[1] + dy <= maxY and minZ <= self[2] + dz <= maxZ:

View File

@ -6,8 +6,8 @@ from typing import Any
@dataclass
class Node:
value: Any
next: "Node" = None
prev: "Node" = None
next: Node = None
prev: Node = None
class LinkedList:

View File

@ -2,8 +2,6 @@ import datetime
import inspect
import os.path
import sys
from fishhook import hook
from functools import wraps
from typing import Any
@ -140,51 +138,3 @@ def human_readable_time_from_ns(ns: int) -> str:
time_parts.insert(0, "%d%s" % (p, unit))
if ns == 0:
return ", ".join(time_parts)
def cache(func):
saved = {}
@wraps(func)
def new_func(*args):
if args in saved:
return saved[args]
result = func(*args)
saved[args] = result
return result
return new_func
@hook(list)
def intersection(self, *args) -> list:
ret = set(self).intersection(*args)
return list(ret)
@hook(list)
def __and__(self, *args) -> list:
return self.intersection(*args)
@hook(str)
def intersection(self, *args) -> str:
ret = set(self).intersection(*args)
return "".join(list(ret))
@hook(str)
def __and__(self, *args) -> str:
return self.intersection(*args)
@hook(int)
def sum_digits(self) -> int:
s = 0
num = self
while num > 0:
s += num % 10
num //= 10
return s

20
src/tools/types.py Normal file
View File

@ -0,0 +1,20 @@
from __future__ import annotations
from collections.abc import Iterable
class Integer(int):
def digits(self) -> Iterable[int]:
for x in str(self):
yield int(x)
def digits_sum(self) -> int:
return sum(int(x) for x in str(self))
class String(str):
def swap(self, x: int, y: int) -> String:
return String(self[:x] + self[y] + self[x + 1 : y] + self[x] + self[y + 1 :])
class List(list):
pass