Compare commits

..

No commits in common. "17c41a0c638787d05a83dc94309982e12c2f344d" and "6adb1ff457f60fc3bf7c8385235f7a247edf4c50" have entirely different histories.

5 changed files with 78 additions and 43 deletions

View File

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

View File

@ -6,21 +6,6 @@ from typing import Union, List, Iterable
from .tools import minmax 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): class DistanceAlgorithm(Enum):
MANHATTAN = 0 MANHATTAN = 0
EUCLIDEAN = 1 EUCLIDEAN = 1
@ -172,17 +157,36 @@ class Coordinate(tuple):
:return: list of Coordinate :return: list of Coordinate
""" """
if self[2] is None: if self[2] is None:
nb_list = [x * dist for x in NEIGHBOURS]
if includeDiagonal: if includeDiagonal:
nb_list += [x * dist for x in DIAGONAL_NEIGHBOURS] 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)]
for dx, dy in nb_list: for dx, dy in nb_list:
if minX <= self[0] + dx <= maxX and minY <= self[1] + dy <= maxY: if minX <= self[0] + dx <= maxX and minY <= self[1] + dy <= maxY:
yield self.__class__(self[0] + dx, self[1] + dy) yield self.__class__(self[0] + dx, self[1] + dy)
else: else:
nb_list = [x * dist for x in NEIGHBOURS_3D]
if includeDiagonal: if includeDiagonal:
nb_list += [x * dist for x in DIAGONAL_NEIGHBOURS_3D] 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),
]
for dx, dy, dz in nb_list: 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: 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 @dataclass
class Node: class Node:
value: Any value: Any
next: Node = None next: "Node" = None
prev: Node = None prev: "Node" = None
class LinkedList: class LinkedList:

View File

@ -2,6 +2,8 @@ import datetime
import inspect import inspect
import os.path import os.path
import sys import sys
from fishhook import hook
from functools import wraps
from typing import Any from typing import Any
@ -138,3 +140,51 @@ def human_readable_time_from_ns(ns: int) -> str:
time_parts.insert(0, "%d%s" % (p, unit)) time_parts.insert(0, "%d%s" % (p, unit))
if ns == 0: if ns == 0:
return ", ".join(time_parts) 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

View File

@ -1,20 +0,0 @@
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