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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ test-cargo:
cargo test --release

format:
$(UV) run ruff check --fix
$(UV) run ruff format
$(UV) run ruff check

lint:
$(UV) run ruff check --exit-non-zero-on-fix
Expand Down
6 changes: 3 additions & 3 deletions docs/data-structures/trie.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Checks if a word has been inserted into the trie.
t = Trie()
t.insert("apple")
assert t.search("apple") == True
assert t.search("app") == False
assert t.search("app") == False
```

---
Expand All @@ -111,7 +111,7 @@ Checks if any inserted word starts with the given prefix.
t = Trie()
t.insert("apple")
t.insert("application")
assert t.starts_with("app") == True
assert t.starts_with("app") == True
assert t.starts_with("appl") == True
assert t.starts_with("banana") == False
assert t.starts_with("") == True
Expand Down Expand Up @@ -140,7 +140,7 @@ t.insert("app")

assert t.delete("apple") == True
assert t.delete("apple") == False
assert t.search("app") == True
assert t.search("app") == True
```

---
Expand Down
9 changes: 0 additions & 9 deletions fast_collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Trie:

def __init__(self) -> None:
"""Initialize an empty trie."""
...

def insert(self, word: str) -> None:
"""
Expand All @@ -45,7 +44,6 @@ class Trie:
>>> "hello" in t
True
"""
...

def search(self, word: str) -> bool:
"""
Expand All @@ -70,7 +68,6 @@ class Trie:
>>> t.search("app")
False
"""
...

def starts_with(self, prefix: str) -> bool:
"""
Expand All @@ -95,7 +92,6 @@ class Trie:
>>> t.starts_with("app")
True
"""
...

def delete(self, word: str) -> bool:
"""
Expand Down Expand Up @@ -123,7 +119,6 @@ class Trie:
>>> t.delete("apple")
True
"""
...

def word_count(self) -> int:
"""
Expand All @@ -140,7 +135,6 @@ class Trie:
>>> t.word_count()
1
"""
...

def collect_words(self) -> list[str]:
"""
Expand All @@ -163,7 +157,6 @@ class Trie:
>>> t.collect_words()
['apple', 'banana']
"""
...

def __contains__(self, word: str) -> bool:
"""
Expand All @@ -179,7 +172,6 @@ class Trie:
>>> "app" in t
False
"""
...

def __len__(self) -> int:
"""
Expand All @@ -194,4 +186,3 @@ class Trie:
>>> len(t)
2
"""
...
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest

from fast_collections import Trie


@pytest.fixture
def trie():
return Trie()
54 changes: 0 additions & 54 deletions tests/test_ds/test_trie/test_trie.py

This file was deleted.

64 changes: 64 additions & 0 deletions tests/test_trie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import pytest


def _insert_all(trie, words):
for w in words:
trie.insert(w)


@pytest.mark.parametrize(
"words", [[], ["hello"], ["hello", "world"], ["", "a", "b", "c"], ["a", "a", "b"]]
)
def test_size_and_word_count(trie, words):
"Test that the trie has the correct size and word count"
_insert_all(trie, words)
expected = len(set(words))
assert len(trie) == expected
assert trie.word_count() == expected


@pytest.mark.parametrize(
"words, present, absent",
[
(
[
"hello",
],
[
"hello",
],
["hell", "world"],
),
([], [], ["hello"]),
(["a", "b", "c"], ["a", "b", "c"], []),
(["a", "a", "b"], ["a", "b"], ["c"]),
],
)
def test_contains(trie, words, present, absent):
"Test that the trie contains the correct words"
_insert_all(trie, words)

for w in present:
assert w in trie

for w in absent:
assert w not in trie
assert not trie.search(w)


@pytest.mark.parametrize(
"words, prefix, expected",
[
(["hello"], "hello", True),
(["hello"], "hell", True),
(["hello"], "h", True),
(["hello"], "", True),
(["hello"], "help", False),
(["hello", "world"], "wo", True),
(["hello", "world"], "worl", True),
(["hello", "world"], "x", False),
],
)
def test_starts_with(trie, words, prefix, expected):
_insert_all(trie, words)
assert trie.starts_with(prefix) is expected
Loading