From c03f16dc4c823f7a18340a45105dd40424cbe1bf Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Wed, 1 May 2024 18:58:38 +0000 Subject: [PATCH 01/11] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f6d6cb463faa..4a053a3f1b7f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -773,6 +773,7 @@ * [Inverse Of Matrix](matrix/inverse_of_matrix.py) * [Largest Square Area In Matrix](matrix/largest_square_area_in_matrix.py) * [Matrix Class](matrix/matrix_class.py) + * [Matrix Equalization](matrix/matrix_equalization.py) * [Matrix Multiplication Recursion](matrix/matrix_multiplication_recursion.py) * [Matrix Operation](matrix/matrix_operation.py) * [Max Area Of Island](matrix/max_area_of_island.py) From eea84edcd5c3ca5bd5e747226ecf25d935050aac Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Wed, 8 May 2024 00:35:02 +0300 Subject: [PATCH 02/11] Fix some RUF012 per file ignores --- other/lfu_cache.py | 4 ++-- other/lru_cache.py | 4 ++-- pyproject.toml | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/other/lfu_cache.py b/other/lfu_cache.py index 788fdf19bb60..53120c39c48d 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -1,7 +1,7 @@ from __future__ import annotations from collections.abc import Callable -from typing import Generic, TypeVar +from typing import ClassVar, Generic, TypeVar T = TypeVar("T") U = TypeVar("U") @@ -197,7 +197,7 @@ class LFUCache(Generic[T, U]): """ # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: dict[Callable[[T], U], LFUCache[T, U]] = {} + decorator_function_to_instance_map: ClassVar[dict[Callable[[T], U], LFUCache[T, U]]] = {} def __init__(self, capacity: int): self.list: DoubleLinkedList[T, U] = DoubleLinkedList() diff --git a/other/lru_cache.py b/other/lru_cache.py index 1e5eeac45b4e..5bdb41977650 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -1,7 +1,7 @@ from __future__ import annotations from collections.abc import Callable -from typing import Generic, TypeVar +from typing import ClassVar, Generic, TypeVar T = TypeVar("T") U = TypeVar("U") @@ -210,7 +210,7 @@ class LRUCache(Generic[T, U]): """ # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: dict[Callable[[T], U], LRUCache[T, U]] = {} + decorator_function_to_instance_map: ClassVar[dict[Callable[[T], U], LRUCache[T, U]]] = {} def __init__(self, capacity: int): self.list: DoubleLinkedList[T, U] = DoubleLinkedList() diff --git a/pyproject.toml b/pyproject.toml index 4c512ca896b4..bd8d1aea4c60 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -85,7 +85,6 @@ max-complexity = 17 # default: 10 "machine_learning/decision_tree.py" = ["SIM114"] "machine_learning/sequential_minimum_optimization.py" = ["SIM115"] "matrix/sherman_morrison.py" = ["SIM103", "SIM114"] -"other/l*u_cache.py" = ["RUF012"] "physics/newtons_second_law_of_motion.py" = ["BLE001"] "project_euler/problem_099/sol1.py" = ["SIM115"] "sorts/external_sort.py" = ["SIM115"] From 2c6a49aa6e5f812dc920e4b63d01b85b2fad1d62 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 10 May 2024 22:02:18 +0000 Subject: [PATCH 03/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/lfu_cache.py | 4 +++- other/lru_cache.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/other/lfu_cache.py b/other/lfu_cache.py index 53120c39c48d..c3158d061d2a 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -197,7 +197,9 @@ class LFUCache(Generic[T, U]): """ # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: ClassVar[dict[Callable[[T], U], LFUCache[T, U]]] = {} + decorator_function_to_instance_map: ClassVar[ + dict[Callable[[T], U], LFUCache[T, U]] + ] = {} def __init__(self, capacity: int): self.list: DoubleLinkedList[T, U] = DoubleLinkedList() diff --git a/other/lru_cache.py b/other/lru_cache.py index 5bdb41977650..f49365241c29 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -210,7 +210,9 @@ class LRUCache(Generic[T, U]): """ # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: ClassVar[dict[Callable[[T], U], LRUCache[T, U]]] = {} + decorator_function_to_instance_map: ClassVar[ + dict[Callable[[T], U], LRUCache[T, U]] + ] = {} def __init__(self, capacity: int): self.list: DoubleLinkedList[T, U] = DoubleLinkedList() From 2a9b649ad2f1e97937ab31c7937809d4dd25f4dd Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 11 May 2024 01:13:35 +0300 Subject: [PATCH 04/11] Fix --- other/lfu_cache.py | 2 +- other/lru_cache.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/other/lfu_cache.py b/other/lfu_cache.py index c3158d061d2a..75a5a175a23e 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -197,7 +197,7 @@ class LFUCache(Generic[T, U]): """ # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: ClassVar[ + decorator_function_to_instance_map: ClassVar[ # type: ignore[misc] dict[Callable[[T], U], LFUCache[T, U]] ] = {} diff --git a/other/lru_cache.py b/other/lru_cache.py index f49365241c29..cca5cbd14cd1 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -210,7 +210,7 @@ class LRUCache(Generic[T, U]): """ # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: ClassVar[ + decorator_function_to_instance_map: ClassVar[ # type: ignore[misc] dict[Callable[[T], U], LRUCache[T, U]] ] = {} From 432474acfbd430e99fcbfb3ec04f4db18dce7006 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 11 May 2024 17:40:44 +0300 Subject: [PATCH 05/11] Fix --- other/lfu_cache.py | 7 +++---- other/lru_cache.py | 6 ++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/other/lfu_cache.py b/other/lfu_cache.py index 75a5a175a23e..f9a31dbcce9a 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -1,7 +1,7 @@ from __future__ import annotations from collections.abc import Callable -from typing import ClassVar, Generic, TypeVar +from typing import Generic, TypeVar T = TypeVar("T") U = TypeVar("U") @@ -197,9 +197,8 @@ class LFUCache(Generic[T, U]): """ # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: ClassVar[ # type: ignore[misc] - dict[Callable[[T], U], LFUCache[T, U]] - ] = {} + decorator_function_to_instance_map: dict[Callable[[T], U], LFUCache[T, U]] = {} # noqa: RUF012 + def __init__(self, capacity: int): self.list: DoubleLinkedList[T, U] = DoubleLinkedList() diff --git a/other/lru_cache.py b/other/lru_cache.py index cca5cbd14cd1..186ff919a3cb 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -1,7 +1,7 @@ from __future__ import annotations from collections.abc import Callable -from typing import ClassVar, Generic, TypeVar +from typing import Generic, TypeVar T = TypeVar("T") U = TypeVar("U") @@ -210,9 +210,7 @@ class LRUCache(Generic[T, U]): """ # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: ClassVar[ # type: ignore[misc] - dict[Callable[[T], U], LRUCache[T, U]] - ] = {} + decorator_function_to_instance_map: dict[Callable[[T], U], LRUCache[T, U]] = {} # noqa: RUF012 def __init__(self, capacity: int): self.list: DoubleLinkedList[T, U] = DoubleLinkedList() From 54cb87fed9f1a18b71fc6f720ee692faaf47f180 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 May 2024 14:41:12 +0000 Subject: [PATCH 06/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/lfu_cache.py | 1 - 1 file changed, 1 deletion(-) diff --git a/other/lfu_cache.py b/other/lfu_cache.py index f9a31dbcce9a..2dee25d0374e 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -199,7 +199,6 @@ class LFUCache(Generic[T, U]): # class variable to map the decorator functions to their respective instance decorator_function_to_instance_map: dict[Callable[[T], U], LFUCache[T, U]] = {} # noqa: RUF012 - def __init__(self, capacity: int): self.list: DoubleLinkedList[T, U] = DoubleLinkedList() self.capacity = capacity From a12d419ed7522c577ed0ce6ac1c5da34bf1e487e Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sun, 22 Dec 2024 23:35:40 +0300 Subject: [PATCH 07/11] Fix --- pyproject.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5eade63817c0..c23fb043c0bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -104,7 +104,6 @@ lint.select = [ # "TCH", # flake8-type-checking # "TRY", # tryceratops ] - lint.ignore = [ # `ruff rule S101` for a description of that rule "B904", # Within an `except` clause, raise exceptions with `raise ... from err` -- FIX ME @@ -136,9 +135,6 @@ lint.per-file-ignores."machine_learning/sequential_minimum_optimization.py" = [ lint.per-file-ignores."matrix/sherman_morrison.py" = [ "SIM103", ] -lint.per-file-ignores."other/l*u_cache.py" = [ - "RUF012", -] lint.per-file-ignores."physics/newtons_second_law_of_motion.py" = [ "BLE001", ] From 02dbed58727730dd63eb62cbba0baf692ced3b4d Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sun, 22 Dec 2024 23:37:40 +0300 Subject: [PATCH 08/11] Improve --- other/lru_cache.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/other/lru_cache.py b/other/lru_cache.py index 186ff919a3cb..9854a5eb4b0e 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -209,9 +209,6 @@ class LRUCache(Generic[T, U]): CacheInfo(hits=194, misses=99, capacity=100, current size=99) """ - # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: dict[Callable[[T], U], LRUCache[T, U]] = {} # noqa: RUF012 - def __init__(self, capacity: int): self.list: DoubleLinkedList[T, U] = DoubleLinkedList() self.capacity = capacity @@ -308,18 +305,21 @@ def decorator( """ def cache_decorator_inner(func: Callable[[T], U]) -> Callable[..., U]: + # variable to map the decorator functions to their respective instance + decorator_function_to_instance_map: dict[Callable[[T], U], LRUCache[T, U]] = {} + def cache_decorator_wrapper(*args: T) -> U: - if func not in cls.decorator_function_to_instance_map: - cls.decorator_function_to_instance_map[func] = LRUCache(size) + if func not in decorator_function_to_instance_map: + decorator_function_to_instance_map[func] = LRUCache(size) - result = cls.decorator_function_to_instance_map[func].get(args[0]) + result = decorator_function_to_instance_map[func].get(args[0]) if result is None: result = func(*args) - cls.decorator_function_to_instance_map[func].put(args[0], result) + decorator_function_to_instance_map[func].put(args[0], result) return result def cache_info() -> LRUCache[T, U]: - return cls.decorator_function_to_instance_map[func] + return decorator_function_to_instance_map[func] setattr(cache_decorator_wrapper, "cache_info", cache_info) # noqa: B010 From 61ec9029be4743055b488aba1029edabc0a7942a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Dec 2024 20:38:26 +0000 Subject: [PATCH 09/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/lru_cache.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/other/lru_cache.py b/other/lru_cache.py index 9854a5eb4b0e..4f0c843c86cc 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -306,7 +306,9 @@ def decorator( def cache_decorator_inner(func: Callable[[T], U]) -> Callable[..., U]: # variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: dict[Callable[[T], U], LRUCache[T, U]] = {} + decorator_function_to_instance_map: dict[ + Callable[[T], U], LRUCache[T, U] + ] = {} def cache_decorator_wrapper(*args: T) -> U: if func not in decorator_function_to_instance_map: From 98c0f97cbc861b0f12f993b036a6fe9e9e016605 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sun, 22 Dec 2024 23:42:18 +0300 Subject: [PATCH 10/11] Improve --- other/lfu_cache.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/other/lfu_cache.py b/other/lfu_cache.py index 2dee25d0374e..8ad56dc3fa54 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -196,9 +196,6 @@ class LFUCache(Generic[T, U]): CacheInfo(hits=196, misses=100, capacity=100, current_size=100) """ - # class variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: dict[Callable[[T], U], LFUCache[T, U]] = {} # noqa: RUF012 - def __init__(self, capacity: int): self.list: DoubleLinkedList[T, U] = DoubleLinkedList() self.capacity = capacity @@ -291,18 +288,21 @@ def decorator( """ def cache_decorator_inner(func: Callable[[T], U]) -> Callable[..., U]: + # variable to map the decorator functions to their respective instance + decorator_function_to_instance_map: dict[Callable[[T], U], LFUCache[T, U]] = {} + def cache_decorator_wrapper(*args: T) -> U: - if func not in cls.decorator_function_to_instance_map: - cls.decorator_function_to_instance_map[func] = LFUCache(size) + if func not in decorator_function_to_instance_map: + decorator_function_to_instance_map[func] = LFUCache(size) - result = cls.decorator_function_to_instance_map[func].get(args[0]) + result = decorator_function_to_instance_map[func].get(args[0]) if result is None: result = func(*args) - cls.decorator_function_to_instance_map[func].put(args[0], result) + decorator_function_to_instance_map[func].put(args[0], result) return result def cache_info() -> LFUCache[T, U]: - return cls.decorator_function_to_instance_map[func] + return decorator_function_to_instance_map[func] setattr(cache_decorator_wrapper, "cache_info", cache_info) # noqa: B010 From d97fe7ad61cb52bf1c0650c1ddb98036fa94cc40 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Dec 2024 20:42:49 +0000 Subject: [PATCH 11/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/lfu_cache.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/other/lfu_cache.py b/other/lfu_cache.py index 8ad56dc3fa54..5a143c739b9d 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -289,7 +289,9 @@ def decorator( def cache_decorator_inner(func: Callable[[T], U]) -> Callable[..., U]: # variable to map the decorator functions to their respective instance - decorator_function_to_instance_map: dict[Callable[[T], U], LFUCache[T, U]] = {} + decorator_function_to_instance_map: dict[ + Callable[[T], U], LFUCache[T, U] + ] = {} def cache_decorator_wrapper(*args: T) -> U: if func not in decorator_function_to_instance_map: