Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions graphs/graph_adjacency_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def add_vertex(self, vertex: T) -> None:
"""
Adds a vertex to the graph. If the given vertex already exists,
a ValueError will be thrown.

>>> g = GraphAdjacencyList(vertices=[], edges=[], directed=False)
>>> g.add_vertex("A")
>>> g.adj_list
{'A': []}
>>> g.add_vertex("A")
Traceback (most recent call last):
...
ValueError: Incorrect input: A is already in the graph.
"""
if self.contains_vertex(vertex):
msg = f"Incorrect input: {vertex} is already in the graph."
Expand Down
8 changes: 7 additions & 1 deletion machine_learning/apriori_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Examples: https://www.kaggle.com/code/earthian/apriori-association-rules-mining
"""

from collections import Counter
from itertools import combinations


Expand Down Expand Up @@ -44,11 +45,16 @@ def prune(itemset: list, candidates: list, length: int) -> list:
>>> prune(itemset, candidates, 3)
[]
"""
itemset_counter = Counter(tuple(item) for item in itemset)
pruned = []
for candidate in candidates:
is_subsequence = True
for item in candidate:
if item not in itemset or itemset.count(item) < length - 1:
item_tuple = tuple(item)
if (
item_tuple not in itemset_counter
or itemset_counter[item_tuple] < length - 1
):
is_subsequence = False
break
if is_subsequence:
Expand Down
6 changes: 6 additions & 0 deletions maths/test_factorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ def test_negative_number(function):
function(-3)


@pytest.mark.parametrize("function", [factorial, factorial_recursive])
def test_float_number(function):
with pytest.raises(ValueError):
function(1.5)


if __name__ == "__main__":
pytest.main(["-v", __file__])