|
| 1 | +import pytest |
| 2 | + |
| 3 | +from leetcode_py.test_utils import logged_test |
| 4 | + |
| 5 | +from .solution import Solution |
| 6 | + |
| 7 | + |
| 8 | +class TestWordLadder: |
| 9 | + def setup_method(self): |
| 10 | + self.solution = Solution() |
| 11 | + |
| 12 | + @pytest.mark.parametrize( |
| 13 | + "begin_word, end_word, word_list, expected", |
| 14 | + [ |
| 15 | + # Basic cases |
| 16 | + ("hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"], 5), |
| 17 | + ("hit", "cog", ["hot", "dot", "dog", "lot", "log"], 0), |
| 18 | + ("a", "c", ["a", "b", "c"], 2), |
| 19 | + # Edge cases |
| 20 | + ("hot", "dog", ["hot", "dog"], 0), # No intermediate words |
| 21 | + ("hot", "hot", ["hot"], 1), # Same word |
| 22 | + ("cat", "dog", [], 0), # Empty word list |
| 23 | + ("cat", "dog", ["cat"], 0), # End word not in list |
| 24 | + # Single character changes |
| 25 | + ("a", "b", ["a", "b"], 2), |
| 26 | + ("ab", "cd", ["ab", "ad", "cd"], 3), |
| 27 | + # Longer paths |
| 28 | + ("red", "tax", ["ted", "tex", "red", "tax", "tad", "den", "rex", "pee"], 4), |
| 29 | + # Multiple possible paths (should return shortest) |
| 30 | + ("cat", "dog", ["cat", "bat", "bag", "dag", "dog", "cag", "cog"], 4), |
| 31 | + # No path exists |
| 32 | + ("abc", "def", ["abc", "def", "ghi"], 0), |
| 33 | + # Direct transformation |
| 34 | + ("cat", "bat", ["cat", "bat"], 2), |
| 35 | + # Longer word length |
| 36 | + ("word", "form", ["word", "worm", "form", "foam", "flam", "flab"], 3), |
| 37 | + ], |
| 38 | + ) |
| 39 | + @logged_test |
| 40 | + def test_ladder_length(self, begin_word: str, end_word: str, word_list: list[str], expected: int): |
| 41 | + result = self.solution.ladder_length(begin_word, end_word, word_list) |
| 42 | + assert result == expected |
0 commit comments