Created
October 27, 2022 19:00
-
-
Save zhukovgreen/819b2877c697b194cd82c3c4e6f17ec5 to your computer and use it in GitHub Desktop.
Task with longest chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
def longest_chain_of_words( | |
inputs: tuple[str, ...], | |
) -> int: | |
"""Find the longest chain of words within an array matching the rule. | |
The rule is: | |
Removing one character from a word, gives you a new word | |
which is in the remaining part of the array of words. | |
Example: | |
Array: | |
("a", "b", "ba", "bca", "bda", "bdca") | |
Answer is 4, because: | |
bd~c~a == bda (present in the array), +1 to the chain | |
b~d~a == ba, +1 to the chain | |
b~a~ == b, +1 to the chain | |
chain length is 4 | |
""" | |
assert inputs, "expect not empty tuple of strings" | |
# TODO finish the implementation | |
@pytest.mark.parametrize( | |
("inputs", "exp"), | |
( | |
(("a", "b", "ba", "bca", "bda", "bdca"), 4), | |
(("a", "b", "ba", "bca", "bdca", "bda"), 4), | |
(("a", "b", "bac", "bca", "bda", "bdca"), 2), | |
(("a", "b", "and", "an", "bac", "bca", "bda", "bdca"), 3), | |
(("a", "and", "an", "bear"), 3), | |
(("a", "b"), 1), | |
(("a", "a"), 1), | |
(("x", "yz"), 1), | |
(("x",), 1), | |
(("x", "xz"), 2), | |
(("xz", "x"), 2), | |
), | |
) | |
def test_case(inputs, exp): | |
assert longest_chain_of_words(inputs) == exp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My solution in python looks like this:
Thank you @yruslan