aboutsummaryrefslogtreecommitdiff
path: root/challenge-064/lubos-kolouch/python/ch-2.py
blob: df2d2041c3bf0af4f2b2c7edaa47e508d404a077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import List


def word_sequence(S: str, W: List[str]) -> List[str]:

    def helper(s: str, words: List[str]) -> List[str]:
        if not s:
            return words
        for word in W:
            if s.startswith(word):
                result = helper(s[len(word):], words + [word])
                if result:
                    return result
        return []

    result = helper(S, [])
    return result if result else [0]


S1 = "perlweeklychallenge"
W1 = ["weekly", "challenge", "perl"]

S2 = "perlandraku"
W2 = ["python", "ruby", "haskell"]

print(word_sequence(S1, W1))  # Output: ['perl', 'weekly', 'challenge']
print(word_sequence(S2, W2))  # Output: [0]