Created
December 6, 2025 13:19
-
-
Save mypy-play/fa5a783524d45090261efe7413488078 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or 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
| # from ib111 import week_11 # noqa | |
| # Napište čistou funkci, která na základě daného vzoru vytvoří | |
| # množinu všech odpovídajících řetězců. Vzor je tvořený | |
| # alfanumerickými znaky a navíc může obsahovat hranaté závorky – | |
| # znaky ‹[› a ‹]›. Mezi těmito závorkami může stát libovolný počet | |
| # přípustných znaků (krom samotných hranatých závorek) a na daném | |
| # místě se ve výsledném řetězci může nacházet libovolný z těchto | |
| # znaků. Například vzor ‹a[bc]d› reprezentuje řetězce ‹abd› a ‹acd›. | |
| def resolve_template(template: str) -> set[str]: | |
| blocks: list[list[str]] = [] | |
| current: list[str] = [] | |
| temp: bool = False | |
| for item in template: | |
| if item == '[': | |
| temp = True | |
| current = [] | |
| continue | |
| if item == ']': | |
| temp = False | |
| blocks.append(current) | |
| current = [] | |
| continue | |
| if temp: | |
| current.append(item) | |
| else: | |
| blocks.append([item]) | |
| if temp: | |
| return set() | |
| for b in blocks: | |
| if b == []: | |
| return set() | |
| return combine(blocks, 0) | |
| def combine(blocks: list[list[str]], index: int) -> set[str]: | |
| result: set[str] = set() | |
| if index == len(blocks): | |
| result.add("") | |
| return result | |
| current = blocks[index] | |
| tail = combine(blocks, index + 1) | |
| for item in current: | |
| for t in tail: | |
| result.add(item + t) | |
| return result | |
| def main() -> None: | |
| assert resolve_template("[]") == set() | |
| assert resolve_template("a") == {"a"} | |
| assert resolve_template("[abc]") == {"a", "b", "c"} | |
| assert resolve_template("a[bc]d") == {"abd", "acd"} | |
| assert resolve_template("[Hh]ello, [Ww]orld") \ | |
| == {"Hello, World", "Hello, world", "hello, World", "hello, world"} | |
| assert resolve_template("[ab]x[cd]y[ef]") \ | |
| == {"axcye", "axcyf", "axdye", "axdyf", | |
| "bxcye", "bxcyf", "bxdye", "bxdyf"} | |
| assert resolve_template("[abc]abc[ef]") \ | |
| == {"aabce", "aabcf", "babce", "babcf", "cabce", "cabcf"} | |
| assert resolve_template("[ab][a][b][c][d]") \ | |
| == {"aabcd", "babcd"} | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment