Skip to content

Commit 09618e8

Browse files
committed
Various ruff fixes
1 parent 7665ba5 commit 09618e8

25 files changed

+69
-70
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@
443443
* [Present Value](financial/present_value.py)
444444
* [Price Plus Tax](financial/price_plus_tax.py)
445445
* [Simple Moving Average](financial/simple_moving_average.py)
446+
* [Straight Line Depreciation](financial/straight_line_depreciation.py)
446447
* [Time And Half Pay](financial/time_and_half_pay.py)
447448

448449
## Fractals
@@ -790,6 +791,7 @@
790791
* [Sumset](maths/sumset.py)
791792
* [Sylvester Sequence](maths/sylvester_sequence.py)
792793
* [Tanh](maths/tanh.py)
794+
* [Test Factorial](maths/test_factorial.py)
793795
* [Test Prime Check](maths/test_prime_check.py)
794796
* [Three Sum](maths/three_sum.py)
795797
* [Trapezoidal Rule](maths/trapezoidal_rule.py)

ciphers/hill_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def replace_digits(self, num: int) -> str:
7979
>>> hill_cipher.replace_digits(26)
8080
'0'
8181
"""
82-
return self.key_string[round(num)]
82+
return self.key_string[(num)]
8383

8484
def check_determinant(self) -> None:
8585
"""

data_structures/binary_tree/non_recursive_segment_tree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@
3939
from __future__ import annotations
4040

4141
from collections.abc import Callable
42-
from typing import Any, Generic, TypeVar
42+
from typing import Any, TypeVar
4343

4444
T = TypeVar("T")
4545

4646

47-
class SegmentTree(Generic[T]):
47+
class SegmentTree[T]:
4848
def __init__(self, arr: list[T], fnc: Callable[[T, T], T]) -> None:
4949
"""
5050
Segment Tree constructor, it works just with commutative combiner.

data_structures/hashing/hash_map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
from collections.abc import Iterator, MutableMapping
1212
from dataclasses import dataclass
13-
from typing import Generic, TypeVar
13+
from typing import TypeVar
1414

1515
KEY = TypeVar("KEY")
1616
VAL = TypeVar("VAL")
1717

1818

1919
@dataclass(slots=True)
20-
class _Item(Generic[KEY, VAL]):
20+
class _Item[KEY, VAL]:
2121
key: KEY
2222
val: VAL
2323

data_structures/heap/heap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from abc import abstractmethod
44
from collections.abc import Iterable
5-
from typing import Generic, Protocol, TypeVar
5+
from typing import Protocol, TypeVar
66

77

88
class Comparable(Protocol):
@@ -22,7 +22,7 @@ def __eq__(self: T, other: object) -> bool:
2222
T = TypeVar("T", bound=Comparable)
2323

2424

25-
class Heap(Generic[T]):
25+
class Heap[T: Comparable]:
2626
"""A Max Heap Implementation
2727
2828
>>> unsorted = [103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5]

data_structures/heap/randomized_heap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
import random
66
from collections.abc import Iterable
7-
from typing import Any, Generic, TypeVar
7+
from typing import Any, TypeVar
88

99
T = TypeVar("T", bound=bool)
1010

1111

12-
class RandomizedHeapNode(Generic[T]):
12+
class RandomizedHeapNode[T: bool]:
1313
"""
1414
One node of the randomized heap. Contains the value and references to
1515
two children.
@@ -73,7 +73,7 @@ def merge(
7373
return root1
7474

7575

76-
class RandomizedHeap(Generic[T]):
76+
class RandomizedHeap[T: bool]:
7777
"""
7878
A data structure that allows inserting a new value and to pop the smallest
7979
values. Both operations take O(logN) time where N is the size of the

data_structures/heap/skew_heap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from __future__ import annotations
44

55
from collections.abc import Iterable, Iterator
6-
from typing import Any, Generic, TypeVar
6+
from typing import Any, TypeVar
77

88
T = TypeVar("T", bound=bool)
99

1010

11-
class SkewNode(Generic[T]):
11+
class SkewNode[T: bool]:
1212
"""
1313
One node of the skew heap. Contains the value and references to
1414
two children.
@@ -87,7 +87,7 @@ def merge(
8787
return result
8888

8989

90-
class SkewHeap(Generic[T]):
90+
class SkewHeap[T: bool]:
9191
"""
9292
A data structure that allows inserting a new value and to pop the smallest
9393
values. Both operations take O(logN) time where N is the size of the

data_structures/linked_list/skip_list.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
from itertools import pairwise
99
from random import random
10-
from typing import Generic, TypeVar
10+
from typing import TypeVar
1111

1212
KT = TypeVar("KT")
1313
VT = TypeVar("VT")
1414

1515

16-
class Node(Generic[KT, VT]):
16+
class Node[KT, VT]:
1717
def __init__(self, key: KT | str = "root", value: VT | None = None):
1818
self.key = key
1919
self.value = value
@@ -49,7 +49,7 @@ def level(self) -> int:
4949
return len(self.forward)
5050

5151

52-
class SkipList(Generic[KT, VT]):
52+
class SkipList[KT, VT]:
5353
def __init__(self, p: float = 0.5, max_level: int = 16):
5454
self.head: Node[KT, VT] = Node[KT, VT]()
5555
self.level = 0

data_structures/queues/queue_by_list.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
"""Queue represented by a Python list"""
22

33
from collections.abc import Iterable
4-
from typing import Generic, TypeVar
54

6-
_T = TypeVar("_T")
75

8-
9-
class QueueByList(Generic[_T]):
10-
def __init__(self, iterable: Iterable[_T] | None = None) -> None:
6+
class QueueByList[T]:
7+
def __init__(self, iterable: Iterable[T] | None = None) -> None:
118
"""
129
>>> QueueByList()
1310
Queue(())
@@ -16,7 +13,7 @@ def __init__(self, iterable: Iterable[_T] | None = None) -> None:
1613
>>> QueueByList((i**2 for i in range(1, 4)))
1714
Queue((1, 4, 9))
1815
"""
19-
self.entries: list[_T] = list(iterable or [])
16+
self.entries: list[T] = list(iterable or [])
2017

2118
def __len__(self) -> int:
2219
"""
@@ -58,7 +55,7 @@ def __repr__(self) -> str:
5855

5956
return f"Queue({tuple(self.entries)})"
6057

61-
def put(self, item: _T) -> None:
58+
def put(self, item: T) -> None:
6259
"""Put `item` to the Queue
6360
6461
>>> queue = QueueByList()
@@ -72,7 +69,7 @@ def put(self, item: _T) -> None:
7269

7370
self.entries.append(item)
7471

75-
def get(self) -> _T:
72+
def get(self) -> T:
7673
"""
7774
Get `item` from the Queue
7875
@@ -118,7 +115,7 @@ def rotate(self, rotation: int) -> None:
118115
for _ in range(rotation):
119116
put(get(0))
120117

121-
def get_front(self) -> _T:
118+
def get_front(self) -> T:
122119
"""Get the front item from the Queue
123120
124121
>>> queue = QueueByList((10, 20, 30))

data_structures/queues/queue_by_two_stacks.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
"""Queue implementation using two stacks"""
22

33
from collections.abc import Iterable
4-
from typing import Generic, TypeVar
54

6-
_T = TypeVar("_T")
75

8-
9-
class QueueByTwoStacks(Generic[_T]):
10-
def __init__(self, iterable: Iterable[_T] | None = None) -> None:
6+
class QueueByTwoStacks[T]:
7+
def __init__(self, iterable: Iterable[T] | None = None) -> None:
118
"""
129
>>> QueueByTwoStacks()
1310
Queue(())
@@ -16,8 +13,8 @@ def __init__(self, iterable: Iterable[_T] | None = None) -> None:
1613
>>> QueueByTwoStacks((i**2 for i in range(1, 4)))
1714
Queue((1, 4, 9))
1815
"""
19-
self._stack1: list[_T] = list(iterable or [])
20-
self._stack2: list[_T] = []
16+
self._stack1: list[T] = list(iterable or [])
17+
self._stack2: list[T] = []
2118

2219
def __len__(self) -> int:
2320
"""
@@ -59,7 +56,7 @@ def __repr__(self) -> str:
5956
"""
6057
return f"Queue({tuple(self._stack2[::-1] + self._stack1)})"
6158

62-
def put(self, item: _T) -> None:
59+
def put(self, item: T) -> None:
6360
"""
6461
Put `item` into the Queue
6562
@@ -74,7 +71,7 @@ def put(self, item: _T) -> None:
7471

7572
self._stack1.append(item)
7673

77-
def get(self) -> _T:
74+
def get(self) -> T:
7875
"""
7976
Get `item` from the Queue
8077

data_structures/stacks/stack.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Generic, TypeVar
3+
from typing import TypeVar
44

55
T = TypeVar("T")
66

@@ -13,7 +13,7 @@ class StackUnderflowError(BaseException):
1313
pass
1414

1515

16-
class Stack(Generic[T]):
16+
class Stack[T]:
1717
"""A stack is an abstract data type that serves as a collection of
1818
elements with two principal operations: push() and pop(). push() adds an
1919
element to the top of the stack, and pop() removes an element from the top

data_structures/stacks/stack_with_doubly_linked_list.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
from __future__ import annotations
55

6-
from typing import Generic, TypeVar
6+
from typing import TypeVar
77

88
T = TypeVar("T")
99

1010

11-
class Node(Generic[T]):
11+
class Node[T]:
1212
def __init__(self, data: T):
1313
self.data = data # Assign data
1414
self.next: Node[T] | None = None # Initialize next as null
1515
self.prev: Node[T] | None = None # Initialize prev as null
1616

1717

18-
class Stack(Generic[T]):
18+
class Stack[T]:
1919
"""
2020
>>> stack = Stack()
2121
>>> stack.is_empty()

data_structures/stacks/stack_with_singly_linked_list.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from __future__ import annotations
44

55
from collections.abc import Iterator
6-
from typing import Generic, TypeVar
6+
from typing import TypeVar
77

88
T = TypeVar("T")
99

1010

11-
class Node(Generic[T]):
11+
class Node[T]:
1212
def __init__(self, data: T):
1313
self.data = data
1414
self.next: Node[T] | None = None
@@ -17,7 +17,7 @@ def __str__(self) -> str:
1717
return f"{self.data}"
1818

1919

20-
class LinkedStack(Generic[T]):
20+
class LinkedStack[T]:
2121
"""
2222
Linked List Stack implementing push (to top),
2323
pop (from top) and is_empty

graphs/graph_adjacency_list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
import random
2222
import unittest
2323
from pprint import pformat
24-
from typing import Generic, TypeVar
24+
from typing import TypeVar
2525

2626
import pytest
2727

2828
T = TypeVar("T")
2929

3030

31-
class GraphAdjacencyList(Generic[T]):
31+
class GraphAdjacencyList[T]:
3232
def __init__(
3333
self, vertices: list[T], edges: list[list[T]], directed: bool = True
3434
) -> None:

graphs/graph_adjacency_matrix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
import random
2222
import unittest
2323
from pprint import pformat
24-
from typing import Generic, TypeVar
24+
from typing import TypeVar
2525

2626
import pytest
2727

2828
T = TypeVar("T")
2929

3030

31-
class GraphAdjacencyMatrix(Generic[T]):
31+
class GraphAdjacencyMatrix[T]:
3232
def __init__(
3333
self, vertices: list[T], edges: list[list[T]], directed: bool = True
3434
) -> None:

graphs/graph_list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
from __future__ import annotations
77

88
from pprint import pformat
9-
from typing import Generic, TypeVar
9+
from typing import TypeVar
1010

1111
T = TypeVar("T")
1212

1313

14-
class GraphAdjacencyList(Generic[T]):
14+
class GraphAdjacencyList[T]:
1515
"""
1616
Adjacency List type Graph Data Structure that accounts for directed and undirected
1717
Graphs. Initialize graph object indicating whether it's directed or undirected.

graphs/minimum_spanning_tree_kruskal2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
from __future__ import annotations
22

3-
from typing import Generic, TypeVar
3+
from typing import TypeVar
44

55
T = TypeVar("T")
66

77

8-
class DisjointSetTreeNode(Generic[T]):
8+
class DisjointSetTreeNode[T]:
99
# Disjoint Set Node to store the parent and rank
1010
def __init__(self, data: T) -> None:
1111
self.data = data
1212
self.parent = self
1313
self.rank = 0
1414

1515

16-
class DisjointSetTree(Generic[T]):
16+
class DisjointSetTree[T]:
1717
# Disjoint Set DataStructure
1818
def __init__(self) -> None:
1919
# map from node name to the node object
@@ -46,7 +46,7 @@ def union(self, data1: T, data2: T) -> None:
4646
self.link(self.find_set(data1), self.find_set(data2))
4747

4848

49-
class GraphUndirectedWeighted(Generic[T]):
49+
class GraphUndirectedWeighted[T]:
5050
def __init__(self) -> None:
5151
# connections: map from the node to the neighbouring nodes (with weights)
5252
self.connections: dict[T, dict[T, int]] = {}

0 commit comments

Comments
 (0)